author | robin <robin@reportlab.com> |
Tue, 07 Mar 2017 10:00:34 +0000 | |
changeset 4330 | 617ffa6bbdc8 |
parent 4252 | fe660f227cac |
child 4630 | 604952d66599 |
permissions | -rw-r--r-- |
4330 | 1 |
#Copyright ReportLab Europe Ltd. 2000-2017 |
2963 | 2 |
#see license.txt for license details |
3 |
"""Tests pleaseTurnOver, pageBreakBefore, frameBreakBefore, keepWithNext... |
|
4 |
""" |
|
4252 | 5 |
__version__='3.3.0' |
2984 | 6 |
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation |
7 |
setOutDir(__name__) |
|
2963 | 8 |
import sys |
2966 | 9 |
import unittest |
2963 | 10 |
from reportlab.platypus.flowables import Flowable, PTOContainer, KeepInFrame |
3706
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
11 |
from reportlab.platypus import FrameBG |
2963 | 12 |
from reportlab.lib.units import cm |
13 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle |
|
14 |
from reportlab.lib.colors import toColor, black |
|
15 |
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY |
|
16 |
from reportlab.platypus.paragraph import Paragraph |
|
17 |
from reportlab.platypus.tables import Table |
|
18 |
from reportlab.platypus.frames import Frame |
|
19 |
from reportlab.lib.randomtext import randomText |
|
20 |
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate, FrameBreak |
|
21 |
||
22 |
def myMainPageFrame(canvas, doc): |
|
23 |
"The page frame used for all PDF documents." |
|
24 |
||
25 |
canvas.saveState() |
|
26 |
canvas.setFont('Times-Roman', 12) |
|
27 |
pageNumber = canvas.getPageNumber() |
|
28 |
canvas.drawString(10*cm, cm, str(pageNumber)) |
|
29 |
canvas.restoreState() |
|
30 |
||
31 |
def _showDoc(fn,story): |
|
32 |
pageTemplate = PageTemplate('normal', [Frame(72, 440, 170, 284, id='F1'), |
|
33 |
Frame(326, 440, 170, 284, id='F2'), |
|
34 |
Frame(72, 72, 170, 284, id='F3'), |
|
35 |
Frame(326, 72, 170, 284, id='F4'), |
|
36 |
], myMainPageFrame) |
|
37 |
doc = BaseDocTemplate(outputfile(fn), |
|
38 |
pageTemplates = pageTemplate, |
|
39 |
showBoundary = 1, |
|
40 |
) |
|
41 |
doc.multiBuild(story) |
|
42 |
||
43 |
text2 ='''We have already seen that the natural general principle that will |
|
44 |
subsume this case cannot be arbitrary in the requirement that branching |
|
45 |
is not tolerated within the dominance scope of a complex symbol. |
|
46 |
Notice, incidentally, that the speaker-hearer's linguistic intuition is |
|
47 |
to be regarded as the strong generative capacity of the theory. A |
|
48 |
consequence of the approach just outlined is that the descriptive power |
|
49 |
of the base component does not affect the structure of the levels of |
|
50 |
acceptability from fairly high (e.g. (99a)) to virtual gibberish (e.g. |
|
51 |
(98d)). By combining adjunctions and certain deformations, a |
|
52 |
descriptively adequate grammar cannot be arbitrary in the strong |
|
53 |
generative capacity of the theory.''' |
|
54 |
||
55 |
text1=''' |
|
56 |
On our assumptions, a descriptively adequate grammar delimits the strong |
|
57 |
generative capacity of the theory. For one thing, the fundamental error |
|
58 |
of regarding functional notions as categorial is to be regarded as a |
|
59 |
corpus of utterance tokens upon which conformity has been defined by the |
|
60 |
paired utterance test. A majority of informed linguistic specialists |
|
61 |
agree that the appearance of parasitic gaps in domains relatively |
|
62 |
inaccessible to ordinary extraction is necessary to impose an |
|
63 |
interpretation on the requirement that branching is not tolerated within |
|
64 |
the dominance scope of a complex symbol. It may be, then, that the |
|
65 |
speaker-hearer's linguistic intuition appears to correlate rather |
|
66 |
closely with the ultimate standard that determines the accuracy of any |
|
67 |
proposed grammar. Analogously, the notion of level of grammaticalness |
|
68 |
may remedy and, at the same time, eliminate a general convention |
|
69 |
regarding the forms of the grammar.''' |
|
70 |
||
71 |
text0 = '''To characterize a linguistic level L, |
|
72 |
this selectionally introduced contextual |
|
73 |
feature delimits the requirement that |
|
74 |
branching is not tolerated within the |
|
75 |
dominance scope of a complex |
|
76 |
symbol. Notice, incidentally, that the |
|
77 |
notion of level of grammaticalness |
|
78 |
does not affect the structure of the |
|
79 |
levels of acceptability from fairly high |
|
80 |
(e.g. (99a)) to virtual gibberish (e.g. |
|
81 |
(98d)). Suppose, for instance, that a |
|
82 |
subset of English sentences interesting |
|
83 |
on quite independent grounds appears |
|
84 |
to correlate rather closely with an |
|
85 |
important distinction in language use. |
|
86 |
Presumably, this analysis of a |
|
87 |
formative as a pair of sets of features is |
|
88 |
not quite equivalent to the system of |
|
89 |
base rules exclusive of the lexicon. We |
|
90 |
have already seen that the appearance |
|
91 |
of parasitic gaps in domains relatively |
|
92 |
inaccessible to ordinary extraction |
|
93 |
does not readily tolerate the strong |
|
94 |
generative capacity of the theory.''' |
|
95 |
||
96 |
def _ptoTestCase(self): |
|
97 |
"""PTO stands for Please Turn Over and is a means for |
|
98 |
specifying content to be inserted when stuff goes over a page. |
|
99 |
This makes one long multi-page paragraph.""" |
|
100 |
||
101 |
# Build story. |
|
102 |
story = [] |
|
103 |
def fbreak(story=story): |
|
104 |
story.append(FrameBreak()) |
|
105 |
||
106 |
styleSheet = getSampleStyleSheet() |
|
107 |
H1 = styleSheet['Heading1'] |
|
108 |
H1.pageBreakBefore = 0 |
|
109 |
H1.keepWithNext = 0 |
|
110 |
||
111 |
bt = styleSheet['BodyText'] |
|
112 |
pto = ParagraphStyle('pto',parent=bt) |
|
113 |
pto.alignment = TA_RIGHT |
|
114 |
pto.fontSize -= 1 |
|
115 |
def ColorParagraph(c,text,style): |
|
116 |
return Paragraph('<para color="%s">%s</para>' % (c,text),style) |
|
117 |
||
118 |
def ptoblob(blurb,content,trailer=None,header=None, story=story, H1=H1): |
|
119 |
if type(content) not in (type([]),type(())): content = [content] |
|
120 |
story.append(PTOContainer([Paragraph(blurb,H1)]+list(content),trailer,header)) |
|
121 |
||
122 |
t0 = [ColorParagraph('blue','Please turn over', pto )] |
|
123 |
h0 = [ColorParagraph('blue','continued from previous page', pto )] |
|
124 |
t1 = [ColorParagraph('red','Please turn over(inner)', pto )] |
|
125 |
h1 = [ColorParagraph('red','continued from previous page(inner)', pto )] |
|
126 |
ptoblob('First Try at a PTO',[Paragraph(text0,bt)],t0,h0) |
|
127 |
fbreak() |
|
128 |
c1 = Table([('alignment', 'align\012alignment'), |
|
129 |
('bulletColor', 'bulletcolor\012bcolor'), |
|
130 |
('bulletFontName', 'bfont\012bulletfontname'), |
|
131 |
('bulletFontSize', 'bfontsize\012bulletfontsize'), |
|
132 |
('bulletIndent', 'bindent\012bulletindent'), |
|
133 |
('firstLineIndent', 'findent\012firstlineindent'), |
|
134 |
('fontName', 'face\012fontname\012font'), |
|
135 |
('fontSize', 'size\012fontsize'), |
|
136 |
('leading', 'leading'), |
|
137 |
('leftIndent', 'leftindent\012lindent'), |
|
138 |
('rightIndent', 'rightindent\012rindent'), |
|
139 |
('spaceAfter', 'spaceafter\012spacea'), |
|
140 |
('spaceBefore', 'spacebefore\012spaceb'), |
|
141 |
('textColor', 'fg\012textcolor\012color')], |
|
142 |
style = [ |
|
143 |
('VALIGN',(0,0),(-1,-1),'TOP'), |
|
144 |
('INNERGRID', (0,0), (-1,-1), 0.25, black), |
|
145 |
('BOX', (0,0), (-1,-1), 0.25, black), |
|
146 |
], |
|
147 |
) |
|
148 |
ptoblob('PTO with a table inside',c1,t0,h0) |
|
149 |
fbreak() |
|
150 |
ptoblob('A long PTO',[Paragraph(text0+' '+text1,bt)],t0,h0) |
|
151 |
fbreak() |
|
152 |
ptoblob('2 PTO (inner split)',[ColorParagraph('pink',text0,bt),PTOContainer([ColorParagraph(black,'Inner Starts',H1),ColorParagraph('yellow',text2,bt),ColorParagraph('black','Inner Ends',H1)],t1,h1),ColorParagraph('magenta',text1,bt)],t0,h0) |
|
153 |
_showDoc('test_platypus_pto.pdf',story) |
|
154 |
||
3183
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
155 |
def _KeepInFrameTestCase(self,mode,offset=0): |
2963 | 156 |
story = [] |
157 |
def fbreak(story=story): |
|
158 |
story.append(FrameBreak()) |
|
159 |
styleSheet = getSampleStyleSheet() |
|
160 |
H1 = styleSheet['Heading1'] |
|
161 |
H1.pageBreakBefore = 0 |
|
162 |
H1.keepWithNext = 0 |
|
163 |
bt = styleSheet['BodyText'] |
|
3706
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
164 |
def subStory(texts,fbg=0): |
3183
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
165 |
style = [ |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
166 |
('VALIGN',(0,0),(-1,-1),'TOP'), |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
167 |
('INNERGRID', (0,0), (-1,-1), 0.25, black), |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
168 |
('BOX', (0,0), (-1,-1), 0.25, black), |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
169 |
] |
3706
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
170 |
if fbg: |
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
171 |
fbg1 = [FrameBG(start=0,color=toColor('limegreen'))] |
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
172 |
fbg0 = [FrameBG(start=0)] |
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
173 |
else: |
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
174 |
fbg0 = fbg1 = [] |
3183
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
175 |
return ([Paragraph(t,bt) for t in texts] |
3706
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
176 |
+fbg1+[Table([('alignment', a.lower())],style = style,hAlign=a) |
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
177 |
for a in ('LEFT','RIGHT','CENTER')]+fbg0) |
3183
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
178 |
def allModesKIF(just,ifb=True,width=170): |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
179 |
if ifb: fbreak() |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
180 |
story.append(KeepInFrame(width-offset,284-offset,subStory(texts=(text0,)),mode=mode,hAlign=just)) |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
181 |
fbreak() |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
182 |
story.append(KeepInFrame(width-offset,284-offset,subStory(texts=(text0,text1)),mode=mode,hAlign=just)) |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
183 |
fbreak() |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
184 |
story.append(KeepInFrame(width-offset,284-offset,subStory(texts=(text0,text1,text2)),mode=mode,hAlign=just)) |
3706
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
185 |
fbreak() |
d468d1f4e8b7
platypus: change implementation of new FrameBG so it can be used in containers
robin
parents:
3617
diff
changeset
|
186 |
story.append(KeepInFrame(width-offset,284-offset,subStory(texts=(text0,text1,text2),fbg=1),mode=mode,hAlign=just)) |
3183
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
187 |
allModesKIF('LEFT',False) |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
188 |
allModesKIF('LEFT',width=100) |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
189 |
allModesKIF('CENTRE',width=100) |
859ea04ea21a
test_platypus_pleaseturnover.py: new tests for justifying KeepInFrame
rgbecker
parents:
2984
diff
changeset
|
190 |
allModesKIF('RIGHT',width=100) |
2963 | 191 |
_showDoc('test_platypus_KeepInFrame%s.pdf'%mode,story) |
192 |
||
193 |
class TestCases(unittest.TestCase): |
|
194 |
"Test multi-page splitting of paragraphs (eyeball-test)." |
|
195 |
def test0(self): |
|
196 |
_ptoTestCase(self) |
|
197 |
def test1(self): |
|
198 |
_KeepInFrameTestCase(self,mode="shrink") |
|
199 |
def test2(self): |
|
200 |
_KeepInFrameTestCase(self,mode="overflow") |
|
201 |
def test3(self): |
|
202 |
_KeepInFrameTestCase(self,mode="truncate") |
|
203 |
def test4(self): |
|
204 |
from reportlab.platypus.doctemplate import LayoutError |
|
205 |
self.assertRaises(LayoutError, _KeepInFrameTestCase,*(self,"error")) |
|
206 |
def test5(self): |
|
207 |
_KeepInFrameTestCase(self,"shrink",0) |
|
208 |
||
209 |
def makeSuite(): |
|
210 |
return makeSuiteForClasses(TestCases) |
|
211 |
||
212 |
#noruntests |
|
213 |
if __name__ == "__main__": #NORUNTESTS |
|
214 |
if 'debug' in sys.argv: |
|
215 |
_KeepInFrameTestCase(None) |
|
216 |
else: |
|
217 |
unittest.TextTestRunner().run(makeSuite()) |
|
218 |
printLocation() |