10 from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation |
10 from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation |
11 setOutDir(__name__) |
11 setOutDir(__name__) |
12 import sys, os |
12 import sys, os |
13 from os.path import join, basename, splitext |
13 from os.path import join, basename, splitext |
14 from math import sqrt |
14 from math import sqrt |
|
15 import random |
15 import unittest |
16 import unittest |
16 from reportlab.lib.units import inch, cm |
17 from reportlab.lib.units import inch, cm |
17 from reportlab.lib.pagesizes import A4 |
18 from reportlab.lib.pagesizes import A4 |
18 from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle |
19 from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle |
19 from reportlab.platypus.paragraph import Paragraph |
20 from reportlab.platypus.paragraph import Paragraph |
20 from reportlab.platypus.xpreformatted import XPreformatted |
21 from reportlab.platypus.xpreformatted import XPreformatted |
21 from reportlab.platypus.frames import Frame |
22 from reportlab.platypus.frames import Frame |
22 from reportlab.platypus.doctemplate \ |
23 from reportlab.platypus.doctemplate \ |
23 import PageTemplate, BaseDocTemplate |
24 import PageTemplate, BaseDocTemplate |
24 from reportlab.platypus import tableofcontents |
25 from reportlab.platypus import tableofcontents, PageBreak |
25 from reportlab.lib import randomtext |
26 from reportlab.lib import randomtext |
26 |
27 |
27 |
28 |
28 def myMainPageFrame(canvas, doc): |
29 def myMainPageFrame(canvas, doc): |
29 "The page frame used for all PDF documents." |
30 "The page frame used for all PDF documents." |
169 path = outputfile('test_platypus_toc.pdf') |
170 path = outputfile('test_platypus_toc.pdf') |
170 doc = MyDocTemplate(path) |
171 doc = MyDocTemplate(path) |
171 doc.multiBuild(story) |
172 doc.multiBuild(story) |
172 |
173 |
173 |
174 |
|
175 |
|
176 def test1(self): |
|
177 """This shows a table which would take more than one page, |
|
178 and need multiple passes to render. But we preload it |
|
179 with the right headings to make it go faster. We used |
|
180 a simple 100-chapter document with one level. |
|
181 """ |
|
182 |
|
183 chapters = 30 #goes over one page |
|
184 |
|
185 headerStyle = makeHeaderStyle(0) |
|
186 d, e = tableofcontents.delta, tableofcontents.epsilon |
|
187 tocLevelStyle = makeTocHeaderStyle(0, d, e) |
|
188 |
|
189 # Build most of the story; we'll re-use it to |
|
190 # make documents with different numbers of passes. |
|
191 |
|
192 story = [] |
|
193 styleSheet = getSampleStyleSheet() |
|
194 bt = styleSheet['BodyText'] |
|
195 |
|
196 description = '<font color=red>%s</font>' % self.test1.__doc__ |
|
197 story.append(XPreformatted(description, bt)) |
|
198 |
|
199 for i in range(chapters): |
|
200 story.append(PageBreak()) |
|
201 story.append(Paragraph('This is chapter %d' % (i+1), |
|
202 headerStyle)) |
|
203 #now put some lengthy body stuff in. |
|
204 for paras in range(random.randint(1,3)): |
|
205 txt = randomtext.randomText(randomtext.PYTHON, 5) |
|
206 para = Paragraph(txt, makeBodyStyle()) |
|
207 story.append(para) |
|
208 |
|
209 |
|
210 #try 1: empty TOC, 3 passes |
|
211 |
|
212 toc = tableofcontents.TableOfContents() |
|
213 toc.levelStyles = [tocLevelStyle] #only need one |
|
214 story1 = [toc] + story |
|
215 |
|
216 |
|
217 path = outputfile('test_platypus_toc_preload.pdf') |
|
218 doc = MyDocTemplate(path) |
|
219 passes = doc.multiBuild(story1) |
|
220 self.assertEquals(passes, 3) |
|
221 |
|
222 #try 2: now preload the TOC with the entries |
|
223 |
|
224 toc = tableofcontents.TableOfContents() |
|
225 toc.levelStyles = [tocLevelStyle] #only need one |
|
226 tocEntries = [] |
|
227 for i in range(chapters): |
|
228 #add tuple of (level, text, pageNum, key) |
|
229 #with an initial guess of pageNum=0 |
|
230 tocEntries.append((0, 'This is chapter %d' % (i+1), 0, None)) |
|
231 toc.addEntries(tocEntries) |
|
232 |
|
233 story2 = [toc] + story |
|
234 |
|
235 |
|
236 path = outputfile('test_platypus_toc_preload.pdf') |
|
237 doc = MyDocTemplate(path) |
|
238 passes = doc.multiBuild(story2) |
|
239 self.assertEquals(passes, 2) |
|
240 |
|
241 |
|
242 |
|
243 #try 3: preload again but try to be really smart and work out |
|
244 #in advance what page everything starts on. We cannot |
|
245 #use a random story for this. |
|
246 |
|
247 |
|
248 toc3 = tableofcontents.TableOfContents() |
|
249 toc3.levelStyles = [tocLevelStyle] #only need one |
|
250 tocEntries = [] |
|
251 for i in range(chapters): |
|
252 #add tuple of (level, text, pageNum, key) |
|
253 #with an initial guess of pageNum= 3 |
|
254 tocEntries.append((0, 'This is chapter %d' % i, 2+i, None)) |
|
255 toc3.addEntries(tocEntries) |
|
256 |
|
257 story3 = [toc3] |
|
258 for i in range(chapters): |
|
259 story3.append(PageBreak()) |
|
260 story3.append(Paragraph('This is chapter %d' % (i+1), |
|
261 headerStyle)) |
|
262 txt = """ |
|
263 The paragraphs in this are not at all random, because |
|
264 we need to be absolutely, totally certain they will fit |
|
265 on one page. Each chapter will be one page long. |
|
266 """ |
|
267 para = Paragraph(txt, makeBodyStyle()) |
|
268 story3.append(para) |
|
269 |
|
270 |
|
271 path = outputfile('test_platypus_toc_preload.pdf') |
|
272 doc = MyDocTemplate(path) |
|
273 passes = doc.multiBuild(story3) |
|
274 |
|
275 # I can't get one pass yet' |
|
276 #self.assertEquals(passes, 1) |
|
277 |
|
278 |
|
279 |
174 def makeSuite(): |
280 def makeSuite(): |
175 return makeSuiteForClasses(TocTestCase) |
281 return makeSuiteForClasses(TocTestCase) |
176 |
282 |
177 |
283 |
178 #noruntests |
284 #noruntests |