author | robin |
Wed, 05 Feb 2014 15:42:45 +0000 | |
branch | py33 |
changeset 4020 | 13ff3d55b5c2 |
parent 4018 | c69c31436936 |
child 4067 | 712e1822ca31 |
permissions | -rw-r--r-- |
3617 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2012 |
494 | 2 |
#see license.txt for license details |
2332 | 3 |
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/platypus/doctemplate.py |
565 | 4 |
|
2332 | 5 |
__version__=''' $Id$ ''' |
565 | 6 |
|
197 | 7 |
__doc__=""" |
268 | 8 |
This module contains the core structure of platypus. |
9 |
||
2762 | 10 |
rlatypus constructs documents. Document styles are determined by DocumentTemplates. |
268 | 11 |
|
12 |
Each DocumentTemplate contains one or more PageTemplates which defines the look of the |
|
13 |
pages of the document. |
|
14 |
||
15 |
Each PageTemplate has a procedure for drawing the "non-flowing" part of the page |
|
16 |
(for example the header, footer, page number, fixed logo graphic, watermark, etcetera) and |
|
17 |
a set of Frames which enclose the flowing part of the page (for example the paragraphs, |
|
18 |
tables, or non-fixed diagrams of the text). |
|
19 |
||
20 |
A document is built when a DocumentTemplate is fed a sequence of Flowables. |
|
21 |
The action of the build consumes the flowables in order and places them onto |
|
22 |
frames on pages as space allows. When a frame runs out of space the next frame |
|
23 |
of the page is used. If no frame remains a new page is created. A new page |
|
24 |
can also be created if a page break is forced. |
|
25 |
||
26 |
The special invisible flowable NextPageTemplate can be used to specify |
|
27 |
the page template for the next page (which by default is the one being used |
|
28 |
for the current frame). |
|
197 | 29 |
""" |
565 | 30 |
|
279 | 31 |
from reportlab.platypus.flowables import * |
2525 | 32 |
from reportlab.lib.units import inch |
279 | 33 |
from reportlab.platypus.paragraph import Paragraph |
34 |
from reportlab.platypus.frames import Frame |
|
1530 | 35 |
from reportlab.rl_config import defaultPageSize, verbose |
279 | 36 |
import reportlab.lib.sequencer |
2525 | 37 |
from reportlab.pdfgen import canvas |
4018
c69c31436936
doctemplate.py: use annotateException & allow page templates to be any string type
robin
parents:
3975
diff
changeset
|
38 |
from reportlab.lib.utils import isSeq, encode_label, decode_label, annotateException, strTypes |
3083 | 39 |
try: |
40 |
set |
|
41 |
except NameError: |
|
42 |
from sets import Set as set |
|
565 | 43 |
|
197 | 44 |
import sys |
2500 | 45 |
import logging |
46 |
logger = logging.getLogger("reportlab.platypus") |
|
197 | 47 |
|
2192 | 48 |
class LayoutError(Exception): |
49 |
pass |
|
565 | 50 |
|
3131
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
51 |
def _fSizeString(f): |
3523 | 52 |
#used to get size during error messages |
3131
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
53 |
w=getattr(f,'width',None) |
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
54 |
if w is None: |
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
55 |
w=getattr(f,'_width',None) |
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
56 |
|
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
57 |
h=getattr(f,'height',None) |
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
58 |
if h is None: |
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
59 |
h=getattr(f,'_height',None) |
3523 | 60 |
#tables in particular may have some nasty large culprit |
61 |
if hasattr(f, '_culprit'): |
|
62 |
c = ', %s, ' % f._culprit() |
|
63 |
else: |
|
64 |
c = '' |
|
3131
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
65 |
if w is not None or h is not None: |
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
66 |
if w is None: w='???' |
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
67 |
if h is None: h='???' |
3523 | 68 |
return '(%s x %s)%s' % (w,h,c) |
3131
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
69 |
return '' |
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
70 |
|
253 | 71 |
def _doNothing(canvas, doc): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
72 |
"Dummy callback for onPage" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
73 |
pass |
512 | 74 |
|
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
75 |
class PTCycle(list): |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
76 |
def __init__(self): |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
77 |
self._restart = 0 |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
78 |
self._idx = 0 |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
79 |
list.__init__(self) |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
80 |
|
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
81 |
def cyclicIterator(self): |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
82 |
while 1: |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
83 |
yield self[self._idx] |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
84 |
self._idx += 1 |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
85 |
if self._idx>=len(self): |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
86 |
self._idx = self._restart |
565 | 87 |
|
1440
243d35446390
Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents:
1428
diff
changeset
|
88 |
class IndexingFlowable(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
89 |
"""Abstract interface definition for flowables which might |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
90 |
hold references to other pages or themselves be targets |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
91 |
of cross-references. XRefStart, XRefDest, Table of Contents, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
92 |
Indexes etc.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
93 |
def isIndexing(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
94 |
return 1 |
512 | 95 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
96 |
def isSatisfied(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
97 |
return 1 |
512 | 98 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
99 |
def notify(self, kind, stuff): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
100 |
"""This will be called by the framework wherever 'stuff' happens. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
101 |
'kind' will be a value that can be used to decide whether to |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
102 |
pay attention or not.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
103 |
pass |
512 | 104 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
105 |
def beforeBuild(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
106 |
"""Called by multiBuild before it starts; use this to clear |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
107 |
old contents""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
108 |
pass |
1428 | 109 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
110 |
def afterBuild(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
111 |
"""Called after build ends but before isSatisfied""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
112 |
pass |
253 | 113 |
|
197 | 114 |
class ActionFlowable(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
115 |
'''This Flowable is never drawn, it can be used for data driven controls |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
116 |
For example to change a page template (from one column to two, for example) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
117 |
use NextPageTemplate which creates an ActionFlowable. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
118 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
119 |
def __init__(self,action=()): |
2575 | 120 |
#must call super init to ensure it has a width and height (of zero), |
121 |
#as in some cases the packer might get called on it... |
|
122 |
Flowable.__init__(self) |
|
3731 | 123 |
if not isSeq(action): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
124 |
action = (action,) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
125 |
self.action = tuple(action) |
197 | 126 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
127 |
def apply(self,doc): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
128 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
129 |
This is called by the doc.build processing to allow the instance to |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
130 |
implement its behaviour |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
131 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
132 |
action = self.action[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
133 |
args = tuple(self.action[1:]) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
134 |
arn = 'handle_'+action |
2762 | 135 |
if arn=="handle_nextPageTemplate" and args[0]=='main': |
136 |
pass |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
137 |
try: |
2762 | 138 |
getattr(doc,arn)(*args) |
3721 | 139 |
except AttributeError as aerr: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
140 |
if aerr.args[0]==arn: |
3721 | 141 |
raise NotImplementedError("Can't handle ActionFlowable(%s)" % action) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
142 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
143 |
raise |
3175
bf81f5b13ba8
doctemplate.py: fix bogus exception and allow for build keyword args in multibuild
rgbecker
parents:
3161
diff
changeset
|
144 |
except: |
4018
c69c31436936
doctemplate.py: use annotateException & allow page templates to be any string type
robin
parents:
3975
diff
changeset
|
145 |
annotateException("\nhandle_%s args=%s"%(action,ascii(args))) |
197 | 146 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
147 |
def __call__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
148 |
return self |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
149 |
|
2192 | 150 |
def identity(self, maxLen=None): |
2531 | 151 |
return "ActionFlowable: %s%s" % (str(self.action),self._frameName()) |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2196
diff
changeset
|
152 |
|
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
153 |
class LCActionFlowable(ActionFlowable): |
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
154 |
locChanger = 1 #we cause a frame or page change |
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
155 |
|
2450 | 156 |
def wrap(self, availWidth, availHeight): |
157 |
'''Should never be called.''' |
|
158 |
raise NotImplementedError |
|
159 |
||
160 |
def draw(self): |
|
161 |
'''Should never be called.''' |
|
162 |
raise NotImplementedError |
|
163 |
||
1324 | 164 |
class NextFrameFlowable(ActionFlowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
165 |
def __init__(self,ix,resume=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
166 |
ActionFlowable.__init__(self,('nextFrame',ix,resume)) |
565 | 167 |
|
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
168 |
class CurrentFrameFlowable(LCActionFlowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
169 |
def __init__(self,ix,resume=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
170 |
ActionFlowable.__init__(self,('currentFrame',ix,resume)) |
1324 | 171 |
|
2575 | 172 |
class NullActionFlowable(ActionFlowable): |
3095
65e60a3bc048
doctemplate.py: standardize NullActionFlowable apply (bug fix from Hans Brand hans at marcans.nl)
rgbecker
parents:
3083
diff
changeset
|
173 |
def apply(self,doc): |
2575 | 174 |
pass |
175 |
||
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
176 |
class _FrameBreak(LCActionFlowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
177 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
178 |
A special ActionFlowable that allows setting doc._nextFrameIndex |
1324 | 179 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
180 |
eg story.append(FrameBreak('mySpecialFrame')) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
181 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
182 |
def __call__(self,ix=None,resume=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
183 |
r = self.__class__(self.action+(resume,)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
184 |
r._ix = ix |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
185 |
return r |
1324 | 186 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
187 |
def apply(self,doc): |
2529
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
188 |
if getattr(self,'_ix',None): |
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
189 |
doc.handle_nextFrame(self._ix) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
190 |
ActionFlowable.apply(self,doc) |
1324 | 191 |
|
192 |
FrameBreak = _FrameBreak('frameEnd') |
|
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
193 |
PageBegin = LCActionFlowable('pageBegin') |
197 | 194 |
|
2366 | 195 |
def _evalMeasurement(n): |
3731 | 196 |
if isinstance(n,str): |
3975
4a3599863c11
eliminate from . imports in favour of absolutes to allow running modules
robin
parents:
3884
diff
changeset
|
197 |
from reportlab.platypus.paraparser import _num |
2366 | 198 |
n = _num(n) |
3731 | 199 |
if isSeq(n): n = n[1] |
2366 | 200 |
return n |
201 |
||
2525 | 202 |
class FrameActionFlowable(Flowable): |
2762 | 203 |
_fixedWidth = _fixedHeight = 1 |
2525 | 204 |
def __init__(self,*arg,**kw): |
205 |
raise NotImplementedError('Abstract Class') |
|
206 |
||
207 |
def frameAction(self,frame): |
|
208 |
raise NotImplementedError('Abstract Class') |
|
209 |
||
210 |
class Indenter(FrameActionFlowable): |
|
1923 | 211 |
"""Increases or decreases left and right margins of frame. |
212 |
||
213 |
This allows one to have a 'context-sensitive' indentation |
|
214 |
and makes nested lists way easier. |
|
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2196
diff
changeset
|
215 |
""" |
3686 | 216 |
_ZEROSIZE=True |
217 |
width=0 |
|
218 |
height=0 |
|
1923 | 219 |
def __init__(self, left=0, right=0): |
2366 | 220 |
self.left = _evalMeasurement(left) |
221 |
self.right = _evalMeasurement(right) |
|
1923 | 222 |
|
2525 | 223 |
def frameAction(self, frame): |
224 |
frame._leftExtraIndent += self.left |
|
225 |
frame._rightExtraIndent += self.right |
|
512 | 226 |
|
2762 | 227 |
class NotAtTopPageBreak(FrameActionFlowable): |
228 |
def __init__(self): |
|
229 |
pass |
|
230 |
||
231 |
def frameAction(self,frame): |
|
232 |
if not frame._atTop: |
|
2950 | 233 |
frame.add_generated_content(PageBreak()) |
2762 | 234 |
|
197 | 235 |
class NextPageTemplate(ActionFlowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
236 |
"""When you get to the next page, use the template specified (change to two column, for example) """ |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
237 |
def __init__(self,pt): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
238 |
ActionFlowable.__init__(self,('nextPageTemplate',pt)) |
197 | 239 |
|
240 |
class PageTemplate: |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
241 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
242 |
essentially a list of Frames and an onPage routine to call at the start |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
243 |
of a page when this is selected. onPageEnd gets called at the end. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
244 |
derived classes can also implement beforeDrawPage and afterDrawPage if they want |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
245 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
246 |
def __init__(self,id=None,frames=[],onPage=_doNothing, onPageEnd=_doNothing, |
3563
3ba0d95480c9
doctemplate.py added support for PageTemplate.autoNextPageTemplate
rgbecker
parents:
3528
diff
changeset
|
247 |
pagesize=None, autoNextPageTemplate=None): |
3272
10f6899a18a5
reportlab: attempt to eliminate empty list argument issues bug contributed by Tim Roberts and Nate Silva
rgbecker
parents:
3241
diff
changeset
|
248 |
frames = frames or [] |
3731 | 249 |
if not isSeq(frames): frames = [frames] |
3721 | 250 |
assert [x for x in frames if not isinstance(x,Frame)]==[], "frames argument error" |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
251 |
self.id = id |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
252 |
self.frames = frames |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
253 |
self.onPage = onPage |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
254 |
self.onPageEnd = onPageEnd |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
255 |
self.pagesize = pagesize |
3563
3ba0d95480c9
doctemplate.py added support for PageTemplate.autoNextPageTemplate
rgbecker
parents:
3528
diff
changeset
|
256 |
self.autoNextPageTemplate = autoNextPageTemplate |
512 | 257 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
258 |
def beforeDrawPage(self,canv,doc): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
259 |
"""Override this if you want additional functionality or prefer |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
260 |
a class based page routine. Called before any flowables for |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
261 |
this page are processed.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
262 |
pass |
197 | 263 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
264 |
def checkPageSize(self,canv,doc): |
2688
f9c4d1488516
doctemplate: apply T Heller's fix emacs syntax colouring patch
rgbecker
parents:
2672
diff
changeset
|
265 |
"""This gets called by the template framework |
1926 | 266 |
If canv size != template size then the canv size is set to |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
267 |
the template size or if that's not available to the |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
268 |
doc size. |
2688
f9c4d1488516
doctemplate: apply T Heller's fix emacs syntax colouring patch
rgbecker
parents:
2672
diff
changeset
|
269 |
""" |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
270 |
#### NEVER EVER EVER COMPARE FLOATS FOR EQUALITY |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
271 |
#RGB converting pagesizes to ints means we are accurate to one point |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
272 |
#RGB I suggest we should be aiming a little better |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
273 |
cp = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
274 |
dp = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
275 |
sp = None |
3721 | 276 |
if canv._pagesize: cp = list(map(int, canv._pagesize)) |
277 |
if self.pagesize: sp = list(map(int, self.pagesize)) |
|
278 |
if doc.pagesize: dp = list(map(int, doc.pagesize)) |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
279 |
if cp!=sp: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
280 |
if sp: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
281 |
canv.setPageSize(self.pagesize) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
282 |
elif cp!=dp: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
283 |
canv.setPageSize(doc.pagesize) |
936 | 284 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
285 |
def afterDrawPage(self, canv, doc): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
286 |
"""This is called after the last flowable for the page has |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
287 |
been processed. You might use this if the page header or |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
288 |
footer needed knowledge of what flowables were drawn on |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
289 |
this page.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
290 |
pass |
1428 | 291 |
|
3009 | 292 |
def _addGeneratedContent(flowables,frame): |
293 |
S = getattr(frame,'_generated_content',None) |
|
3323
8fe71f699df9
doctemplate.py: fix bug in order of split[0] generated content contributed by Robert Hölzl <robert.hoelzl@baltech.de>
rgbecker
parents:
3272
diff
changeset
|
294 |
if S: |
8fe71f699df9
doctemplate.py: fix bug in order of split[0] generated content contributed by Robert Hölzl <robert.hoelzl@baltech.de>
rgbecker
parents:
3272
diff
changeset
|
295 |
flowables[0:0] = S |
3009 | 296 |
del frame._generated_content |
297 |
||
3241
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
298 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
299 |
class onDrawStr(str): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
300 |
def __new__(cls,value,onDraw,label,kind=None): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
301 |
self = str.__new__(cls,value) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
302 |
self.onDraw = onDraw |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
303 |
self.kind = kind |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
304 |
self.label = label |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
305 |
return self |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
306 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
307 |
class PageAccumulator: |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
308 |
'''gadget to accumulate information in a page |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
309 |
and then allow it to be interrogated at the end |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
310 |
of the page''' |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
311 |
_count = 0 |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
312 |
def __init__(self,name=None): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
313 |
if name is None: |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
314 |
name = self.__class__.__name__+str(self.__class__._count) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
315 |
self.__class__._count += 1 |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
316 |
self.name = name |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
317 |
self.data = [] |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
318 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
319 |
def reset(self): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
320 |
self.data[:] = [] |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
321 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
322 |
def add(self,*args): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
323 |
self.data.append(args) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
324 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
325 |
def onDrawText(self,*args): |
3850 | 326 |
return '<onDraw name="%s" label="%s" />' % (self.name,encode_label(args)) |
3241
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
327 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
328 |
def __call__(self,canv,kind,label): |
3850 | 329 |
self.add(*decode_label(label)) |
3241
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
330 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
331 |
def attachToPageTemplate(self,pt): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
332 |
if pt.onPage: |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
333 |
def onPage(canv,doc,oop=pt.onPage): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
334 |
self.onPage(canv,doc) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
335 |
oop(canv,doc) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
336 |
else: |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
337 |
def onPage(canv,doc): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
338 |
self.onPage(canv,doc) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
339 |
pt.onPage = onPage |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
340 |
if pt.onPageEnd: |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
341 |
def onPageEnd(canv,doc,oop=pt.onPageEnd): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
342 |
self.onPageEnd(canv,doc) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
343 |
oop(canv,doc) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
344 |
else: |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
345 |
def onPageEnd(canv,doc): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
346 |
self.onPageEnd(canv,doc) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
347 |
pt.onPageEnd = onPageEnd |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
348 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
349 |
def onPage(self,canv,doc): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
350 |
'''this will be called at the start of the page''' |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
351 |
setattr(canv,self.name,self) #push ourselves onto the canvas |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
352 |
self.reset() |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
353 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
354 |
def onPageEnd(self,canv,doc): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
355 |
'''this will be called at the end of a page''' |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
356 |
self.pageEndAction(canv,doc) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
357 |
try: |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
358 |
delattr(canv,self.name) |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
359 |
except: |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
360 |
pass |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
361 |
self.reset() |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
362 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
363 |
def pageEndAction(self,canv,doc): |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
364 |
'''this should be overridden to do something useful''' |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
365 |
pass |
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
366 |
|
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
367 |
def onDrawStr(self,value,*args): |
3850 | 368 |
return onDrawStr(value,self,encode_label(args)) |
3241
575302ed6ab9
doctemplate.py : add PageAccumulator class and support in tables.py
rgbecker
parents:
3175
diff
changeset
|
369 |
|
512 | 370 |
class BaseDocTemplate: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
371 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
372 |
First attempt at defining a document template class. |
512 | 373 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
374 |
The basic idea is simple. |
3031 | 375 |
|
376 |
1) The document has a list of data associated with it |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
377 |
this data should derive from flowables. We'll have |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
378 |
special classes like PageBreak, FrameBreak to do things |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
379 |
like forcing a page end etc. |
512 | 380 |
|
3031 | 381 |
2) The document has one or more page templates. |
512 | 382 |
|
3031 | 383 |
3) Each page template has one or more frames. |
512 | 384 |
|
3031 | 385 |
4) The document class provides base methods for handling the |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
386 |
story events and some reasonable methods for getting the |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
387 |
story flowables into the frames. |
214 | 388 |
|
3031 | 389 |
5) The document instances can override the base handler routines. |
1428 | 390 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
391 |
Most of the methods for this class are not called directly by the user, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
392 |
but in some advanced usages they may need to be overridden via subclassing. |
1428 | 393 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
394 |
EXCEPTION: doctemplate.build(...) must be called for most reasonable uses |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
395 |
since it builds a document using the page template. |
1428 | 396 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
397 |
Each document template builds exactly one document into a file specified |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
398 |
by the filename argument on initialization. |
197 | 399 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
400 |
Possible keyword arguments for the initialization: |
1428 | 401 |
|
3031 | 402 |
- pageTemplates: A list of templates. Must be nonempty. Names |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
403 |
assigned to the templates are used for referring to them so no two used |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
404 |
templates should have the same name. For example you might want one template |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
405 |
for a title page, one for a section first page, one for a first page of |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
406 |
a chapter and two more for the interior of a chapter on odd and even pages. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
407 |
If this argument is omitted then at least one pageTemplate should be provided |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
408 |
using the addPageTemplates method before the document is built. |
3031 | 409 |
- pageSize: a 2-tuple or a size constant from reportlab/lib/pagesizes.pu. |
410 |
Used by the SimpleDocTemplate subclass which does NOT accept a list of |
|
411 |
pageTemplates but makes one for you; ignored when using pageTemplates. |
|
2216 | 412 |
|
3031 | 413 |
- showBoundary: if set draw a box around the frame boundaries. |
414 |
- leftMargin: |
|
415 |
- rightMargin: |
|
416 |
- topMargin: |
|
417 |
- bottomMargin: Margin sizes in points (default 1 inch). These margins may be |
|
418 |
overridden by the pageTemplates. They are primarily of interest for the |
|
419 |
SimpleDocumentTemplate subclass. |
|
420 |
||
421 |
- allowSplitting: If set flowables (eg, paragraphs) may be split across frames or pages |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
422 |
(default: 1) |
3031 | 423 |
- title: Internal title for document (does not automatically display on any page) |
424 |
- author: Internal author for document (does not automatically display on any page) |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
425 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
426 |
_initArgs = { 'pagesize':defaultPageSize, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
427 |
'pageTemplates':[], |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
428 |
'showBoundary':0, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
429 |
'leftMargin':inch, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
430 |
'rightMargin':inch, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
431 |
'topMargin':inch, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
432 |
'bottomMargin':inch, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
433 |
'allowSplitting':1, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
434 |
'title':None, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
435 |
'author':None, |
2666 | 436 |
'subject':None, |
3456 | 437 |
'creator':None, |
2666 | 438 |
'keywords':[], |
1839
084e4af662dc
Passing invariant argument through to canvas
andy_robinson
parents:
1838
diff
changeset
|
439 |
'invariant':None, |
2531 | 440 |
'pageCompression':None, |
2492
8142a1737889
doctemplate.py: add rotation argument to BaseDocTemplate
rgbecker
parents:
2491
diff
changeset
|
441 |
'_pageBreakQuick':1, |
2531 | 442 |
'rotation':0, |
3040 | 443 |
'_debug':0, |
3110 | 444 |
'encrypt': None, |
445 |
'cropMarks': None, |
|
3443
64d1fba94908
reportlab: added preliminary support for enforcing color spaces
rgbecker
parents:
3384
diff
changeset
|
446 |
'enforceColorSpace': None, |
3110 | 447 |
} |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
448 |
_invalidInitArgs = () |
2104 | 449 |
_firstPageTemplateIndex = 0 |
197 | 450 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
451 |
def __init__(self, filename, **kw): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
452 |
"""create a document template bound to a filename (see class documentation for keyword arguments)""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
453 |
self.filename = filename |
2955 | 454 |
self._nameSpace = dict(doc=self) |
455 |
self._lifetimes = {} |
|
512 | 456 |
|
3723
99aa837b6703
second stage of port to Python 3.3; working hello world
rptlab
parents:
3721
diff
changeset
|
457 |
for k in self._initArgs.keys(): |
3326 | 458 |
if k not in kw: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
459 |
v = self._initArgs[k] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
460 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
461 |
if k in self._invalidInitArgs: |
3721 | 462 |
raise ValueError("Invalid argument %s" % k) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
463 |
v = kw[k] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
464 |
setattr(self,k,v) |
310 | 465 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
466 |
p = self.pageTemplates |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
467 |
self.pageTemplates = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
468 |
self.addPageTemplates(p) |
1428 | 469 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
470 |
# facility to assist multi-build and cross-referencing. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
471 |
# various hooks can put things into here - key is what |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
472 |
# you want, value is a page number. This can then be |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
473 |
# passed to indexing flowables. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
474 |
self._pageRefs = {} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
475 |
self._indexingFlowables = [] |
512 | 476 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
477 |
#callback facility for progress monitoring |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
478 |
self._onPage = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
479 |
self._onProgress = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
480 |
self._flowableCount = 0 # so we know how far to go |
1668
448a9205be12
Provision for callback progress monitoring in basic doctemplate class
andy_robinson
parents:
1530
diff
changeset
|
481 |
|
2192 | 482 |
#infinite loop detection if we start doing lots of empty pages |
483 |
self._curPageFlowableCount = 0 |
|
484 |
self._emptyPages = 0 |
|
2193 | 485 |
self._emptyPagesAllowed = 10 |
2192 | 486 |
|
1923 | 487 |
#context sensitive margins - set by story, not from outside |
488 |
self._leftExtraIndent = 0.0 |
|
489 |
self._rightExtraIndent = 0.0 |
|
3760
674899ebab37
platypus: FrameBG related stuff ifrom default that got lost somehow
robin
parents:
3731
diff
changeset
|
490 |
self._frameBGs = [] |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2196
diff
changeset
|
491 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
492 |
self._calc() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
493 |
self.afterInit() |
1502 | 494 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
495 |
def _calc(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
496 |
self._rightMargin = self.pagesize[0] - self.rightMargin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
497 |
self._topMargin = self.pagesize[1] - self.topMargin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
498 |
self.width = self._rightMargin - self.leftMargin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
499 |
self.height = self._topMargin - self.bottomMargin |
1502 | 500 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
501 |
def setPageCallBack(self, func): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
502 |
'Simple progress monitor - func(pageNo) called on each new page' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
503 |
self._onPage = func |
197 | 504 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
505 |
def setProgressCallBack(self, func): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
506 |
'''Cleverer progress monitor - func(typ, value) called regularly''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
507 |
self._onProgress = func |
1668
448a9205be12
Provision for callback progress monitoring in basic doctemplate class
andy_robinson
parents:
1530
diff
changeset
|
508 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
509 |
def clean_hanging(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
510 |
'handle internal postponed actions' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
511 |
while len(self._hanging): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
512 |
self.handle_flowable(self._hanging) |
1428 | 513 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
514 |
def addPageTemplates(self,pageTemplates): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
515 |
'add one or a sequence of pageTemplates' |
3731 | 516 |
if not isSeq(pageTemplates): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
517 |
pageTemplates = [pageTemplates] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
518 |
#this test below fails due to inconsistent imports! |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
519 |
#assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
520 |
for t in pageTemplates: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
521 |
self.pageTemplates.append(t) |
1502 | 522 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
523 |
def handle_documentBegin(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
524 |
'''implement actions at beginning of document''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
525 |
self._hanging = [PageBegin] |
2104 | 526 |
self.pageTemplate = self.pageTemplates[self._firstPageTemplateIndex] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
527 |
self.page = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
528 |
self.beforeDocument() |
253 | 529 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
530 |
def handle_pageBegin(self): |
2688
f9c4d1488516
doctemplate: apply T Heller's fix emacs syntax colouring patch
rgbecker
parents:
2672
diff
changeset
|
531 |
"""Perform actions required at beginning of page. |
f9c4d1488516
doctemplate: apply T Heller's fix emacs syntax colouring patch
rgbecker
parents:
2672
diff
changeset
|
532 |
shouldn't normally be called directly""" |
2593
3adaab508968
reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents:
2575
diff
changeset
|
533 |
self.page += 1 |
2531 | 534 |
if self._debug: logger.debug("beginning page %d" % self.page) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
535 |
self.pageTemplate.beforeDrawPage(self.canv,self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
536 |
self.pageTemplate.checkPageSize(self.canv,self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
537 |
self.pageTemplate.onPage(self.canv,self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
538 |
for f in self.pageTemplate.frames: f._reset() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
539 |
self.beforePage() |
2192 | 540 |
#keep a count of flowables added to this page. zero indicates bad stuff |
541 |
self._curPageFlowableCount = 0 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
542 |
if hasattr(self,'_nextFrameIndex'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
543 |
del self._nextFrameIndex |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
544 |
self.frame = self.pageTemplate.frames[0] |
2531 | 545 |
self.frame._debug = self._debug |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
546 |
self.handle_frameBegin() |
197 | 547 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
548 |
def handle_pageEnd(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
549 |
''' show the current page |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
550 |
check the next page template |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
551 |
hang a page begin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
552 |
''' |
2957 | 553 |
self._removeVars(('page','frame')) |
3519
eae7f1448301
doctemplate.py: fix transfer of extra indents in handle_pageBreak
rgbecker
parents:
3484
diff
changeset
|
554 |
self._leftExtraIndent = self.frame._leftExtraIndent |
eae7f1448301
doctemplate.py: fix transfer of extra indents in handle_pageBreak
rgbecker
parents:
3484
diff
changeset
|
555 |
self._rightExtraIndent = self.frame._rightExtraIndent |
3760
674899ebab37
platypus: FrameBG related stuff ifrom default that got lost somehow
robin
parents:
3731
diff
changeset
|
556 |
self._frameBGs = self.frame._frameBGs |
2192 | 557 |
#detect infinite loops... |
558 |
if self._curPageFlowableCount == 0: |
|
2593
3adaab508968
reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents:
2575
diff
changeset
|
559 |
self._emptyPages += 1 |
2192 | 560 |
else: |
561 |
self._emptyPages = 0 |
|
562 |
if self._emptyPages >= self._emptyPagesAllowed: |
|
2196 | 563 |
if 1: |
2466 | 564 |
ident = "More than %d pages generated without content - halting layout. Likely that a flowable is too large for any frame." % self._emptyPagesAllowed |
565 |
#leave to keep apart from the raise |
|
566 |
raise LayoutError(ident) |
|
2196 | 567 |
else: |
568 |
pass #attempt to restore to good state |
|
569 |
else: |
|
570 |
if self._onProgress: |
|
571 |
self._onProgress('PAGE', self.canv.getPageNumber()) |
|
572 |
self.pageTemplate.afterDrawPage(self.canv, self) |
|
573 |
self.pageTemplate.onPageEnd(self.canv, self) |
|
574 |
self.afterPage() |
|
2531 | 575 |
if self._debug: logger.debug("ending page %d" % self.page) |
2492
8142a1737889
doctemplate.py: add rotation argument to BaseDocTemplate
rgbecker
parents:
2491
diff
changeset
|
576 |
self.canv.setPageRotation(getattr(self.pageTemplate,'rotation',self.rotation)) |
2196 | 577 |
self.canv.showPage() |
2418 | 578 |
|
579 |
if hasattr(self,'_nextPageTemplateCycle'): |
|
580 |
#they are cycling through pages'; we keep the index |
|
3721 | 581 |
self.pageTemplate = next(self._nextPageTemplateCycle) |
2418 | 582 |
elif hasattr(self,'_nextPageTemplateIndex'): |
2196 | 583 |
self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex] |
584 |
del self._nextPageTemplateIndex |
|
3563
3ba0d95480c9
doctemplate.py added support for PageTemplate.autoNextPageTemplate
rgbecker
parents:
3528
diff
changeset
|
585 |
elif self.pageTemplate.autoNextPageTemplate: |
3ba0d95480c9
doctemplate.py added support for PageTemplate.autoNextPageTemplate
rgbecker
parents:
3528
diff
changeset
|
586 |
self.handle_nextPageTemplate(self.pageTemplate.autoNextPageTemplate) |
3ba0d95480c9
doctemplate.py added support for PageTemplate.autoNextPageTemplate
rgbecker
parents:
3528
diff
changeset
|
587 |
self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex] |
2196 | 588 |
if self._emptyPages==0: |
589 |
pass #store good state here |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
590 |
self._hanging.append(PageBegin) |
197 | 591 |
|
2408 | 592 |
def handle_pageBreak(self,slow=None): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
593 |
'''some might choose not to end all the frames''' |
2408 | 594 |
if self._pageBreakQuick and not slow: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
595 |
self.handle_pageEnd() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
596 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
597 |
n = len(self._hanging) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
598 |
while len(self._hanging)==n: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
599 |
self.handle_frameEnd() |
512 | 600 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
601 |
def handle_frameBegin(self,resume=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
602 |
'''What to do at the beginning of a frame''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
603 |
f = self.frame |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
604 |
if f._atTop: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
605 |
if self.showBoundary or self.frame.showBoundary: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
606 |
self.frame.drawBoundary(self.canv) |
1923 | 607 |
f._leftExtraIndent = self._leftExtraIndent |
608 |
f._rightExtraIndent = self._rightExtraIndent |
|
3760
674899ebab37
platypus: FrameBG related stuff ifrom default that got lost somehow
robin
parents:
3731
diff
changeset
|
609 |
f._frameBGs = self._frameBGs |
197 | 610 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
611 |
def handle_frameEnd(self,resume=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
612 |
''' Handles the semantics of the end of a frame. This includes the selection of |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
613 |
the next frame or if this is the last frame then invoke pageEnd. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
614 |
''' |
2957 | 615 |
self._removeVars(('frame',)) |
1923 | 616 |
self._leftExtraIndent = self.frame._leftExtraIndent |
617 |
self._rightExtraIndent = self.frame._rightExtraIndent |
|
3760
674899ebab37
platypus: FrameBG related stuff ifrom default that got lost somehow
robin
parents:
3731
diff
changeset
|
618 |
self._frameBGs = self.frame._frameBGs |
1923 | 619 |
|
2950 | 620 |
f = self.frame |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
621 |
if hasattr(self,'_nextFrameIndex'): |
2529
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
622 |
self.frame = self.pageTemplate.frames[self._nextFrameIndex] |
2531 | 623 |
self.frame._debug = self._debug |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
624 |
del self._nextFrameIndex |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
625 |
self.handle_frameBegin(resume) |
2950 | 626 |
elif hasattr(f,'lastFrame') or f is self.pageTemplate.frames[-1]: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
627 |
self.handle_pageEnd() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
628 |
self.frame = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
629 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
630 |
self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1] |
2531 | 631 |
self.frame._debug = self._debug |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
632 |
self.handle_frameBegin() |
197 | 633 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
634 |
def handle_nextPageTemplate(self,pt): |
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
635 |
'''On endPage change to the page template with name or index pt''' |
4018
c69c31436936
doctemplate.py: use annotateException & allow page templates to be any string type
robin
parents:
3975
diff
changeset
|
636 |
if isinstance(pt,strTypes): |
2418 | 637 |
if hasattr(self, '_nextPageTemplateCycle'): del self._nextPageTemplateCycle |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
638 |
for t in self.pageTemplates: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
639 |
if t.id == pt: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
640 |
self._nextPageTemplateIndex = self.pageTemplates.index(t) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
641 |
return |
3721 | 642 |
raise ValueError("can't find template('%s')"%pt) |
3731 | 643 |
elif isinstance(pt,int): |
2418 | 644 |
if hasattr(self, '_nextPageTemplateCycle'): del self._nextPageTemplateCycle |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
645 |
self._nextPageTemplateIndex = pt |
3731 | 646 |
elif isSeq(pt): |
2418 | 647 |
#used for alternating left/right pages |
648 |
#collect the refs to the template objects, complain if any are bad |
|
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
649 |
c = PTCycle() |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
650 |
for ptn in pt: |
2418 | 651 |
found = 0 |
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
652 |
if ptn=='*': #special case name used to short circuit the iteration |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
653 |
c._restart = len(c) |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
654 |
continue |
2418 | 655 |
for t in self.pageTemplates: |
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
656 |
if t.id == ptn: |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
657 |
c.append(t) |
2418 | 658 |
found = 1 |
659 |
if not found: |
|
3161 | 660 |
raise ValueError("Cannot find page template called %s" % ptn) |
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
661 |
if not c: |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
662 |
raise ValueError("No valid page templates in cycle") |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
663 |
elif c._restart>len(c): |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
664 |
raise ValueError("Invalid cycle restart position") |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
665 |
|
2418 | 666 |
#ensure we start on the first one |
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
667 |
self._nextPageTemplateCycle = c.cyclicIterator() |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
668 |
else: |
2950 | 669 |
raise TypeError("argument pt should be string or integer or list") |
197 | 670 |
|
2529
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
671 |
def handle_nextFrame(self,fx,resume=0): |
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
672 |
'''On endFrame change to the frame with name or index fx''' |
4020 | 673 |
if isinstance(fx,strTypes): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
674 |
for f in self.pageTemplate.frames: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
675 |
if f.id == fx: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
676 |
self._nextFrameIndex = self.pageTemplate.frames.index(f) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
677 |
return |
2950 | 678 |
raise ValueError("can't find frame('%s') in %r(%s) which has frames %r"%(fx,self.pageTemplate,self.pageTemplate.id,[(f,f.id) for f in self.pageTemplate.frames])) |
3731 | 679 |
elif isinstance(fx,int): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
680 |
self._nextFrameIndex = fx |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
681 |
else: |
3721 | 682 |
raise TypeError("argument fx should be string or integer") |
512 | 683 |
|
2529
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
684 |
def handle_currentFrame(self,fx,resume=0): |
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
685 |
'''change to the frame with name or index fx''' |
2529
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
686 |
self.handle_nextFrame(fx,resume) |
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
687 |
self.handle_frameEnd(resume) |
197 | 688 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
689 |
def handle_breakBefore(self, flowables): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
690 |
'''preprocessing step to allow pageBreakBefore and frameBreakBefore attributes''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
691 |
first = flowables[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
692 |
# if we insert a page break before, we'll process that, see it again, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
693 |
# and go in an infinite loop. So we need to set a flag on the object |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
694 |
# saying 'skip me'. This should be unset on the next pass |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
695 |
if hasattr(first, '_skipMeNextTime'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
696 |
delattr(first, '_skipMeNextTime') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
697 |
return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
698 |
# this could all be made much quicker by putting the attributes |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
699 |
# in to the flowables with a defult value of 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
700 |
if hasattr(first,'pageBreakBefore') and first.pageBreakBefore == 1: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
701 |
first._skipMeNextTime = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
702 |
first.insert(0, PageBreak()) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
703 |
return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
704 |
if hasattr(first,'style') and hasattr(first.style, 'pageBreakBefore') and first.style.pageBreakBefore == 1: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
705 |
first._skipMeNextTime = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
706 |
flowables.insert(0, PageBreak()) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
707 |
return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
708 |
if hasattr(first,'frameBreakBefore') and first.frameBreakBefore == 1: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
709 |
first._skipMeNextTime = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
710 |
flowables.insert(0, FrameBreak()) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
711 |
return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
712 |
if hasattr(first,'style') and hasattr(first.style, 'frameBreakBefore') and first.style.frameBreakBefore == 1: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
713 |
first._skipMeNextTime = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
714 |
flowables.insert(0, FrameBreak()) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
715 |
return |
1428 | 716 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
717 |
def handle_keepWithNext(self, flowables): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
718 |
"implements keepWithNext" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
719 |
i = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
720 |
n = len(flowables) |
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
721 |
while i<n and flowables[i].getKeepWithNext(): i += 1 |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
722 |
if i: |
2664 | 723 |
if i<n and not getattr(flowables[i],'locChanger',None): i += 1 |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
724 |
K = KeepTogether(flowables[:i]) |
2995
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
725 |
mbe = getattr(self,'_multiBuildEdits',None) |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
726 |
if mbe: |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
727 |
for f in K._content[:-1]: |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
728 |
if hasattr(f,'keepWithNext'): |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
729 |
mbe((setattr,f,'keepWithNext',f.keepWithNext)) |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
730 |
else: |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
731 |
mbe((delattr,f,'keepWithNext')) #must get it from a style |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
732 |
f.__dict__['keepWithNext'] = 0 |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
733 |
else: |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
734 |
for f in K._content[:-1]: |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
735 |
f.__dict__['keepWithNext'] = 0 |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
736 |
del flowables[:i] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
737 |
flowables.insert(0,K) |
1425 | 738 |
|
2531 | 739 |
def _fIdent(self,f,maxLen=None,frame=None): |
740 |
if frame: f._frame = frame |
|
741 |
try: |
|
742 |
return f.identity(maxLen) |
|
743 |
finally: |
|
744 |
if frame: del f._frame |
|
745 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
746 |
def handle_flowable(self,flowables): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
747 |
'''try to handle one flowable from the front of list flowables.''' |
1428 | 748 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
749 |
#allow document a chance to look at, modify or ignore |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
750 |
#the object(s) about to be processed |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
751 |
self.filterFlowables(flowables) |
1428 | 752 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
753 |
self.handle_breakBefore(flowables) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
754 |
self.handle_keepWithNext(flowables) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
755 |
f = flowables[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
756 |
del flowables[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
757 |
if f is None: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
758 |
return |
197 | 759 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
760 |
if isinstance(f,PageBreak): |
2408 | 761 |
if isinstance(f,SlowPageBreak): |
762 |
self.handle_pageBreak(slow=1) |
|
763 |
else: |
|
764 |
self.handle_pageBreak() |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
765 |
self.afterFlowable(f) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
766 |
elif isinstance(f,ActionFlowable): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
767 |
f.apply(self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
768 |
self.afterFlowable(f) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
769 |
else: |
2762 | 770 |
frame = self.frame |
2950 | 771 |
canv = self.canv |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
772 |
#try to fit it then draw it |
2950 | 773 |
if frame.add(f, canv, trySplit=self.allowSplitting): |
2762 | 774 |
if not isinstance(f,FrameActionFlowable): |
775 |
self._curPageFlowableCount += 1 |
|
776 |
self.afterFlowable(f) |
|
3009 | 777 |
_addGeneratedContent(flowables,frame) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
778 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
779 |
if self.allowSplitting: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
780 |
# see if this is a splittable thing |
2950 | 781 |
S = frame.split(f,canv) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
782 |
n = len(S) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
783 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
784 |
n = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
785 |
if n: |
3528
5c3249611b60
doctemplate/flowables.py: fix DDIndenter split issue
rgbecker
parents:
3523
diff
changeset
|
786 |
if not isinstance(S[0],(PageBreak,SlowPageBreak,ActionFlowable,DDIndenter)): |
3323
8fe71f699df9
doctemplate.py: fix bug in order of split[0] generated content contributed by Robert Hölzl <robert.hoelzl@baltech.de>
rgbecker
parents:
3272
diff
changeset
|
787 |
if not frame.add(S[0], canv, trySplit=0): |
3484
9c12031155ba
platypus: fix splitting for ListFlowables (maybe)
rgbecker
parents:
3456
diff
changeset
|
788 |
ident = "Splitting error(n==%d) on page %d in\n%s\nS[0]=%s" % (n,self.page,self._fIdent(f,60,frame),self._fIdent(S[0],60,frame)) |
2575 | 789 |
#leave to keep apart from the raise |
790 |
raise LayoutError(ident) |
|
3323
8fe71f699df9
doctemplate.py: fix bug in order of split[0] generated content contributed by Robert Hölzl <robert.hoelzl@baltech.de>
rgbecker
parents:
3272
diff
changeset
|
791 |
self._curPageFlowableCount += 1 |
8fe71f699df9
doctemplate.py: fix bug in order of split[0] generated content contributed by Robert Hölzl <robert.hoelzl@baltech.de>
rgbecker
parents:
3272
diff
changeset
|
792 |
self.afterFlowable(S[0]) |
8fe71f699df9
doctemplate.py: fix bug in order of split[0] generated content contributed by Robert Hölzl <robert.hoelzl@baltech.de>
rgbecker
parents:
3272
diff
changeset
|
793 |
flowables[0:0] = S[1:] # put rest of splitted flowables back on the list |
8fe71f699df9
doctemplate.py: fix bug in order of split[0] generated content contributed by Robert Hölzl <robert.hoelzl@baltech.de>
rgbecker
parents:
3272
diff
changeset
|
794 |
_addGeneratedContent(flowables,frame) |
8fe71f699df9
doctemplate.py: fix bug in order of split[0] generated content contributed by Robert Hölzl <robert.hoelzl@baltech.de>
rgbecker
parents:
3272
diff
changeset
|
795 |
else: |
8fe71f699df9
doctemplate.py: fix bug in order of split[0] generated content contributed by Robert Hölzl <robert.hoelzl@baltech.de>
rgbecker
parents:
3272
diff
changeset
|
796 |
flowables[0:0] = S # put splitted flowables back on the list |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
797 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
798 |
if hasattr(f,'_postponed'): |
3131
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
799 |
ident = "Flowable %s%s too large on page %d in frame %r%s of template %r" % \ |
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
800 |
(self._fIdent(f,60,frame),_fSizeString(f),self.page, self.frame.id, |
0f15fabe9d8d
reportlab: improve information in doctemplate overflow error
rgbecker
parents:
3129
diff
changeset
|
801 |
self.frame._aSpaceString(), self.pageTemplate.id) |
2466 | 802 |
#leave to keep apart from the raise |
803 |
raise LayoutError(ident) |
|
2196 | 804 |
# this ought to be cleared when they are finally drawn! |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
805 |
f._postponed = 1 |
2998
efe6a1d93dd2
doctemplate.py: robustify access to _multiBuildEdits
rgbecker
parents:
2996
diff
changeset
|
806 |
mbe = getattr(self,'_multiBuildEdits',None) |
efe6a1d93dd2
doctemplate.py: robustify access to _multiBuildEdits
rgbecker
parents:
2996
diff
changeset
|
807 |
if mbe: |
efe6a1d93dd2
doctemplate.py: robustify access to _multiBuildEdits
rgbecker
parents:
2996
diff
changeset
|
808 |
mbe((delattr,f,'_postponed')) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
809 |
flowables.insert(0,f) # put the flowable back |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
810 |
self.handle_frameEnd() |
197 | 811 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
812 |
#these are provided so that deriving classes can refer to them |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
813 |
_handle_documentBegin = handle_documentBegin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
814 |
_handle_pageBegin = handle_pageBegin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
815 |
_handle_pageEnd = handle_pageEnd |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
816 |
_handle_frameBegin = handle_frameBegin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
817 |
_handle_frameEnd = handle_frameEnd |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
818 |
_handle_flowable = handle_flowable |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
819 |
_handle_nextPageTemplate = handle_nextPageTemplate |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
820 |
_handle_currentFrame = handle_currentFrame |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
821 |
_handle_nextFrame = handle_nextFrame |
1505 | 822 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
823 |
def _startBuild(self, filename=None, canvasmaker=canvas.Canvas): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
824 |
self._calc() |
2923 | 825 |
|
826 |
#each distinct pass gets a sequencer |
|
827 |
self.seq = reportlab.lib.sequencer.Sequencer() |
|
2955 | 828 |
|
1839
084e4af662dc
Passing invariant argument through to canvas
andy_robinson
parents:
1838
diff
changeset
|
829 |
self.canv = canvasmaker(filename or self.filename, |
084e4af662dc
Passing invariant argument through to canvas
andy_robinson
parents:
1838
diff
changeset
|
830 |
pagesize=self.pagesize, |
2531 | 831 |
invariant=self.invariant, |
3443
64d1fba94908
reportlab: added preliminary support for enforcing color spaces
rgbecker
parents:
3384
diff
changeset
|
832 |
pageCompression=self.pageCompression, |
64d1fba94908
reportlab: added preliminary support for enforcing color spaces
rgbecker
parents:
3384
diff
changeset
|
833 |
enforceColorSpace=self.enforceColorSpace, |
64d1fba94908
reportlab: added preliminary support for enforcing color spaces
rgbecker
parents:
3384
diff
changeset
|
834 |
) |
3040 | 835 |
|
3048
3ff2383916b9
Renamed setencrypt to setEncrypt to follow naming convention.
jonas
parents:
3040
diff
changeset
|
836 |
getattr(self.canv,'setEncrypt',lambda x: None)(self.encrypt) |
2666 | 837 |
|
3110 | 838 |
self.canv._cropMarks = self.cropMarks |
2666 | 839 |
self.canv.setAuthor(self.author) |
840 |
self.canv.setTitle(self.title) |
|
841 |
self.canv.setSubject(self.subject) |
|
3456 | 842 |
self.canv.setCreator(self.creator) |
2666 | 843 |
self.canv.setKeywords(self.keywords) |
2762 | 844 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
845 |
if self._onPage: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
846 |
self.canv.setPageCallBack(self._onPage) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
847 |
self.handle_documentBegin() |
512 | 848 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
849 |
def _endBuild(self): |
2957 | 850 |
self._removeVars(('build','page','frame')) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
851 |
if self._hanging!=[] and self._hanging[-1] is PageBegin: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
852 |
del self._hanging[-1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
853 |
self.clean_hanging() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
854 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
855 |
self.clean_hanging() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
856 |
self.handle_pageBreak() |
1505 | 857 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
858 |
if getattr(self,'_doSave',1): self.canv.save() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
859 |
if self._onPage: self.canv.setPageCallBack(None) |
512 | 860 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
861 |
def build(self, flowables, filename=None, canvasmaker=canvas.Canvas): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
862 |
"""Build the document from a list of flowables. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
863 |
If the filename argument is provided then that filename is used |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
864 |
rather than the one provided upon initialization. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
865 |
If the canvasmaker argument is provided then it will be used |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
866 |
instead of the default. For example a slideshow might use |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
867 |
an alternate canvas which places 6 slides on a page (by |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
868 |
doing translations, scalings and redefining the page break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
869 |
operations). |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
870 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
871 |
#assert filter(lambda x: not isinstance(x,Flowable), flowables)==[], "flowables argument error" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
872 |
flowableCount = len(flowables) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
873 |
if self._onProgress: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
874 |
self._onProgress('STARTED',0) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
875 |
self._onProgress('SIZE_EST', len(flowables)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
876 |
self._startBuild(filename,canvasmaker) |
512 | 877 |
|
2666 | 878 |
#pagecatcher can drag in information from embedded PDFs and we want ours |
879 |
#to take priority, so cache and reapply our own info dictionary after the build. |
|
2950 | 880 |
canv = self.canv |
881 |
self._savedInfo = canv._doc.info |
|
2666 | 882 |
handled = 0 |
2950 | 883 |
|
884 |
try: |
|
885 |
canv._doctemplate = self |
|
886 |
while len(flowables): |
|
887 |
self.clean_hanging() |
|
888 |
try: |
|
889 |
first = flowables[0] |
|
890 |
self.handle_flowable(flowables) |
|
891 |
handled += 1 |
|
892 |
except: |
|
893 |
#if it has trace info, add it to the traceback message. |
|
894 |
if hasattr(first, '_traceInfo') and first._traceInfo: |
|
895 |
exc = sys.exc_info()[1] |
|
896 |
args = list(exc.args) |
|
897 |
tr = first._traceInfo |
|
898 |
args[0] += '\n(srcFile %s, line %d char %d to line %d char %d)' % ( |
|
899 |
tr.srcFile, |
|
900 |
tr.startLineNo, |
|
901 |
tr.startLinePos, |
|
902 |
tr.endLineNo, |
|
903 |
tr.endLinePos |
|
904 |
) |
|
905 |
exc.args = tuple(args) |
|
906 |
raise |
|
907 |
if self._onProgress: |
|
908 |
self._onProgress('PROGRESS',flowableCount - len(flowables)) |
|
909 |
finally: |
|
910 |
del canv._doctemplate |
|
911 |
||
1505 | 912 |
|
2666 | 913 |
#reapply pagecatcher info |
2950 | 914 |
canv._doc.info = self._savedInfo |
2666 | 915 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
916 |
self._endBuild() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
917 |
if self._onProgress: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
918 |
self._onProgress('FINISHED',0) |
512 | 919 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
920 |
def _allSatisfied(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
921 |
"""Called by multi-build - are all cross-references resolved?""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
922 |
allHappy = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
923 |
for f in self._indexingFlowables: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
924 |
if not f.isSatisfied(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
925 |
allHappy = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
926 |
break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
927 |
return allHappy |
197 | 928 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
929 |
def notify(self, kind, stuff): |
2688
f9c4d1488516
doctemplate: apply T Heller's fix emacs syntax colouring patch
rgbecker
parents:
2672
diff
changeset
|
930 |
"""Forward to any listeners""" |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
931 |
for l in self._indexingFlowables: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
932 |
l.notify(kind, stuff) |
1505 | 933 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
934 |
def pageRef(self, label): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
935 |
"""hook to register a page number""" |
3721 | 936 |
if verbose: print("pageRef called with label '%s' on page %d" % ( |
937 |
label, self.page)) |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
938 |
self._pageRefs[label] = self.page |
1428 | 939 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
940 |
def multiBuild(self, story, |
3175
bf81f5b13ba8
doctemplate.py: fix bogus exception and allow for build keyword args in multibuild
rgbecker
parents:
3161
diff
changeset
|
941 |
maxPasses = 10, |
bf81f5b13ba8
doctemplate.py: fix bogus exception and allow for build keyword args in multibuild
rgbecker
parents:
3161
diff
changeset
|
942 |
**buildKwds |
bf81f5b13ba8
doctemplate.py: fix bogus exception and allow for build keyword args in multibuild
rgbecker
parents:
3161
diff
changeset
|
943 |
): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
944 |
"""Makes multiple passes until all indexing flowables |
3374 | 945 |
are happy. |
946 |
||
947 |
Returns number of passes""" |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
948 |
self._indexingFlowables = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
949 |
#scan the story and keep a copy |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
950 |
for thing in story: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
951 |
if thing.isIndexing(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
952 |
self._indexingFlowables.append(thing) |
1683 | 953 |
|
1829
ce5ceec32eab
Fix up multiBuild to be cleverer about initial saves
rgbecker
parents:
1824
diff
changeset
|
954 |
#better fix for filename is a 'file' problem |
ce5ceec32eab
Fix up multiBuild to be cleverer about initial saves
rgbecker
parents:
1824
diff
changeset
|
955 |
self._doSave = 0 |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
956 |
passes = 0 |
2995
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
957 |
mbe = [] |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
958 |
self._multiBuildEdits = mbe.append |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
959 |
while 1: |
2593
3adaab508968
reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents:
2575
diff
changeset
|
960 |
passes += 1 |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
961 |
if self._onProgress: |
2460
dc28c1738ea9
doctemplate.py: fix buglet reported by Steve Halasz
rgbecker
parents:
2450
diff
changeset
|
962 |
self._onProgress('PASS', passes) |
3884 | 963 |
if verbose: sys.stdout.write('building pass '+str(passes) + '...') |
197 | 964 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
965 |
for fl in self._indexingFlowables: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
966 |
fl.beforeBuild() |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
967 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
968 |
# work with a copy of the story, since it is consumed |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
969 |
tempStory = story[:] |
3175
bf81f5b13ba8
doctemplate.py: fix bogus exception and allow for build keyword args in multibuild
rgbecker
parents:
3161
diff
changeset
|
970 |
self.build(tempStory, **buildKwds) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
971 |
#self.notify('debug',None) |
512 | 972 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
973 |
for fl in self._indexingFlowables: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
974 |
fl.afterBuild() |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
975 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
976 |
happy = self._allSatisfied() |
221 | 977 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
978 |
if happy: |
1829
ce5ceec32eab
Fix up multiBuild to be cleverer about initial saves
rgbecker
parents:
1824
diff
changeset
|
979 |
self._doSave = 0 |
ce5ceec32eab
Fix up multiBuild to be cleverer about initial saves
rgbecker
parents:
1824
diff
changeset
|
980 |
self.canv.save() |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
981 |
break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
982 |
if passes > maxPasses: |
3721 | 983 |
raise IndexError("Index entries not resolved after %d passes" % maxPasses) |
1428 | 984 |
|
2995
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
985 |
#work through any edits |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
986 |
while mbe: |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
987 |
e = mbe.pop(0) |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
988 |
e[0](*e[1:]) |
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
989 |
|
b510fcf46c0b
doctemplate.py: first try at clean up of keepWithNext for multiBuild
rgbecker
parents:
2964
diff
changeset
|
990 |
del self._multiBuildEdits |
3721 | 991 |
if verbose: print('saved') |
3374 | 992 |
return passes |
993 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
994 |
#these are pure virtuals override in derived classes |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
995 |
#NB these get called at suitable places by the base class |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
996 |
#so if you derive and override the handle_xxx methods |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
997 |
#it's up to you to ensure that they maintain the needed consistency |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
998 |
def afterInit(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
999 |
"""This is called after initialisation of the base class.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1000 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
1001 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1002 |
def beforeDocument(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1003 |
"""This is called before any processing is |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1004 |
done on the document.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1005 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
1006 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1007 |
def beforePage(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1008 |
"""This is called at the beginning of page |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1009 |
processing, and immediately before the |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1010 |
beforeDrawPage method of the current page |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1011 |
template.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1012 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
1013 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1014 |
def afterPage(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1015 |
"""This is called after page processing, and |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1016 |
immediately after the afterDrawPage method |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1017 |
of the current page template.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1018 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
1019 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1020 |
def filterFlowables(self,flowables): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1021 |
'''called to filter flowables at the start of the main handle_flowable method. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1022 |
Upon return if flowables[0] has been set to None it is discarded and the main |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1023 |
method returns. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1024 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1025 |
pass |
1428 | 1026 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1027 |
def afterFlowable(self, flowable): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1028 |
'''called after a flowable has been rendered''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1029 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
1030 |
|
2955 | 1031 |
_allowedLifetimes = 'page','frame','build','forever' |
1032 |
def docAssign(self,var,expr,lifetime): |
|
4020 | 1033 |
if not isinstance(expr,strTypes): expr=str(expr) |
2955 | 1034 |
expr=expr.strip() |
1035 |
var=var.strip() |
|
2957 | 1036 |
self.docExec('%s=(%s)'%(var.strip(),expr.strip()),lifetime) |
1037 |
||
1038 |
def docExec(self,stmt,lifetime): |
|
1039 |
stmt=stmt.strip() |
|
1040 |
NS=self._nameSpace |
|
3721 | 1041 |
K0=list(NS.keys()) |
2955 | 1042 |
try: |
1043 |
if lifetime not in self._allowedLifetimes: |
|
1044 |
raise ValueError('bad lifetime %r not in %r'%(lifetime,self._allowedLifetimes)) |
|
3721 | 1045 |
exec(stmt, {},NS) |
2955 | 1046 |
except: |
1047 |
exc = sys.exc_info()[1] |
|
1048 |
args = list(exc.args) |
|
3384 | 1049 |
msg = '\ndocExec %s lifetime=%r failed!' % (stmt,lifetime) |
1050 |
args.append(msg) |
|
2955 | 1051 |
exc.args = tuple(args) |
3721 | 1052 |
for k in NS.keys(): |
2957 | 1053 |
if k not in K0: |
1054 |
del NS[k] |
|
2955 | 1055 |
raise |
3721 | 1056 |
self._addVars([k for k in NS.keys() if k not in K0],lifetime) |
2957 | 1057 |
|
1058 |
def _addVars(self,vars,lifetime): |
|
1059 |
'''add namespace variables to lifetimes lists''' |
|
1060 |
LT=self._lifetimes |
|
1061 |
for var in vars: |
|
3721 | 1062 |
for v in LT.values(): |
2955 | 1063 |
if var in v: |
1064 |
v.remove(var) |
|
2957 | 1065 |
LT.setdefault(lifetime,set([])).add(var) |
2955 | 1066 |
|
2957 | 1067 |
def _removeVars(self,lifetimes): |
1068 |
'''remove namespace variables for with lifetime in lifetimes''' |
|
1069 |
LT=self._lifetimes |
|
1070 |
NS=self._nameSpace |
|
1071 |
for lifetime in lifetimes: |
|
1072 |
for k in LT.setdefault(lifetime,[]): |
|
1073 |
try: |
|
1074 |
del NS[k] |
|
1075 |
except KeyError: |
|
1076 |
pass |
|
1077 |
del LT[lifetime] |
|
2955 | 1078 |
|
1079 |
def docEval(self,expr): |
|
1080 |
try: |
|
1081 |
return eval(expr.strip(),{},self._nameSpace) |
|
1082 |
except: |
|
1083 |
exc = sys.exc_info()[1] |
|
1084 |
args = list(exc.args) |
|
1085 |
args[-1] += '\ndocEval %s failed!' % expr |
|
1086 |
exc.args = tuple(args) |
|
1087 |
raise |
|
1088 |
||
221 | 1089 |
class SimpleDocTemplate(BaseDocTemplate): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1090 |
"""A special case document template that will handle many simple documents. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1091 |
See documentation for BaseDocTemplate. No pageTemplates are required |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1092 |
for this special case. A page templates are inferred from the |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1093 |
margin information and the onFirstPage, onLaterPages arguments to the build method. |
1428 | 1094 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1095 |
A document which has all pages with the same look except for the first |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1096 |
page may can be built using this special approach. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1097 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1098 |
_invalidInitArgs = ('pageTemplates',) |
565 | 1099 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1100 |
def handle_pageBegin(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1101 |
'''override base method to add a change of page template after the firstpage. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1102 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1103 |
self._handle_pageBegin() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1104 |
self._handle_nextPageTemplate('Later') |
221 | 1105 |
|
2488
8bc9673fffa6
doctemplate.py: support for canvasmaker and rotation
rgbecker
parents:
2466
diff
changeset
|
1106 |
def build(self,flowables,onFirstPage=_doNothing, onLaterPages=_doNothing, canvasmaker=canvas.Canvas): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1107 |
"""build the document using the flowables. Annotate the first page using the onFirstPage |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1108 |
function and later pages using the onLaterPages function. The onXXX pages should follow |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1109 |
the signature |
1428 | 1110 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1111 |
def myOnFirstPage(canvas, document): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1112 |
# do annotations and modify the document |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1113 |
... |
1428 | 1114 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1115 |
The functions can do things like draw logos, page numbers, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1116 |
footers, etcetera. They can use external variables to vary |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1117 |
the look (for example providing page numbering or section names). |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1118 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1119 |
self._calc() #in case we changed margins sizes etc |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1120 |
frameT = Frame(self.leftMargin, self.bottomMargin, self.width, self.height, id='normal') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1121 |
self.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=onFirstPage,pagesize=self.pagesize), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1122 |
PageTemplate(id='Later',frames=frameT, onPage=onLaterPages,pagesize=self.pagesize)]) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1123 |
if onFirstPage is _doNothing and hasattr(self,'onFirstPage'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1124 |
self.pageTemplates[0].beforeDrawPage = self.onFirstPage |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1125 |
if onLaterPages is _doNothing and hasattr(self,'onLaterPages'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1126 |
self.pageTemplates[1].beforeDrawPage = self.onLaterPages |
2488
8bc9673fffa6
doctemplate.py: support for canvasmaker and rotation
rgbecker
parents:
2466
diff
changeset
|
1127 |
BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker) |
512 | 1128 |
|
1669 | 1129 |
def progressCB(typ, value): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1130 |
"""Example prototype for progress monitoring. |
1669 | 1131 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1132 |
This aims to provide info about what is going on |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1133 |
during a big job. It should enable, for example, a reasonably |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1134 |
smooth progress bar to be drawn. We design the argument |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1135 |
signature to be predictable and conducive to programming in |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1136 |
other (type safe) languages. If set, this will be called |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1137 |
repeatedly with pairs of values. The first is a string |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1138 |
indicating the type of call; the second is a numeric value. |
1669 | 1139 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1140 |
typ 'STARTING', value = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1141 |
typ 'SIZE_EST', value = numeric estimate of job size |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1142 |
typ 'PASS', value = number of this rendering pass |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1143 |
typ 'PROGRESS', value = number between 0 and SIZE_EST |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1144 |
typ 'PAGE', value = page number of page |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1145 |
type 'FINISHED', value = 0 |
1669 | 1146 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1147 |
The sequence is |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1148 |
STARTING - always called once |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1149 |
SIZE_EST - always called once |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1150 |
PROGRESS - called often |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1151 |
PAGE - called often when page is emitted |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1152 |
FINISHED - called when really, really finished |
1683 | 1153 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1154 |
some juggling is needed to accurately estimate numbers of |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1155 |
pages in pageDrawing mode. |
1669 | 1156 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1157 |
NOTE: the SIZE_EST is a guess. It is possible that the |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1158 |
PROGRESS value may slightly exceed it, or may even step |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1159 |
back a little on rare occasions. The only way to be |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1160 |
really accurate would be to do two passes, and I don't |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1161 |
want to take that performance hit. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1162 |
""" |
3721 | 1163 |
print('PROGRESS MONITOR: %-10s %d' % (typ, value)) |
1669 |