author | rgbecker |
Fri, 22 Aug 2008 10:31:32 +0000 | |
changeset 2956 | 5c17941635bd |
parent 2955 | cc16265295fb |
child 2957 | 137b1d6cdca4 |
permissions | -rw-r--r-- |
2332 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2004 |
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 |
2955 | 38 |
import tokenize |
565 | 39 |
|
197 | 40 |
from types import * |
41 |
import sys |
|
2500 | 42 |
import logging |
43 |
logger = logging.getLogger("reportlab.platypus") |
|
197 | 44 |
|
2192 | 45 |
class LayoutError(Exception): |
46 |
pass |
|
565 | 47 |
|
253 | 48 |
def _doNothing(canvas, doc): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
49 |
"Dummy callback for onPage" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
50 |
pass |
512 | 51 |
|
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
52 |
class PTCycle(list): |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
53 |
def __init__(self): |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
54 |
self._restart = 0 |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
55 |
self._idx = 0 |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
56 |
list.__init__(self) |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
57 |
|
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
58 |
def cyclicIterator(self): |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
59 |
while 1: |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
60 |
yield self[self._idx] |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
61 |
self._idx += 1 |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
62 |
if self._idx>=len(self): |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
63 |
self._idx = self._restart |
565 | 64 |
|
1440
243d35446390
Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents:
1428
diff
changeset
|
65 |
class IndexingFlowable(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
66 |
"""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
|
67 |
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
|
68 |
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
|
69 |
Indexes etc.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
70 |
def isIndexing(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
71 |
return 1 |
512 | 72 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
73 |
def isSatisfied(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
74 |
return 1 |
512 | 75 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
76 |
def notify(self, kind, stuff): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
77 |
"""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
|
78 |
'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
|
79 |
pay attention or not.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
80 |
pass |
512 | 81 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
82 |
def beforeBuild(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
83 |
"""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
|
84 |
old contents""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
85 |
pass |
1428 | 86 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
87 |
def afterBuild(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
88 |
"""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
|
89 |
pass |
253 | 90 |
|
197 | 91 |
class ActionFlowable(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
92 |
'''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
|
93 |
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
|
94 |
use NextPageTemplate which creates an ActionFlowable. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
95 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
96 |
def __init__(self,action=()): |
2575 | 97 |
#must call super init to ensure it has a width and height (of zero), |
98 |
#as in some cases the packer might get called on it... |
|
99 |
Flowable.__init__(self) |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
100 |
if type(action) not in (ListType, TupleType): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
101 |
action = (action,) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
102 |
self.action = tuple(action) |
197 | 103 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
104 |
def apply(self,doc): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
105 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
106 |
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
|
107 |
implement its behaviour |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
108 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
109 |
action = self.action[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
110 |
args = tuple(self.action[1:]) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
111 |
arn = 'handle_'+action |
2762 | 112 |
if arn=="handle_nextPageTemplate" and args[0]=='main': |
113 |
pass |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
114 |
try: |
2762 | 115 |
getattr(doc,arn)(*args) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
116 |
except AttributeError, aerr: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
117 |
if aerr.args[0]==arn: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
118 |
raise NotImplementedError, "Can't handle ActionFlowable(%s)" % action |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
119 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
120 |
raise |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
121 |
except "bogus": |
2098 | 122 |
t, v, unused = sys.exc_info() |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
123 |
raise t, "%s\n handle_%s args=%s"%(v,action,args) |
197 | 124 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
125 |
def __call__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
126 |
return self |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
127 |
|
2192 | 128 |
def identity(self, maxLen=None): |
2531 | 129 |
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
|
130 |
|
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
131 |
class LCActionFlowable(ActionFlowable): |
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
132 |
locChanger = 1 #we cause a frame or page change |
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
133 |
|
2450 | 134 |
def wrap(self, availWidth, availHeight): |
135 |
'''Should never be called.''' |
|
136 |
raise NotImplementedError |
|
137 |
||
138 |
def draw(self): |
|
139 |
'''Should never be called.''' |
|
140 |
raise NotImplementedError |
|
141 |
||
1324 | 142 |
class NextFrameFlowable(ActionFlowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
143 |
def __init__(self,ix,resume=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
144 |
ActionFlowable.__init__(self,('nextFrame',ix,resume)) |
565 | 145 |
|
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
146 |
class CurrentFrameFlowable(LCActionFlowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
147 |
def __init__(self,ix,resume=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
148 |
ActionFlowable.__init__(self,('currentFrame',ix,resume)) |
1324 | 149 |
|
2575 | 150 |
class NullActionFlowable(ActionFlowable): |
151 |
def apply(self): |
|
152 |
pass |
|
153 |
||
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
154 |
class _FrameBreak(LCActionFlowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
155 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
156 |
A special ActionFlowable that allows setting doc._nextFrameIndex |
1324 | 157 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
158 |
eg story.append(FrameBreak('mySpecialFrame')) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
159 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
160 |
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
|
161 |
r = self.__class__(self.action+(resume,)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
162 |
r._ix = ix |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
163 |
return r |
1324 | 164 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
165 |
def apply(self,doc): |
2529
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
166 |
if getattr(self,'_ix',None): |
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
167 |
doc.handle_nextFrame(self._ix) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
168 |
ActionFlowable.apply(self,doc) |
1324 | 169 |
|
170 |
FrameBreak = _FrameBreak('frameEnd') |
|
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
171 |
PageBegin = LCActionFlowable('pageBegin') |
197 | 172 |
|
2366 | 173 |
def _evalMeasurement(n): |
174 |
if type(n) is type(''): |
|
175 |
from paraparser import _num |
|
176 |
n = _num(n) |
|
177 |
if type(n) is type(()): n = n[1] |
|
178 |
return n |
|
179 |
||
2525 | 180 |
class FrameActionFlowable(Flowable): |
2762 | 181 |
_fixedWidth = _fixedHeight = 1 |
2525 | 182 |
def __init__(self,*arg,**kw): |
183 |
raise NotImplementedError('Abstract Class') |
|
184 |
||
185 |
def frameAction(self,frame): |
|
186 |
raise NotImplementedError('Abstract Class') |
|
187 |
||
188 |
class Indenter(FrameActionFlowable): |
|
1923 | 189 |
"""Increases or decreases left and right margins of frame. |
190 |
||
191 |
This allows one to have a 'context-sensitive' indentation |
|
192 |
and makes nested lists way easier. |
|
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2196
diff
changeset
|
193 |
""" |
1923 | 194 |
def __init__(self, left=0, right=0): |
2366 | 195 |
self.left = _evalMeasurement(left) |
196 |
self.right = _evalMeasurement(right) |
|
1923 | 197 |
|
2525 | 198 |
def frameAction(self, frame): |
199 |
frame._leftExtraIndent += self.left |
|
200 |
frame._rightExtraIndent += self.right |
|
512 | 201 |
|
2762 | 202 |
class NotAtTopPageBreak(FrameActionFlowable): |
203 |
def __init__(self): |
|
204 |
pass |
|
205 |
||
206 |
def frameAction(self,frame): |
|
207 |
if not frame._atTop: |
|
2950 | 208 |
frame.add_generated_content(PageBreak()) |
2762 | 209 |
|
197 | 210 |
class NextPageTemplate(ActionFlowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
211 |
"""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
|
212 |
def __init__(self,pt): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
213 |
ActionFlowable.__init__(self,('nextPageTemplate',pt)) |
197 | 214 |
|
215 |
class PageTemplate: |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
216 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
217 |
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
|
218 |
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
|
219 |
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
|
220 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
221 |
def __init__(self,id=None,frames=[],onPage=_doNothing, onPageEnd=_doNothing, |
1926 | 222 |
pagesize=None): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
223 |
if type(frames) not in (ListType,TupleType): frames = [frames] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
224 |
assert filter(lambda x: not isinstance(x,Frame), frames)==[], "frames argument error" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
225 |
self.id = id |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
226 |
self.frames = frames |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
227 |
self.onPage = onPage |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
228 |
self.onPageEnd = onPageEnd |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
229 |
self.pagesize = pagesize |
512 | 230 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
231 |
def beforeDrawPage(self,canv,doc): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
232 |
"""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
|
233 |
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
|
234 |
this page are processed.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
235 |
pass |
197 | 236 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
237 |
def checkPageSize(self,canv,doc): |
2688
f9c4d1488516
doctemplate: apply T Heller's fix emacs syntax colouring patch
rgbecker
parents:
2672
diff
changeset
|
238 |
"""This gets called by the template framework |
1926 | 239 |
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
|
240 |
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
|
241 |
doc size. |
2688
f9c4d1488516
doctemplate: apply T Heller's fix emacs syntax colouring patch
rgbecker
parents:
2672
diff
changeset
|
242 |
""" |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
243 |
#### 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
|
244 |
#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
|
245 |
#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
|
246 |
cp = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
247 |
dp = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
248 |
sp = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
249 |
if canv._pagesize: cp = map(int, canv._pagesize) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
250 |
if self.pagesize: sp = map(int, self.pagesize) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
251 |
if doc.pagesize: dp = map(int, doc.pagesize) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
252 |
if cp!=sp: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
253 |
if sp: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
254 |
canv.setPageSize(self.pagesize) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
255 |
elif cp!=dp: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
256 |
canv.setPageSize(doc.pagesize) |
936 | 257 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
258 |
def afterDrawPage(self, canv, doc): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
259 |
"""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
|
260 |
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
|
261 |
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
|
262 |
this page.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
263 |
pass |
1428 | 264 |
|
512 | 265 |
class BaseDocTemplate: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
266 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
267 |
First attempt at defining a document template class. |
512 | 268 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
269 |
The basic idea is simple. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
270 |
0) The document has a list of data associated with it |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
271 |
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
|
272 |
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
|
273 |
like forcing a page end etc. |
512 | 274 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
275 |
1) The document has one or more page templates. |
512 | 276 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
277 |
2) Each page template has one or more frames. |
512 | 278 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
279 |
3) The document class provides base methods for handling the |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
280 |
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
|
281 |
story flowables into the frames. |
214 | 282 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
283 |
4) The document instances can override the base handler routines. |
1428 | 284 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
285 |
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
|
286 |
but in some advanced usages they may need to be overridden via subclassing. |
1428 | 287 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
288 |
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
|
289 |
since it builds a document using the page template. |
1428 | 290 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
291 |
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
|
292 |
by the filename argument on initialization. |
197 | 293 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
294 |
Possible keyword arguments for the initialization: |
1428 | 295 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
296 |
pageTemplates: A list of templates. Must be nonempty. Names |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
297 |
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
|
298 |
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
|
299 |
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
|
300 |
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
|
301 |
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
|
302 |
using the addPageTemplates method before the document is built. |
2216 | 303 |
pageSize: a 2-tuple or a size constant from reportlab/lib/pagesizes.pu. |
304 |
Used by the SimpleDocTemplate subclass which does NOT accept a list of |
|
305 |
pageTemplates but makes one for you; ignored when using pageTemplates. |
|
306 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
307 |
showBoundary: if set draw a box around the frame boundaries. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
308 |
leftMargin: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
309 |
rightMargin: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
310 |
topMargin: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
311 |
bottomMargin: Margin sizes in points (default 1 inch) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
312 |
These margins may be overridden by the pageTemplates. They are primarily of interest |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
313 |
for the SimpleDocumentTemplate subclass. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
314 |
allowSplitting: If set flowables (eg, paragraphs) may be split across frames or pages |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
315 |
(default: 1) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
316 |
title: Internal title for document (does not automatically display on any page) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
317 |
author: Internal author for document (does not automatically display on any page) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
318 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
319 |
_initArgs = { 'pagesize':defaultPageSize, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
320 |
'pageTemplates':[], |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
321 |
'showBoundary':0, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
322 |
'leftMargin':inch, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
323 |
'rightMargin':inch, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
324 |
'topMargin':inch, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
325 |
'bottomMargin':inch, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
326 |
'allowSplitting':1, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
327 |
'title':None, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
328 |
'author':None, |
2666 | 329 |
'subject':None, |
330 |
'keywords':[], |
|
1839
084e4af662dc
Passing invariant argument through to canvas
andy_robinson
parents:
1838
diff
changeset
|
331 |
'invariant':None, |
2531 | 332 |
'pageCompression':None, |
2492
8142a1737889
doctemplate.py: add rotation argument to BaseDocTemplate
rgbecker
parents:
2491
diff
changeset
|
333 |
'_pageBreakQuick':1, |
2531 | 334 |
'rotation':0, |
335 |
'_debug':0} |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
336 |
_invalidInitArgs = () |
2104 | 337 |
_firstPageTemplateIndex = 0 |
197 | 338 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
339 |
def __init__(self, filename, **kw): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
340 |
"""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
|
341 |
self.filename = filename |
2955 | 342 |
self._nameSpace = dict(doc=self) |
343 |
self._lifetimes = {} |
|
512 | 344 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
345 |
for k in self._initArgs.keys(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
346 |
if not kw.has_key(k): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
347 |
v = self._initArgs[k] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
348 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
349 |
if k in self._invalidInitArgs: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
350 |
raise ValueError, "Invalid argument %s" % k |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
351 |
v = kw[k] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
352 |
setattr(self,k,v) |
310 | 353 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
354 |
p = self.pageTemplates |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
355 |
self.pageTemplates = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
356 |
self.addPageTemplates(p) |
1428 | 357 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
358 |
# 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
|
359 |
# 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
|
360 |
# 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
|
361 |
# passed to indexing flowables. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
362 |
self._pageRefs = {} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
363 |
self._indexingFlowables = [] |
512 | 364 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
365 |
#callback facility for progress monitoring |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
366 |
self._onPage = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
367 |
self._onProgress = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
368 |
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
|
369 |
|
2192 | 370 |
#infinite loop detection if we start doing lots of empty pages |
371 |
self._curPageFlowableCount = 0 |
|
372 |
self._emptyPages = 0 |
|
2193 | 373 |
self._emptyPagesAllowed = 10 |
2192 | 374 |
|
1923 | 375 |
#context sensitive margins - set by story, not from outside |
376 |
self._leftExtraIndent = 0.0 |
|
377 |
self._rightExtraIndent = 0.0 |
|
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2196
diff
changeset
|
378 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
379 |
self._calc() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
380 |
self.afterInit() |
1502 | 381 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
382 |
def _calc(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
383 |
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
|
384 |
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
|
385 |
self.width = self._rightMargin - self.leftMargin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
386 |
self.height = self._topMargin - self.bottomMargin |
1502 | 387 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
388 |
def setPageCallBack(self, func): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
389 |
'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
|
390 |
self._onPage = func |
197 | 391 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
392 |
def setProgressCallBack(self, func): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
393 |
'''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
|
394 |
self._onProgress = func |
1668
448a9205be12
Provision for callback progress monitoring in basic doctemplate class
andy_robinson
parents:
1530
diff
changeset
|
395 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
396 |
def clean_hanging(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
397 |
'handle internal postponed actions' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
398 |
while len(self._hanging): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
399 |
self.handle_flowable(self._hanging) |
1428 | 400 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
401 |
def addPageTemplates(self,pageTemplates): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
402 |
'add one or a sequence of pageTemplates' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
403 |
if type(pageTemplates) not in (ListType,TupleType): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
404 |
pageTemplates = [pageTemplates] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
405 |
#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
|
406 |
#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
|
407 |
for t in pageTemplates: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
408 |
self.pageTemplates.append(t) |
1502 | 409 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
410 |
def handle_documentBegin(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
411 |
'''implement actions at beginning of document''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
412 |
self._hanging = [PageBegin] |
2104 | 413 |
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
|
414 |
self.page = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
415 |
self.beforeDocument() |
253 | 416 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
417 |
def handle_pageBegin(self): |
2688
f9c4d1488516
doctemplate: apply T Heller's fix emacs syntax colouring patch
rgbecker
parents:
2672
diff
changeset
|
418 |
"""Perform actions required at beginning of page. |
f9c4d1488516
doctemplate: apply T Heller's fix emacs syntax colouring patch
rgbecker
parents:
2672
diff
changeset
|
419 |
shouldn't normally be called directly""" |
2593
3adaab508968
reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents:
2575
diff
changeset
|
420 |
self.page += 1 |
2531 | 421 |
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
|
422 |
self.pageTemplate.beforeDrawPage(self.canv,self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
423 |
self.pageTemplate.checkPageSize(self.canv,self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
424 |
self.pageTemplate.onPage(self.canv,self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
425 |
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
|
426 |
self.beforePage() |
2192 | 427 |
#keep a count of flowables added to this page. zero indicates bad stuff |
428 |
self._curPageFlowableCount = 0 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
429 |
if hasattr(self,'_nextFrameIndex'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
430 |
del self._nextFrameIndex |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
431 |
self.frame = self.pageTemplate.frames[0] |
2531 | 432 |
self.frame._debug = self._debug |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
433 |
self.handle_frameBegin() |
197 | 434 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
435 |
def handle_pageEnd(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
436 |
''' show the current page |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
437 |
check the next page template |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
438 |
hang a page begin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
439 |
''' |
2955 | 440 |
self._removeVars('page') |
2192 | 441 |
#detect infinite loops... |
442 |
if self._curPageFlowableCount == 0: |
|
2593
3adaab508968
reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents:
2575
diff
changeset
|
443 |
self._emptyPages += 1 |
2192 | 444 |
else: |
445 |
self._emptyPages = 0 |
|
446 |
if self._emptyPages >= self._emptyPagesAllowed: |
|
2196 | 447 |
if 1: |
2466 | 448 |
ident = "More than %d pages generated without content - halting layout. Likely that a flowable is too large for any frame." % self._emptyPagesAllowed |
449 |
#leave to keep apart from the raise |
|
450 |
raise LayoutError(ident) |
|
2196 | 451 |
else: |
452 |
pass #attempt to restore to good state |
|
453 |
else: |
|
454 |
if self._onProgress: |
|
455 |
self._onProgress('PAGE', self.canv.getPageNumber()) |
|
456 |
self.pageTemplate.afterDrawPage(self.canv, self) |
|
457 |
self.pageTemplate.onPageEnd(self.canv, self) |
|
458 |
self.afterPage() |
|
2531 | 459 |
if self._debug: logger.debug("ending page %d" % self.page) |
2492
8142a1737889
doctemplate.py: add rotation argument to BaseDocTemplate
rgbecker
parents:
2491
diff
changeset
|
460 |
self.canv.setPageRotation(getattr(self.pageTemplate,'rotation',self.rotation)) |
2196 | 461 |
self.canv.showPage() |
2418 | 462 |
|
463 |
if hasattr(self,'_nextPageTemplateCycle'): |
|
464 |
#they are cycling through pages'; we keep the index |
|
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
465 |
self.pageTemplate = self._nextPageTemplateCycle.next() |
2418 | 466 |
elif hasattr(self,'_nextPageTemplateIndex'): |
2196 | 467 |
self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex] |
468 |
del self._nextPageTemplateIndex |
|
469 |
if self._emptyPages==0: |
|
470 |
pass #store good state here |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
471 |
self._hanging.append(PageBegin) |
197 | 472 |
|
2408 | 473 |
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
|
474 |
'''some might choose not to end all the frames''' |
2408 | 475 |
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
|
476 |
self.handle_pageEnd() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
477 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
478 |
n = len(self._hanging) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
479 |
while len(self._hanging)==n: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
480 |
self.handle_frameEnd() |
512 | 481 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
482 |
def handle_frameBegin(self,resume=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
483 |
'''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
|
484 |
f = self.frame |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
485 |
if f._atTop: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
486 |
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
|
487 |
self.frame.drawBoundary(self.canv) |
1923 | 488 |
f._leftExtraIndent = self._leftExtraIndent |
489 |
f._rightExtraIndent = self._rightExtraIndent |
|
197 | 490 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
491 |
def handle_frameEnd(self,resume=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
492 |
''' 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
|
493 |
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
|
494 |
''' |
2955 | 495 |
self._removeVars('frame') |
1923 | 496 |
self._leftExtraIndent = self.frame._leftExtraIndent |
497 |
self._rightExtraIndent = self.frame._rightExtraIndent |
|
498 |
||
2950 | 499 |
f = self.frame |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
500 |
if hasattr(self,'_nextFrameIndex'): |
2529
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
501 |
self.frame = self.pageTemplate.frames[self._nextFrameIndex] |
2531 | 502 |
self.frame._debug = self._debug |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
503 |
del self._nextFrameIndex |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
504 |
self.handle_frameBegin(resume) |
2950 | 505 |
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
|
506 |
self.handle_pageEnd() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
507 |
self.frame = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
508 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
509 |
self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1] |
2531 | 510 |
self.frame._debug = self._debug |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
511 |
self.handle_frameBegin() |
197 | 512 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
513 |
def handle_nextPageTemplate(self,pt): |
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
514 |
'''On endPage change to the page template with name or index pt''' |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
515 |
if type(pt) is StringType: |
2418 | 516 |
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
|
517 |
for t in self.pageTemplates: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
518 |
if t.id == pt: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
519 |
self._nextPageTemplateIndex = self.pageTemplates.index(t) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
520 |
return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
521 |
raise ValueError, "can't find template('%s')"%pt |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
522 |
elif type(pt) is IntType: |
2418 | 523 |
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
|
524 |
self._nextPageTemplateIndex = pt |
2418 | 525 |
elif type(pt) in (ListType, TupleType): |
526 |
#used for alternating left/right pages |
|
527 |
#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
|
528 |
c = PTCycle() |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
529 |
for ptn in pt: |
2418 | 530 |
found = 0 |
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
531 |
if ptn=='*': #special case name used to short circuit the iteration |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
532 |
c._restart = len(c) |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
533 |
continue |
2418 | 534 |
for t in self.pageTemplates: |
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
535 |
if t.id == ptn: |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
536 |
c.append(t) |
2418 | 537 |
found = 1 |
538 |
if not found: |
|
539 |
raise ValueError("Cannot find page template called %s" % templateName) |
|
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
540 |
if not c: |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
541 |
raise ValueError("No valid page templates in cycle") |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
542 |
elif c._restart>len(c): |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
543 |
raise ValueError("Invalid cycle restart position") |
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
544 |
|
2418 | 545 |
#ensure we start on the first one |
2661
e179247ffea3
platypus: add * as shortcircuit template cycle notation
rgbecker
parents:
2654
diff
changeset
|
546 |
self._nextPageTemplateCycle = c.cyclicIterator() |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
547 |
else: |
2950 | 548 |
raise TypeError("argument pt should be string or integer or list") |
197 | 549 |
|
2529
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
550 |
def handle_nextFrame(self,fx,resume=0): |
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
551 |
'''On endFrame change to the frame with name or index fx''' |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
552 |
if type(fx) is StringType: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
553 |
for f in self.pageTemplate.frames: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
554 |
if f.id == fx: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
555 |
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
|
556 |
return |
2950 | 557 |
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])) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
558 |
elif type(fx) is IntType: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
559 |
self._nextFrameIndex = fx |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
560 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
561 |
raise TypeError, "argument fx should be string or integer" |
512 | 562 |
|
2529
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
563 |
def handle_currentFrame(self,fx,resume=0): |
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
564 |
'''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
|
565 |
self.handle_nextFrame(fx,resume) |
dced304f8584
flowables.py: keepInFrame now truncates etc properly, doctemplate.py: fix handle_frameEnd
rgbecker
parents:
2525
diff
changeset
|
566 |
self.handle_frameEnd(resume) |
197 | 567 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
568 |
def handle_breakBefore(self, flowables): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
569 |
'''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
|
570 |
first = flowables[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
571 |
# 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
|
572 |
# 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
|
573 |
# 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
|
574 |
if hasattr(first, '_skipMeNextTime'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
575 |
delattr(first, '_skipMeNextTime') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
576 |
return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
577 |
# 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
|
578 |
# 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
|
579 |
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
|
580 |
first._skipMeNextTime = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
581 |
first.insert(0, PageBreak()) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
582 |
return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
583 |
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
|
584 |
first._skipMeNextTime = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
585 |
flowables.insert(0, PageBreak()) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
586 |
return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
587 |
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
|
588 |
first._skipMeNextTime = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
589 |
flowables.insert(0, FrameBreak()) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
590 |
return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
591 |
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
|
592 |
first._skipMeNextTime = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
593 |
flowables.insert(0, FrameBreak()) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
594 |
return |
1428 | 595 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
596 |
def handle_keepWithNext(self, flowables): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
597 |
"implements keepWithNext" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
598 |
i = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
599 |
n = len(flowables) |
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2418
diff
changeset
|
600 |
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
|
601 |
if i: |
2664 | 602 |
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
|
603 |
K = KeepTogether(flowables[:i]) |
2885
b8a467b98da9
reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents:
2762
diff
changeset
|
604 |
for f in K._content[:-1]: |
b8a467b98da9
reprotlab: fix keepWithnext bug found by Wietse Jacobs
rgbecker
parents:
2762
diff
changeset
|
605 |
f.__dict__['keepWithNext'] = 0 |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
606 |
del flowables[:i] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
607 |
flowables.insert(0,K) |
1425 | 608 |
|
2531 | 609 |
def _fIdent(self,f,maxLen=None,frame=None): |
610 |
if frame: f._frame = frame |
|
611 |
try: |
|
612 |
return f.identity(maxLen) |
|
613 |
finally: |
|
614 |
if frame: del f._frame |
|
615 |
||
2950 | 616 |
def _addGeneratedContent(self,flowables,frame): |
617 |
S = getattr(frame,'_generated_content',None) |
|
618 |
if S: |
|
619 |
for i,f in enumerate(S): |
|
620 |
flowables.insert(i,f) |
|
621 |
del frame._generated_content |
|
622 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
623 |
def handle_flowable(self,flowables): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
624 |
'''try to handle one flowable from the front of list flowables.''' |
1428 | 625 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
626 |
#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
|
627 |
#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
|
628 |
self.filterFlowables(flowables) |
1428 | 629 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
630 |
self.handle_breakBefore(flowables) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
631 |
self.handle_keepWithNext(flowables) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
632 |
f = flowables[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
633 |
del flowables[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
634 |
if f is None: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
635 |
return |
197 | 636 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
637 |
if isinstance(f,PageBreak): |
2408 | 638 |
if isinstance(f,SlowPageBreak): |
639 |
self.handle_pageBreak(slow=1) |
|
640 |
else: |
|
641 |
self.handle_pageBreak() |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
642 |
self.afterFlowable(f) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
643 |
elif isinstance(f,ActionFlowable): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
644 |
f.apply(self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
645 |
self.afterFlowable(f) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
646 |
else: |
2762 | 647 |
frame = self.frame |
2950 | 648 |
canv = self.canv |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
649 |
#try to fit it then draw it |
2950 | 650 |
if frame.add(f, canv, trySplit=self.allowSplitting): |
2762 | 651 |
if not isinstance(f,FrameActionFlowable): |
652 |
self._curPageFlowableCount += 1 |
|
653 |
self.afterFlowable(f) |
|
2950 | 654 |
self._addGeneratedContent(flowables,frame) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
655 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
656 |
if self.allowSplitting: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
657 |
# see if this is a splittable thing |
2950 | 658 |
S = frame.split(f,canv) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
659 |
n = len(S) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
660 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
661 |
n = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
662 |
if n: |
2575 | 663 |
if not isinstance(S[0],(PageBreak,SlowPageBreak,ActionFlowable)): |
2950 | 664 |
if frame.add(S[0], canv, trySplit=0): |
2593
3adaab508968
reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents:
2575
diff
changeset
|
665 |
self._curPageFlowableCount += 1 |
2575 | 666 |
self.afterFlowable(S[0]) |
2950 | 667 |
self._addGeneratedContent(flowables,frame) |
2575 | 668 |
else: |
2950 | 669 |
ident = "Splitting error(n==%d) on page %d in\n%s" % (n,self.page,self._fIdent(f,60,frame)) |
2575 | 670 |
#leave to keep apart from the raise |
671 |
raise LayoutError(ident) |
|
672 |
del S[0] |
|
673 |
for i,f in enumerate(S): |
|
674 |
flowables.insert(i,f) # put split flowables back on the list |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
675 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
676 |
if hasattr(f,'_postponed'): |
2950 | 677 |
ident = "Flowable %s too large on page %d" % (self._fIdent(f,60,frame), self.page) |
2466 | 678 |
#leave to keep apart from the raise |
679 |
raise LayoutError(ident) |
|
2196 | 680 |
# 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
|
681 |
f._postponed = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
682 |
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
|
683 |
self.handle_frameEnd() |
197 | 684 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
685 |
#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
|
686 |
_handle_documentBegin = handle_documentBegin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
687 |
_handle_pageBegin = handle_pageBegin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
688 |
_handle_pageEnd = handle_pageEnd |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
689 |
_handle_frameBegin = handle_frameBegin |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
690 |
_handle_frameEnd = handle_frameEnd |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
691 |
_handle_flowable = handle_flowable |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
692 |
_handle_nextPageTemplate = handle_nextPageTemplate |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
693 |
_handle_currentFrame = handle_currentFrame |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
694 |
_handle_nextFrame = handle_nextFrame |
1505 | 695 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
696 |
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
|
697 |
self._calc() |
2923 | 698 |
|
699 |
#each distinct pass gets a sequencer |
|
700 |
self.seq = reportlab.lib.sequencer.Sequencer() |
|
2955 | 701 |
|
1839
084e4af662dc
Passing invariant argument through to canvas
andy_robinson
parents:
1838
diff
changeset
|
702 |
self.canv = canvasmaker(filename or self.filename, |
084e4af662dc
Passing invariant argument through to canvas
andy_robinson
parents:
1838
diff
changeset
|
703 |
pagesize=self.pagesize, |
2531 | 704 |
invariant=self.invariant, |
705 |
pageCompression=self.pageCompression) |
|
2666 | 706 |
|
707 |
self.canv.setAuthor(self.author) |
|
708 |
self.canv.setTitle(self.title) |
|
709 |
self.canv.setSubject(self.subject) |
|
710 |
self.canv.setKeywords(self.keywords) |
|
2762 | 711 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
712 |
if self._onPage: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
713 |
self.canv.setPageCallBack(self._onPage) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
714 |
self.handle_documentBegin() |
512 | 715 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
716 |
def _endBuild(self): |
2955 | 717 |
self._removeVars('build') |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
718 |
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
|
719 |
del self._hanging[-1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
720 |
self.clean_hanging() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
721 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
722 |
self.clean_hanging() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
723 |
self.handle_pageBreak() |
1505 | 724 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
725 |
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
|
726 |
if self._onPage: self.canv.setPageCallBack(None) |
512 | 727 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
728 |
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
|
729 |
"""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
|
730 |
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
|
731 |
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
|
732 |
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
|
733 |
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
|
734 |
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
|
735 |
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
|
736 |
operations). |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
737 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
738 |
#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
|
739 |
flowableCount = len(flowables) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
740 |
if self._onProgress: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
741 |
self._onProgress('STARTED',0) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
742 |
self._onProgress('SIZE_EST', len(flowables)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
743 |
self._startBuild(filename,canvasmaker) |
512 | 744 |
|
2666 | 745 |
#pagecatcher can drag in information from embedded PDFs and we want ours |
746 |
#to take priority, so cache and reapply our own info dictionary after the build. |
|
2950 | 747 |
canv = self.canv |
748 |
self._savedInfo = canv._doc.info |
|
2666 | 749 |
handled = 0 |
2950 | 750 |
|
751 |
try: |
|
752 |
canv._doctemplate = self |
|
753 |
while len(flowables): |
|
754 |
self.clean_hanging() |
|
755 |
try: |
|
756 |
first = flowables[0] |
|
757 |
self.handle_flowable(flowables) |
|
758 |
handled += 1 |
|
759 |
except: |
|
760 |
#if it has trace info, add it to the traceback message. |
|
761 |
if hasattr(first, '_traceInfo') and first._traceInfo: |
|
762 |
exc = sys.exc_info()[1] |
|
763 |
args = list(exc.args) |
|
764 |
tr = first._traceInfo |
|
765 |
args[0] += '\n(srcFile %s, line %d char %d to line %d char %d)' % ( |
|
766 |
tr.srcFile, |
|
767 |
tr.startLineNo, |
|
768 |
tr.startLinePos, |
|
769 |
tr.endLineNo, |
|
770 |
tr.endLinePos |
|
771 |
) |
|
772 |
exc.args = tuple(args) |
|
773 |
raise |
|
774 |
if self._onProgress: |
|
775 |
self._onProgress('PROGRESS',flowableCount - len(flowables)) |
|
776 |
finally: |
|
777 |
del canv._doctemplate |
|
778 |
||
1505 | 779 |
|
2666 | 780 |
#reapply pagecatcher info |
2950 | 781 |
canv._doc.info = self._savedInfo |
2666 | 782 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
783 |
self._endBuild() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
784 |
if self._onProgress: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
785 |
self._onProgress('FINISHED',0) |
512 | 786 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
787 |
def _allSatisfied(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
788 |
"""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
|
789 |
allHappy = 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
790 |
for f in self._indexingFlowables: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
791 |
if not f.isSatisfied(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
792 |
allHappy = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
793 |
break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
794 |
return allHappy |
197 | 795 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
796 |
def notify(self, kind, stuff): |
2688
f9c4d1488516
doctemplate: apply T Heller's fix emacs syntax colouring patch
rgbecker
parents:
2672
diff
changeset
|
797 |
"""Forward to any listeners""" |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
798 |
for l in self._indexingFlowables: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
799 |
l.notify(kind, stuff) |
1505 | 800 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
801 |
def pageRef(self, label): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
802 |
"""hook to register a page number""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
803 |
if verbose: print "pageRef called with label '%s' on page %d" % ( |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
804 |
label, self.page) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
805 |
self._pageRefs[label] = self.page |
1428 | 806 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
807 |
def multiBuild(self, story, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
808 |
filename=None, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
809 |
canvasmaker=canvas.Canvas, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
810 |
maxPasses = 10): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
811 |
"""Makes multiple passes until all indexing flowables |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
812 |
are happy.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
813 |
self._indexingFlowables = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
814 |
#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
|
815 |
for thing in story: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
816 |
if thing.isIndexing(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
817 |
self._indexingFlowables.append(thing) |
1683 | 818 |
|
1829
ce5ceec32eab
Fix up multiBuild to be cleverer about initial saves
rgbecker
parents:
1824
diff
changeset
|
819 |
#better fix for filename is a 'file' problem |
ce5ceec32eab
Fix up multiBuild to be cleverer about initial saves
rgbecker
parents:
1824
diff
changeset
|
820 |
self._doSave = 0 |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
821 |
passes = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
822 |
while 1: |
2593
3adaab508968
reportlab: minor fixes to platypus, tests and fix jap splitting bug
rgbecker
parents:
2575
diff
changeset
|
823 |
passes += 1 |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
824 |
if self._onProgress: |
2460
dc28c1738ea9
doctemplate.py: fix buglet reported by Steve Halasz
rgbecker
parents:
2450
diff
changeset
|
825 |
self._onProgress('PASS', passes) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
826 |
if verbose: print 'building pass '+str(passes) + '...', |
197 | 827 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
828 |
for fl in self._indexingFlowables: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
829 |
fl.beforeBuild() |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
830 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
831 |
# 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
|
832 |
tempStory = story[:] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
833 |
self.build(tempStory, filename, canvasmaker) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
834 |
#self.notify('debug',None) |
512 | 835 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
836 |
#clean up so multi-build does not go wrong - the frame |
2196 | 837 |
#packer might have tacked an attribute onto some flowables |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
838 |
for elem in story: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
839 |
if hasattr(elem, '_postponed'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
840 |
del elem._postponed |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
841 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
842 |
for fl in self._indexingFlowables: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
843 |
fl.afterBuild() |
218
274db2129c04
Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents:
214
diff
changeset
|
844 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
845 |
happy = self._allSatisfied() |
221 | 846 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
847 |
if happy: |
1829
ce5ceec32eab
Fix up multiBuild to be cleverer about initial saves
rgbecker
parents:
1824
diff
changeset
|
848 |
self._doSave = 0 |
ce5ceec32eab
Fix up multiBuild to be cleverer about initial saves
rgbecker
parents:
1824
diff
changeset
|
849 |
self.canv.save() |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
850 |
break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
851 |
if passes > maxPasses: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
852 |
raise IndexError, "Index entries not resolved after %d passes" % maxPasses |
1428 | 853 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
854 |
if verbose: print 'saved' |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
855 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
856 |
#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
|
857 |
#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
|
858 |
#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
|
859 |
#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
|
860 |
def afterInit(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
861 |
"""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
|
862 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
863 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
864 |
def beforeDocument(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
865 |
"""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
|
866 |
done on the document.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
867 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
868 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
869 |
def beforePage(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
870 |
"""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
|
871 |
processing, and immediately before the |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
872 |
beforeDrawPage method of the current page |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
873 |
template.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
874 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
875 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
876 |
def afterPage(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
877 |
"""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
|
878 |
immediately after the afterDrawPage method |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
879 |
of the current page template.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
880 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
881 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
882 |
def filterFlowables(self,flowables): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
883 |
'''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
|
884 |
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
|
885 |
method returns. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
886 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
887 |
pass |
1428 | 888 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
889 |
def afterFlowable(self, flowable): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
890 |
'''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
|
891 |
pass |
284
eabeb5f4e851
Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents:
279
diff
changeset
|
892 |
|
2955 | 893 |
_allowedLifetimes = 'page','frame','build','forever' |
894 |
def docAssign(self,var,expr,lifetime): |
|
895 |
var=var.strip()+'\n' |
|
2956 | 896 |
if not isinstance(expr,(str,unicode)): expr=str(expr) |
2955 | 897 |
expr=expr.strip() |
898 |
T=tokenize.generate_tokens(lambda :var) |
|
899 |
tokens=[] |
|
900 |
while 1: |
|
901 |
t=T.next() |
|
902 |
if t[0]==tokenize.NEWLINE: break |
|
903 |
tokens.append(t) |
|
904 |
simple = len(tokens)==1 |
|
905 |
del T |
|
906 |
var=var.strip() |
|
907 |
try: |
|
908 |
if lifetime not in self._allowedLifetimes: |
|
909 |
raise ValueError('bad lifetime %r not in %r'%(lifetime,self._allowedLifetimes)) |
|
910 |
exec '%s=(%s)' % (var,expr) in {},self._nameSpace |
|
911 |
except: |
|
912 |
exc = sys.exc_info()[1] |
|
913 |
args = list(exc.args) |
|
914 |
args[-1] += '\ndocAssign %s=(%s) lifetime=%r failed!' % (var,expr,lifetime) |
|
915 |
exc.args = tuple(args) |
|
916 |
raise |
|
917 |
if simple: |
|
918 |
for v in self._lifetimes.itervalues(): |
|
919 |
if var in v: |
|
920 |
v.remove(var) |
|
921 |
self._lifetimes.setdefault(lifetime,set([])).add(var) |
|
922 |
||
923 |
def docExec(self,stmt): |
|
924 |
stmt=stmt.strip() |
|
925 |
try: |
|
926 |
exec stmt in {},self._nameSpace |
|
927 |
except: |
|
928 |
exc = sys.exc_info()[1] |
|
929 |
args = list(exc.args) |
|
930 |
args[-1] += '\ndocExec %s failed!' % stmt |
|
931 |
exc.args = tuple(args) |
|
932 |
raise |
|
933 |
||
934 |
def _removeVars(self,lifetime): |
|
935 |
for k in self._lifetimes.setdefault(lifetime,[]): |
|
936 |
try: |
|
937 |
del self._nameSpace[k] |
|
938 |
except KeyError: |
|
939 |
pass |
|
940 |
del self._lifetimes[lifetime] |
|
941 |
||
942 |
def docEval(self,expr): |
|
943 |
try: |
|
944 |
return eval(expr.strip(),{},self._nameSpace) |
|
945 |
except: |
|
946 |
exc = sys.exc_info()[1] |
|
947 |
args = list(exc.args) |
|
948 |
args[-1] += '\ndocEval %s failed!' % expr |
|
949 |
exc.args = tuple(args) |
|
950 |
raise |
|
951 |
||
221 | 952 |
class SimpleDocTemplate(BaseDocTemplate): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
953 |
"""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
|
954 |
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
|
955 |
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
|
956 |
margin information and the onFirstPage, onLaterPages arguments to the build method. |
1428 | 957 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
958 |
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
|
959 |
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
|
960 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
961 |
_invalidInitArgs = ('pageTemplates',) |
565 | 962 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
963 |
def handle_pageBegin(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
964 |
'''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
|
965 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
966 |
self._handle_pageBegin() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
967 |
self._handle_nextPageTemplate('Later') |
221 | 968 |
|
2488
8bc9673fffa6
doctemplate.py: support for canvasmaker and rotation
rgbecker
parents:
2466
diff
changeset
|
969 |
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
|
970 |
"""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
|
971 |
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
|
972 |
the signature |
1428 | 973 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
974 |
def myOnFirstPage(canvas, document): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
975 |
# do annotations and modify the document |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
976 |
... |
1428 | 977 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
978 |
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
|
979 |
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
|
980 |
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
|
981 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
982 |
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
|
983 |
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
|
984 |
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
|
985 |
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
|
986 |
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
|
987 |
self.pageTemplates[0].beforeDrawPage = self.onFirstPage |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
988 |
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
|
989 |
self.pageTemplates[1].beforeDrawPage = self.onLaterPages |
2488
8bc9673fffa6
doctemplate.py: support for canvasmaker and rotation
rgbecker
parents:
2466
diff
changeset
|
990 |
BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker) |
512 | 991 |
|
1669 | 992 |
def progressCB(typ, value): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
993 |
"""Example prototype for progress monitoring. |
1669 | 994 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
995 |
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
|
996 |
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
|
997 |
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
|
998 |
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
|
999 |
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
|
1000 |
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
|
1001 |
indicating the type of call; the second is a numeric value. |
1669 | 1002 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1003 |
typ 'STARTING', value = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1004 |
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
|
1005 |
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
|
1006 |
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
|
1007 |
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
|
1008 |
type 'FINISHED', value = 0 |
1669 | 1009 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1010 |
The sequence is |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1011 |
STARTING - always called once |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1012 |
SIZE_EST - always called once |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1013 |
PROGRESS - called often |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1014 |
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
|
1015 |
FINISHED - called when really, really finished |
1683 | 1016 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1017 |
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
|
1018 |
pages in pageDrawing mode. |
1669 | 1019 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1020 |
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
|
1021 |
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
|
1022 |
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
|
1023 |
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
|
1024 |
want to take that performance hit. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1025 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1026 |
print 'PROGRESS MONITOR: %-10s %d' % (typ, value) |
1669 | 1027 |
|
221 | 1028 |
if __name__ == '__main__': |
1029 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1030 |
def myFirstPage(canvas, doc): |
2654 | 1031 |
from reportlab.lib.colors import red |
1032 |
PAGE_HEIGHT = canvas._pagesize[1] |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1033 |
canvas.saveState() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1034 |
canvas.setStrokeColor(red) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1035 |
canvas.setLineWidth(5) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1036 |
canvas.line(66,72,66,PAGE_HEIGHT-72) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1037 |
canvas.setFont('Times-Bold',24) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1038 |
canvas.drawString(108, PAGE_HEIGHT-108, "TABLE OF CONTENTS DEMO") |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1039 |
canvas.setFont('Times-Roman',12) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1040 |
canvas.drawString(4 * inch, 0.75 * inch, "First Page") |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1041 |
canvas.restoreState() |
221 | 1042 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1043 |
def myLaterPages(canvas, doc): |
2654 | 1044 |
from reportlab.lib.colors import red |
1045 |
PAGE_HEIGHT = canvas._pagesize[1] |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1046 |
canvas.saveState() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1047 |
canvas.setStrokeColor(red) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1048 |
canvas.setLineWidth(5) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1049 |
canvas.line(66,72,66,PAGE_HEIGHT-72) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1050 |
canvas.setFont('Times-Roman',12) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1051 |
canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1052 |
canvas.restoreState() |
221 | 1053 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1054 |
def run(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1055 |
objects_to_draw = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1056 |
from reportlab.lib.styles import ParagraphStyle |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1057 |
#from paragraph import Paragraph |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1058 |
from doctemplate import SimpleDocTemplate |
221 | 1059 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1060 |
#need a style |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1061 |
normal = ParagraphStyle('normal') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1062 |
normal.firstLineIndent = 18 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1063 |
normal.spaceBefore = 6 |
2083 | 1064 |
from reportlab.lib.randomtext import randomText |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1065 |
import random |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1066 |
for i in range(15): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1067 |
height = 0.5 + (2*random.random()) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1068 |
box = XBox(6 * inch, height * inch, 'Box Number %d' % i) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1069 |
objects_to_draw.append(box) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1070 |
para = Paragraph(randomText(), normal) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1071 |
objects_to_draw.append(para) |
221 | 1072 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1073 |
SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1074 |
onFirstPage=myFirstPage,onLaterPages=myLaterPages) |
221 | 1075 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1672
diff
changeset
|
1076 |
run() |