author | andy |
Wed, 12 May 2010 14:49:43 +0000 | |
changeset 3374 | 348f9bcb4c11 |
parent 3326 | ce725978d11c |
child 3468 | 4a75ba27637d |
permissions | -rw-r--r-- |
2967
ea62529bd1df
reportlab-2.2: first stage changes in on the trunk
rgbecker
parents:
2966
diff
changeset
|
1 |
#Copyright ReportLab Europe Ltd. 2000-2008 |
2963 | 2 |
#see license.txt for license details |
3 |
"""Tests for the Platypus TableOfContents class. |
|
4 |
||
5 |
Currently there is only one such test. Most such tests, like this |
|
6 |
one, will be generating a PDF document that needs to be eye-balled |
|
7 |
in order to find out if it is 'correct'. |
|
8 |
""" |
|
2984 | 9 |
__version__='''$Id$''' |
10 |
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation |
|
11 |
setOutDir(__name__) |
|
2963 | 12 |
import sys, os |
13 |
from os.path import join, basename, splitext |
|
14 |
from math import sqrt |
|
3374 | 15 |
import random |
2966 | 16 |
import unittest |
2963 | 17 |
from reportlab.lib.units import inch, cm |
18 |
from reportlab.lib.pagesizes import A4 |
|
19 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle |
|
20 |
from reportlab.platypus.paragraph import Paragraph |
|
21 |
from reportlab.platypus.xpreformatted import XPreformatted |
|
22 |
from reportlab.platypus.frames import Frame |
|
23 |
from reportlab.platypus.doctemplate \ |
|
24 |
import PageTemplate, BaseDocTemplate |
|
3374 | 25 |
from reportlab.platypus import tableofcontents, PageBreak |
2963 | 26 |
from reportlab.lib import randomtext |
27 |
||
28 |
||
29 |
def myMainPageFrame(canvas, doc): |
|
30 |
"The page frame used for all PDF documents." |
|
31 |
||
32 |
canvas.saveState() |
|
33 |
||
34 |
canvas.rect(2.5*cm, 2.5*cm, 15*cm, 25*cm) |
|
35 |
canvas.setFont('Times-Roman', 12) |
|
36 |
pageNumber = canvas.getPageNumber() |
|
37 |
canvas.drawString(10*cm, cm, str(pageNumber)) |
|
38 |
||
39 |
canvas.restoreState() |
|
40 |
||
41 |
||
42 |
class MyDocTemplate(BaseDocTemplate): |
|
43 |
"The document template used for all PDF documents." |
|
44 |
||
45 |
_invalidInitArgs = ('pageTemplates',) |
|
46 |
||
47 |
def __init__(self, filename, **kw): |
|
48 |
frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1') |
|
49 |
self.allowSplitting = 0 |
|
3326 | 50 |
BaseDocTemplate.__init__(self, filename, **kw) |
2963 | 51 |
template = PageTemplate('normal', [frame1], myMainPageFrame) |
52 |
self.addPageTemplates(template) |
|
53 |
||
54 |
||
55 |
def afterFlowable(self, flowable): |
|
3060
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
56 |
"Registers TOC entries." |
2963 | 57 |
|
58 |
if flowable.__class__.__name__ == 'Paragraph': |
|
59 |
styleName = flowable.style.name |
|
60 |
if styleName[:7] == 'Heading': |
|
3060
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
61 |
key = str(hash(flowable)) |
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
62 |
self.canv.bookmarkPage(key) |
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
63 |
|
2963 | 64 |
# Register TOC entries. |
65 |
level = int(styleName[7:]) |
|
66 |
text = flowable.getPlainText() |
|
67 |
pageNum = self.page |
|
3060
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
68 |
# Try calling this with and without a key to test both |
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
69 |
# Entries of every second level will have links, others won't |
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
70 |
if level % 2 == 1: |
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
71 |
self.notify('TOCEntry', (level, text, pageNum, key)) |
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
72 |
else: |
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
73 |
self.notify('TOCEntry', (level, text, pageNum)) |
2963 | 74 |
|
75 |
||
76 |
def makeHeaderStyle(level, fontName='Times-Roman'): |
|
77 |
"Make a header style for different levels." |
|
78 |
||
79 |
assert level >= 0, "Level must be >= 0." |
|
80 |
||
81 |
PS = ParagraphStyle |
|
82 |
size = 24.0 / sqrt(1+level) |
|
83 |
style = PS(name = 'Heading' + str(level), |
|
84 |
fontName = fontName, |
|
85 |
fontSize = size, |
|
86 |
leading = size*1.2, |
|
87 |
spaceBefore = size/4.0, |
|
88 |
spaceAfter = size/8.0) |
|
89 |
||
90 |
return style |
|
91 |
||
92 |
||
93 |
def makeBodyStyle(): |
|
94 |
"Body text style - the default will do" |
|
95 |
return ParagraphStyle('body') |
|
96 |
||
97 |
||
98 |
def makeTocHeaderStyle(level, delta, epsilon, fontName='Times-Roman'): |
|
99 |
"Make a header style for different levels." |
|
100 |
||
101 |
assert level >= 0, "Level must be >= 0." |
|
102 |
||
103 |
PS = ParagraphStyle |
|
104 |
size = 12 |
|
105 |
style = PS(name = 'Heading' + str(level), |
|
106 |
fontName = fontName, |
|
107 |
fontSize = size, |
|
108 |
leading = size*1.2, |
|
109 |
spaceBefore = size/4.0, |
|
110 |
spaceAfter = size/8.0, |
|
111 |
firstLineIndent = -epsilon, |
|
112 |
leftIndent = level*delta + epsilon) |
|
113 |
||
114 |
return style |
|
115 |
||
116 |
||
117 |
class TocTestCase(unittest.TestCase): |
|
118 |
"Test TableOfContents class (eyeball-test)." |
|
119 |
||
120 |
def test0(self): |
|
121 |
"""Test story with TOC and a cascaded header hierarchy. |
|
122 |
||
123 |
The story should contain exactly one table of contents that is |
|
124 |
immediatly followed by a list of of cascaded levels of header |
|
125 |
lines, each nested one level deeper than the previous one. |
|
126 |
||
127 |
Features to be visually confirmed by a human being are: |
|
128 |
||
129 |
1. TOC lines are indented in multiples of 1 cm. |
|
130 |
2. Wrapped TOC lines continue with additional 0.5 cm indentation. |
|
3060
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
131 |
3. Only entries of every second level has links |
eeedb611fa67
Added feature to have clickable TableOfContents entries (ToDo 978).
jonas
parents:
3059
diff
changeset
|
132 |
... |
2963 | 133 |
""" |
134 |
||
135 |
maxLevels = 12 |
|
136 |
||
137 |
# Create styles to be used for document headers |
|
138 |
# on differnet levels. |
|
139 |
headerLevelStyles = [] |
|
140 |
for i in range(maxLevels): |
|
141 |
headerLevelStyles.append(makeHeaderStyle(i)) |
|
142 |
||
143 |
# Create styles to be used for TOC entry lines |
|
144 |
# for headers on differnet levels. |
|
145 |
tocLevelStyles = [] |
|
146 |
d, e = tableofcontents.delta, tableofcontents.epsilon |
|
147 |
for i in range(maxLevels): |
|
148 |
tocLevelStyles.append(makeTocHeaderStyle(i, d, e)) |
|
149 |
||
150 |
# Build story. |
|
151 |
story = [] |
|
152 |
styleSheet = getSampleStyleSheet() |
|
153 |
bt = styleSheet['BodyText'] |
|
154 |
||
155 |
description = '<font color=red>%s</font>' % self.test0.__doc__ |
|
156 |
story.append(XPreformatted(description, bt)) |
|
157 |
||
3059
3a6ff201e927
Added documentation for the table of contents to the userguide.
jonas
parents:
2984
diff
changeset
|
158 |
toc = tableofcontents.TableOfContents() |
2963 | 159 |
toc.levelStyles = tocLevelStyles |
160 |
story.append(toc) |
|
161 |
||
162 |
for i in range(maxLevels): |
|
163 |
story.append(Paragraph('HEADER, LEVEL %d' % i, |
|
164 |
headerLevelStyles[i])) |
|
165 |
#now put some body stuff in. |
|
166 |
txt = randomtext.randomText(randomtext.PYTHON, 5) |
|
167 |
para = Paragraph(txt, makeBodyStyle()) |
|
168 |
story.append(para) |
|
169 |
||
170 |
path = outputfile('test_platypus_toc.pdf') |
|
171 |
doc = MyDocTemplate(path) |
|
172 |
doc.multiBuild(story) |
|
173 |
||
174 |
||
3374 | 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 |
||
2963 | 280 |
def makeSuite(): |
281 |
return makeSuiteForClasses(TocTestCase) |
|
282 |
||
283 |
||
284 |
#noruntests |
|
285 |
if __name__ == "__main__": |
|
286 |
unittest.TextTestRunner().run(makeSuite()) |
|
287 |
printLocation() |