author | andy_robinson |
Sun, 09 Nov 2003 00:54:22 +0000 | |
changeset 2113 | e82d8b3880d8 |
parent 2080 | 39156f56ab6f |
child 2158 | e9c34b93e317 |
permissions | -rw-r--r-- |
494 | 1 |
#copyright ReportLab Inc. 2000 |
2 |
#see license.txt for license details |
|
3 |
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/flowables.py?cvsroot=reportlab |
|
2113
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
4 |
#$Header: /tmp/reportlab/reportlab/platypus/flowables.py,v 1.41 2003/11/09 00:54:22 andy_robinson Exp $ |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
5 |
__version__=''' $Id: flowables.py,v 1.41 2003/11/09 00:54:22 andy_robinson Exp $ ''' |
253 | 6 |
__doc__=""" |
268 | 7 |
A flowable is a "floating element" in a document whose exact position is determined by the |
8 |
other elements that precede it, such as a paragraph, a diagram interspersed between paragraphs, |
|
9 |
a section header, etcetera. Examples of non-flowables include page numbering annotations, |
|
10 |
headers, footers, fixed diagrams or logos, among others. |
|
11 |
||
12 |
Flowables are defined here as objects which know how to determine their size and which |
|
13 |
can draw themselves onto a page with respect to a relative "origin" position determined |
|
426 | 14 |
at a higher level. The object's draw() method should assume that (0,0) corresponds to the |
15 |
bottom left corner of the enclosing rectangle that will contain the object. The attributes |
|
16 |
vAlign and hAlign may be used by 'packers' as hints as to how the object should be placed. |
|
17 |
||
18 |
Some Flowables also know how to "split themselves". For example a |
|
268 | 19 |
long paragraph might split itself between one page and the next. |
20 |
||
1490 | 21 |
Packers should set the canv attribute during wrap, split & draw operations to allow |
22 |
the flowable to work out sizes etc in the proper context. |
|
23 |
||
268 | 24 |
The "text" of a document usually consists mainly of a sequence of flowables which |
25 |
flow into a document from top to bottom (with column and page breaks controlled by |
|
26 |
higher level components). |
|
253 | 27 |
""" |
28 |
import os |
|
29 |
import string |
|
30 |
from copy import deepcopy |
|
557 | 31 |
from types import ListType, TupleType, StringType |
253 | 32 |
|
33 |
from reportlab.pdfgen import canvas |
|
34 |
from reportlab.lib.units import inch |
|
35 |
from reportlab.lib.colors import red |
|
36 |
from reportlab.pdfbase import pdfutils |
|
37 |
||
724 | 38 |
from reportlab.rl_config import defaultPageSize |
684 | 39 |
PAGE_HEIGHT = defaultPageSize[1] |
253 | 40 |
|
2113
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
41 |
|
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
42 |
class TraceInfo: |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
43 |
"Holder for info about where an object originated" |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
44 |
def __init__(self): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
45 |
self.srcFile = '(unknown)' |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
46 |
self.startLineNo = -1 |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
47 |
self.startLinePos = -1 |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
48 |
self.endLineNo = -1 |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
49 |
self.endLinePos = -1 |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
50 |
|
253 | 51 |
############################################################# |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
52 |
# Flowable Objects - a base class and a few examples. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
53 |
# One is just a box to get some metrics. We also have |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
54 |
# a paragraph, an image and a special 'page break' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
55 |
# object which fills the space. |
253 | 56 |
############################################################# |
57 |
class Flowable: |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
58 |
"""Abstract base class for things to be drawn. Key concepts: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
59 |
1. It knows its size |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
60 |
2. It draws in its own coordinate system (this requires the |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
61 |
base API to provide a translate() function. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
62 |
""" |
1917 | 63 |
_fixedWidth = 0 #assume wrap results depend on arguments? |
64 |
_fixedHeight = 0 |
|
65 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
66 |
def __init__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
67 |
self.width = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
68 |
self.height = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
69 |
self.wrapped = 0 |
253 | 70 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
71 |
#these are hints to packers/frames as to how the floable should be positioned |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
72 |
self.hAlign = 'LEFT' #CENTER/CENTRE or RIGHT |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
73 |
self.vAlign = 'BOTTOM' #MIDDLE or TOP |
426 | 74 |
|
2113
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
75 |
#optional holder for trace info |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
76 |
self._traceInfo = None |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
77 |
|
1494 | 78 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
79 |
def _drawOn(self,canv): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
80 |
'''ensure canv is set on and then draw''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
81 |
self.canv = canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
82 |
self.draw()#this is the bit you overload |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
83 |
del self.canv |
1494 | 84 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
85 |
def drawOn(self, canvas, x, y, _sW=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
86 |
"Tell it to draw itself on the canvas. Do not override" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
87 |
if _sW and hasattr(self,'hAlign'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
88 |
a = self.hAlign |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
89 |
if a in ['CENTER','CENTRE']: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
90 |
x = x + 0.5*_sW |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
91 |
elif a == 'RIGHT': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
92 |
x = x + _sW |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
93 |
elif a != 'LEFT': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
94 |
raise ValueError, "Bad hAlign value "+str(a) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
95 |
canvas.saveState() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
96 |
canvas.translate(x, y) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
97 |
self._drawOn(canvas) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
98 |
canvas.restoreState() |
253 | 99 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
100 |
def wrapOn(self, canv, aW, aH): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
101 |
'''intended for use by packers allows setting the canvas on |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
102 |
during the actual wrap''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
103 |
self.canv = canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
104 |
w, h = self.wrap(aW,aH) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
105 |
del self.canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
106 |
return w, h |
253 | 107 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
108 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
109 |
"""This will be called by the enclosing frame before objects |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
110 |
are asked their size, drawn or whatever. It returns the |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
111 |
size actually used.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
112 |
return (self.width, self.height) |
253 | 113 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
114 |
def minWidth(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
115 |
"""This should return the minimum required width""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
116 |
return getattr(self,'_minWidth',self.width) |
954 | 117 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
118 |
def splitOn(self, canv, aW, aH): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
119 |
'''intended for use by packers allows setting the canvas on |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
120 |
during the actual split''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
121 |
self.canv = canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
122 |
S = self.split(aW,aH) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
123 |
del self.canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
124 |
return S |
1491 | 125 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
126 |
def split(self, availWidth, availheight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
127 |
"""This will be called by more sophisticated frames when |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
128 |
wrap fails. Stupid flowables should return []. Clever flowables |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
129 |
should split themselves and return a list of flowables""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
130 |
return [] |
253 | 131 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
132 |
def getKeepWithNext(self): |
1879 | 133 |
"""returns boolean determining whether the next flowable should stay with this one""" |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
134 |
if hasattr(self,'keepWithNext'): return self.keepWithNext |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
135 |
elif hasattr(self,'style') and hasattr(self.style,'keepWithNext'): return self.style.keepWithNext |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
136 |
else: return 0 |
253 | 137 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
138 |
def getSpaceAfter(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
139 |
"""returns how much space should follow this item if another item follows on the same page.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
140 |
if hasattr(self,'spaceAfter'): return self.spaceAfter |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
141 |
elif hasattr(self,'style') and hasattr(self.style,'spaceAfter'): return self.style.spaceAfter |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
142 |
else: return 0 |
253 | 143 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
144 |
def getSpaceBefore(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
145 |
"""returns how much space should precede this item if another item precedess on the same page.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
146 |
if hasattr(self,'spaceBefore'): return self.spaceBefore |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
147 |
elif hasattr(self,'style') and hasattr(self.style,'spaceBefore'): return self.style.spaceBefore |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
148 |
else: return 0 |
1426 | 149 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
150 |
def isIndexing(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
151 |
"""Hook for IndexingFlowables - things which have cross references""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
152 |
return 0 |
512 | 153 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
154 |
def identity(self, maxLen=None): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
155 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
156 |
This method should attempt to return a string that can be used to identify |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
157 |
a particular flowable uniquely. The result can then be used for debugging |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
158 |
and or error printouts |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
159 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
160 |
if hasattr(self, 'getPlainText'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
161 |
r = self.getPlainText() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
162 |
elif hasattr(self, 'text'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
163 |
r = self.text |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
164 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
165 |
r = '...' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
166 |
if r and maxLen: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
167 |
r = r[:maxLen] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
168 |
return "<%s at %d>%s" % (self.__class__.__name__, id(self), r) |
1103 | 169 |
|
253 | 170 |
class XBox(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
171 |
"""Example flowable - a box with an x through it and a caption. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
172 |
This has a known size, so does not need to respond to wrap().""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
173 |
def __init__(self, width, height, text = 'A Box'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
174 |
Flowable.__init__(self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
175 |
self.width = width |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
176 |
self.height = height |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
177 |
self.text = text |
253 | 178 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
179 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
180 |
return "XBox(w=%s, h=%s, t=%s)" % (self.width, self.height, self.text) |
541
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
512
diff
changeset
|
181 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
182 |
def draw(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
183 |
self.canv.rect(0, 0, self.width, self.height) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
184 |
self.canv.line(0, 0, self.width, self.height) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
185 |
self.canv.line(0, self.height, self.width, 0) |
253 | 186 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
187 |
#centre the text |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
188 |
self.canv.setFont('Times-Roman',12) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
189 |
self.canv.drawCentredString(0.5*self.width, 0.5*self.height, self.text) |
253 | 190 |
|
445 | 191 |
def _trimEmptyLines(lines): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
192 |
#don't want the first or last to be empty |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
193 |
while len(lines) and string.strip(lines[0]) == '': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
194 |
lines = lines[1:] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
195 |
while len(lines) and string.strip(lines[-1]) == '': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
196 |
lines = lines[:-1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
197 |
return lines |
445 | 198 |
|
442 | 199 |
def _dedenter(text,dedent=0): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
200 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
201 |
tidy up text - carefully, it is probably code. If people want to |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
202 |
indent code within a source script, you can supply an arg to dedent |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
203 |
and it will chop off that many character, otherwise it leaves |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
204 |
left edge intact. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
205 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
206 |
lines = string.split(text, '\n') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
207 |
if dedent>0: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
208 |
templines = _trimEmptyLines(lines) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
209 |
lines = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
210 |
for line in templines: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
211 |
line = string.rstrip(line[dedent:]) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
212 |
lines.append(line) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
213 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
214 |
lines = _trimEmptyLines(lines) |
445 | 215 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
216 |
return lines |
442 | 217 |
|
253 | 218 |
class Preformatted(Flowable): |
1683 | 219 |
"""This is like the HTML <PRE> tag. |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
220 |
It attempts to display text exactly as you typed it in a fixed width "typewriter" font. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
221 |
The line breaks are exactly where you put |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
222 |
them, and it will not be wrapped.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
223 |
def __init__(self, text, style, bulletText = None, dedent=0): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
224 |
"""text is the text to display. If dedent is set then common leading space |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
225 |
will be chopped off the front (for example if the entire text is indented |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
226 |
6 spaces or more then each line will have 6 spaces removed from the front). |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
227 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
228 |
self.style = style |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
229 |
self.bulletText = bulletText |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
230 |
self.lines = _dedenter(text,dedent) |
253 | 231 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
232 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
233 |
bT = self.bulletText |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
234 |
H = "Preformatted(" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
235 |
if bT is not None: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
236 |
H = "Preformatted(bulletText=%s," % repr(bT) |
2023 | 237 |
return "%s'''\\ \n%s''')" % (H, string.join(self.lines,'\n')) |
541
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
512
diff
changeset
|
238 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
239 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
240 |
self.width = availWidth |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
241 |
self.height = self.style.leading*len(self.lines) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
242 |
return (self.width, self.height) |
253 | 243 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
244 |
def split(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
245 |
#returns two Preformatted objects |
253 | 246 |
|
1683 | 247 |
#not sure why they can be called with a negative height |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
248 |
if availHeight < self.style.leading: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
249 |
return [] |
1683 | 250 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
251 |
linesThatFit = int(availHeight * 1.0 / self.style.leading) |
1683 | 252 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
253 |
text1 = string.join(self.lines[0:linesThatFit], '\n') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
254 |
text2 = string.join(self.lines[linesThatFit:], '\n') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
255 |
style = self.style |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
256 |
if style.firstLineIndent != 0: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
257 |
style = deepcopy(style) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
258 |
style.firstLineIndent = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
259 |
return [Preformatted(text1, self.style), Preformatted(text2, style)] |
1683 | 260 |
|
253 | 261 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
262 |
def draw(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
263 |
#call another method for historical reasons. Besides, I |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
264 |
#suspect I will be playing with alternate drawing routines |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
265 |
#so not doing it here makes it easier to switch. |
253 | 266 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
267 |
cur_x = self.style.leftIndent |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
268 |
cur_y = self.height - self.style.fontSize |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
269 |
self.canv.addLiteral('%PreformattedPara') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
270 |
if self.style.textColor: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
271 |
self.canv.setFillColor(self.style.textColor) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
272 |
tx = self.canv.beginText(cur_x, cur_y) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
273 |
#set up the font etc. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
274 |
tx.setFont( self.style.fontName, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
275 |
self.style.fontSize, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
276 |
self.style.leading) |
253 | 277 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
278 |
for text in self.lines: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
279 |
tx.textLine(text) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
280 |
self.canv.drawText(tx) |
253 | 281 |
|
282 |
class Image(Flowable): |
|
2045 | 283 |
"""an image (digital picture). Formats supported by PIL/Java 1.4 (the Python/Java Imaging Library |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
284 |
are supported. At the present time images as flowables are always centered horozontally |
2080 | 285 |
in the frame. We allow for two kinds of lazyness to allow for many images in a document |
286 |
which could lead to file handle starvation. |
|
287 |
lazy=1 don't open image until required. |
|
288 |
lazy=2 open image when required then shut it. |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
289 |
""" |
1917 | 290 |
_fixedWidth = 1 |
291 |
_fixedHeight = 1 |
|
2080 | 292 |
def __init__(self, filename, width=None, height=None, kind='direct', mask="auto", lazy=1): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
293 |
"""If size to draw at not specified, get it from the image.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
294 |
self.hAlign = 'CENTER' |
1880 | 295 |
self._mask = mask |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
296 |
# if it is a JPEG, will be inlined within the file - |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
297 |
# but we still need to know its size now |
2080 | 298 |
fp = hasattr(filename,'read') |
299 |
if fp: |
|
300 |
self.filename = `filename` |
|
301 |
else: |
|
302 |
self.filename = filename |
|
303 |
if not fp and os.path.splitext(filename)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']: |
|
304 |
f = open(filename, 'rb') |
|
305 |
info = pdfutils.readJPEGInfo(f) |
|
306 |
f.close() |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
307 |
self.imageWidth = info[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
308 |
self.imageHeight = info[1] |
2080 | 309 |
self._setup(width,height,kind,0) |
310 |
self._img = None |
|
311 |
elif fp: |
|
312 |
self._setup(width,height,kind,0) |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
313 |
else: |
2080 | 314 |
self._setup(width,height,kind,lazy) |
1477 | 315 |
|
2080 | 316 |
def _setup(self,width,height,kind,lazy): |
317 |
self._lazy = lazy |
|
318 |
self._width = width |
|
319 |
self._height = height |
|
320 |
self._kind = kind |
|
321 |
if lazy<=0: self._setup_inner() |
|
322 |
||
323 |
def _setup_inner(self): |
|
324 |
width = self._width |
|
325 |
height = self._height |
|
326 |
kind = self._kind |
|
327 |
(self.imageWidth, self.imageHeight) = self._img.getSize() |
|
328 |
if self._lazy>=2: del self._img |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
329 |
if kind in ['direct','absolute']: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
330 |
self.drawWidth = width or self.imageWidth |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
331 |
self.drawHeight = height or self.imageHeight |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
332 |
elif kind in ['percentage','%']: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
333 |
self.drawWidth = self.imageWidth*width*0.01 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
334 |
self.drawHeight = self.imageHeight*height*0.01 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
335 |
elif kind in ['bound','proportional']: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
336 |
factor = min(float(width)/self.imageWidth,float(height)/self.imageHeight) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
337 |
self.drawWidth = self.imageWidth*factor |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
338 |
self.drawHeight = self.imageHeight*factor |
253 | 339 |
|
2080 | 340 |
def __getattr__(self,a): |
341 |
if a=='_img': |
|
342 |
from reportlab.lib.utils import ImageReader #this may raise an error |
|
343 |
self._img = ImageReader(self.filename) |
|
344 |
return self._img |
|
345 |
elif a in ('drawWidth','drawHeight','imageWidth','imageHeight'): |
|
346 |
self._setup_inner() |
|
347 |
return self.__dict__[a] |
|
348 |
raise AttributeError(a) |
|
349 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
350 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
351 |
#the caller may decide it does not fit. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
352 |
return (self.drawWidth, self.drawHeight) |
253 | 353 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
354 |
def draw(self): |
2080 | 355 |
lazy = self._lazy |
356 |
if lazy>=2: self._lazy = 1 |
|
357 |
self.canv.drawImage( self._img or self.filename, |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
358 |
0, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
359 |
0, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
360 |
self.drawWidth, |
1880 | 361 |
self.drawHeight, |
1882 | 362 |
mask=self._mask, |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
363 |
) |
2080 | 364 |
if lazy>=2: |
365 |
self._img = None |
|
366 |
self._lazy = lazy |
|
1103 | 367 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
368 |
def identity(self,maxLen): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
369 |
r = Flowable.identity(self,maxLen) |
2080 | 370 |
if r[-4:]=='>...' and type(self.filename) is StringType: |
371 |
r = "%s filename=%s>" % (r[:-4],self.filename) |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
372 |
return r |
1103 | 373 |
|
253 | 374 |
class Spacer(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
375 |
"""A spacer just takes up space and doesn't draw anything - it guarantees |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
376 |
a gap between objects.""" |
1917 | 377 |
_fixedWidth = 1 |
378 |
_fixedHeight = 1 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
379 |
def __init__(self, width, height): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
380 |
self.width = width |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
381 |
self.height = height |
253 | 382 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
383 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
384 |
return "Spacer(%s, %s)" % (self.width, self.height) |
541
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
512
diff
changeset
|
385 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
386 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
387 |
return (self.width, self.height) |
253 | 388 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
389 |
def draw(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
390 |
pass |
253 | 391 |
|
307 | 392 |
class PageBreak(Spacer): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
393 |
"""Move on to the next page in the document. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
394 |
This works by consuming all remaining space in the frame!""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
395 |
def __init__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
396 |
pass |
1683 | 397 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
398 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
399 |
return "PageBreak()" |
253 | 400 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
401 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
402 |
self.width = availWidth |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
403 |
self.height = availHeight |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
404 |
return (availWidth,availHeight) #step back a point |
253 | 405 |
|
307 | 406 |
class CondPageBreak(Spacer): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
407 |
"""Throw a page if not enough vertical space""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
408 |
def __init__(self, height): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
409 |
self.height = height |
1683 | 410 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
411 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
412 |
return "CondPageBreak(%s)" %(self.height,) |
307 | 413 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
414 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
415 |
if availHeight<self.height: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
416 |
return (availWidth, availHeight) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
417 |
return (0, 0) |
253 | 418 |
|
367 | 419 |
_SeqTypes = (ListType, TupleType) |
558 | 420 |
|
367 | 421 |
class KeepTogether(Flowable): |
1994 | 422 |
def __init__(self,flowables,maxHeight=None): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
423 |
if type(flowables) not in _SeqTypes: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
424 |
self._flowables = [flowables] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
425 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
426 |
self._flowables = flowables |
1994 | 427 |
self._maxHeight = maxHeight |
367 | 428 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
429 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
430 |
f = self._flowables |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
431 |
L = map(repr,f) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
432 |
import string |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
433 |
L = "\n"+string.join(L, "\n") |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
434 |
L = string.replace(L, "\n", "\n ") |
1994 | 435 |
return "KeepTogether(%s,maxHeight=%s) # end KeepTogether" % (L,self._maxHeight) |
541
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
512
diff
changeset
|
436 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
437 |
def wrap(self, aW, aH): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
438 |
W = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
439 |
H = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
440 |
F = self._flowables |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
441 |
canv = self.canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
442 |
for f in F: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
443 |
w,h = f.wrapOn(canv,aW,0xfffffff) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
444 |
if f is not F[0]: h = h + f.getSpaceBefore() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
445 |
if f is not F[-1]: h = h + f.getSpaceAfter() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
446 |
W = max(W,w) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
447 |
H = H+h |
1994 | 448 |
self._CPage = (H>aH) and (not self._maxHeight or H<=self._maxHeight) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
449 |
return W, 0xffffff # force a split |
367 | 450 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
451 |
def split(self, aW, aH): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
452 |
S = getattr(self,'_CPage',1) and [CondPageBreak(aH+1)] or [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
453 |
for f in self._flowables: S.append(f) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
454 |
return S |
367 | 455 |
|
253 | 456 |
class Macro(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
457 |
"""This is not actually drawn (i.e. it has zero height) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
458 |
but is executed when it would fit in the frame. Allows direct |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
459 |
access to the canvas through the object 'canvas'""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
460 |
def __init__(self, command): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
461 |
self.command = command |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
462 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
463 |
return "Macro(%s)" % repr(self.command) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
464 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
465 |
return (0,0) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
466 |
def draw(self): |
1768 | 467 |
exec self.command in globals(), {'canvas':self.canv} |
468 |
||
469 |
class ParagraphAndImage(Flowable): |
|
470 |
'''combine a Paragraph and an Image''' |
|
471 |
def __init__(self,P,I,xpad=3,ypad=3): |
|
472 |
self.P, self.style, self.I, self.xpad, self.ypad = P,P.style,I,xpad,ypad |
|
473 |
||
474 |
def wrap(self,availWidth,availHeight): |
|
475 |
wI, hI = self.I.wrap(availWidth,availHeight) |
|
476 |
self.wI, self.hI = wI, hI |
|
477 |
# work out widths array for breaking |
|
478 |
self.width = availWidth |
|
479 |
P, style, xpad, ypad = self.P, self.style, self.xpad, self.ypad |
|
480 |
leading = style.leading |
|
481 |
leftIndent = style.leftIndent |
|
482 |
later_widths = availWidth - leftIndent - style.rightIndent |
|
483 |
intermediate_widths = later_widths - xpad - wI |
|
484 |
first_line_width = intermediate_widths - style.firstLineIndent |
|
485 |
P.width = 0 |
|
486 |
P.blPara = P.breakLines([first_line_width] + int((hI+ypad)/leading)*[intermediate_widths]+[later_widths]) |
|
487 |
P.height = len(P.blPara.lines)*leading |
|
488 |
self.height = max(hI,P.height) |
|
489 |
return (self.width, self.height) |
|
490 |
||
491 |
def split(self,availWidth, availHeight): |
|
492 |
P, wI, hI, ypad = self.P, self.wI, self.hI, self.ypad |
|
493 |
if hI+ypad>availHeight or len(P.frags)<=0: return [] |
|
494 |
S = P.split(availWidth,availHeight) |
|
495 |
if not S: return S |
|
496 |
P = self.P = S[0] |
|
497 |
del S[0] |
|
498 |
style = self.style = P.style |
|
499 |
P.height = len(self.P.blPara.lines)*style.leading |
|
500 |
self.height = max(hI,P.height) |
|
501 |
return [self]+S |
|
502 |
||
503 |
def draw(self): |
|
504 |
canv = self.canv |
|
505 |
self.I.drawOn(canv,self.width-self.wI-self.xpad,self.height-self.hI) |
|
506 |
self.P.drawOn(canv,0,0) |
|
2113
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
507 |
|
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
508 |
class FailOnWrap(Flowable): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
509 |
def wrap(self, availWidth, availHeight): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
510 |
raise ValueError("FailOnWrap flowable wrapped and failing as ordered!") |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
511 |
|
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
512 |
def draw(self): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
513 |
pass |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
514 |
|
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
515 |
class FailOnDraw(Flowable): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
516 |
def wrap(self, availWidth, availHeight): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
517 |
return (0,0) |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
518 |
|
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
519 |
def draw(self): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
520 |
raise ValueError("FailOnDraw flowable drawn, and failing as ordered!") |