author | rgbecker |
Tue, 27 Sep 2005 13:26:12 +0000 | |
changeset 2525 | 634ec1f48514 |
parent 2523 | 0473810aff11 |
child 2526 | eaaa012dffd9 |
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/flowables.py |
2321 | 4 |
__version__=''' $Id$ ''' |
253 | 5 |
__doc__=""" |
268 | 6 |
A flowable is a "floating element" in a document whose exact position is determined by the |
7 |
other elements that precede it, such as a paragraph, a diagram interspersed between paragraphs, |
|
8 |
a section header, etcetera. Examples of non-flowables include page numbering annotations, |
|
9 |
headers, footers, fixed diagrams or logos, among others. |
|
10 |
||
11 |
Flowables are defined here as objects which know how to determine their size and which |
|
12 |
can draw themselves onto a page with respect to a relative "origin" position determined |
|
426 | 13 |
at a higher level. The object's draw() method should assume that (0,0) corresponds to the |
14 |
bottom left corner of the enclosing rectangle that will contain the object. The attributes |
|
15 |
vAlign and hAlign may be used by 'packers' as hints as to how the object should be placed. |
|
16 |
||
17 |
Some Flowables also know how to "split themselves". For example a |
|
268 | 18 |
long paragraph might split itself between one page and the next. |
19 |
||
1490 | 20 |
Packers should set the canv attribute during wrap, split & draw operations to allow |
21 |
the flowable to work out sizes etc in the proper context. |
|
22 |
||
268 | 23 |
The "text" of a document usually consists mainly of a sequence of flowables which |
24 |
flow into a document from top to bottom (with column and page breaks controlled by |
|
25 |
higher level components). |
|
253 | 26 |
""" |
27 |
import os |
|
28 |
import string |
|
29 |
from copy import deepcopy |
|
557 | 30 |
from types import ListType, TupleType, StringType |
253 | 31 |
|
2276 | 32 |
from reportlab.lib.colors import red, gray, lightgrey |
2525 | 33 |
from reportlab.lib.utils import fp_str |
253 | 34 |
from reportlab.pdfbase import pdfutils |
35 |
||
2525 | 36 |
from reportlab.rl_config import _FUZZ, overlapAttachedSpace |
37 |
__all__=('TraceInfo','Flowable','XBox','Preformatted','Image','Spacer','PageBreak','SlowPageBreak', |
|
38 |
'CondPageBreak','KeepTogether','Macro','CallerMacro','ParagraphAndImage', |
|
39 |
'FailOnWrap','HRFlowable','PTOContainer','FrameFlowable') |
|
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 |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2192
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 |
2158 | 77 |
self._showBoundary = None |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2192
diff
changeset
|
78 |
|
1494 | 79 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
80 |
def _drawOn(self,canv): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
81 |
'''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
|
82 |
self.canv = canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
83 |
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
|
84 |
del self.canv |
1494 | 85 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
86 |
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
|
87 |
"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
|
88 |
if _sW and hasattr(self,'hAlign'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
89 |
a = self.hAlign |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
90 |
if a in ['CENTER','CENTRE']: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
91 |
x = x + 0.5*_sW |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
92 |
elif a == 'RIGHT': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
93 |
x = x + _sW |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
94 |
elif a != 'LEFT': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
95 |
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
|
96 |
canvas.saveState() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
97 |
canvas.translate(x, y) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
98 |
self._drawOn(canvas) |
2158 | 99 |
if hasattr(self, '_showBoundary') and self._showBoundary: |
100 |
#diagnostic tool support |
|
101 |
canvas.setStrokeColor(gray) |
|
102 |
canvas.rect(0,0,self.width, self.height) |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
103 |
canvas.restoreState() |
253 | 104 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
105 |
def wrapOn(self, canv, aW, aH): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
106 |
'''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
|
107 |
during the actual wrap''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
108 |
self.canv = canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
109 |
w, h = self.wrap(aW,aH) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
110 |
del self.canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
111 |
return w, h |
253 | 112 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
113 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
114 |
"""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
|
115 |
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
|
116 |
size actually used.""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
117 |
return (self.width, self.height) |
253 | 118 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
119 |
def minWidth(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
120 |
"""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
|
121 |
return getattr(self,'_minWidth',self.width) |
954 | 122 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
123 |
def splitOn(self, canv, aW, aH): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
124 |
'''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
|
125 |
during the actual split''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
126 |
self.canv = canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
127 |
S = self.split(aW,aH) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
128 |
del self.canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
129 |
return S |
1491 | 130 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
131 |
def split(self, availWidth, availheight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
132 |
"""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
|
133 |
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
|
134 |
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
|
135 |
return [] |
253 | 136 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
137 |
def getKeepWithNext(self): |
1879 | 138 |
"""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
|
139 |
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
|
140 |
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
|
141 |
else: return 0 |
253 | 142 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
143 |
def getSpaceAfter(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
144 |
"""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
|
145 |
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
|
146 |
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
|
147 |
else: return 0 |
253 | 148 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
149 |
def getSpaceBefore(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
150 |
"""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
|
151 |
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
|
152 |
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
|
153 |
else: return 0 |
1426 | 154 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
155 |
def isIndexing(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
156 |
"""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
|
157 |
return 0 |
512 | 158 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
159 |
def identity(self, maxLen=None): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
160 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
161 |
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
|
162 |
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
|
163 |
and or error printouts |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
164 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
165 |
if hasattr(self, 'getPlainText'): |
2172 | 166 |
r = self.getPlainText(identify=1) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
167 |
elif hasattr(self, 'text'): |
2461 | 168 |
r = str(self.text) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
169 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
170 |
r = '...' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
171 |
if r and maxLen: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
172 |
r = r[:maxLen] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
173 |
return "<%s at %d>%s" % (self.__class__.__name__, id(self), r) |
1103 | 174 |
|
253 | 175 |
class XBox(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
176 |
"""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
|
177 |
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
|
178 |
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
|
179 |
Flowable.__init__(self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
180 |
self.width = width |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
181 |
self.height = height |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
182 |
self.text = text |
253 | 183 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
184 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
185 |
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
|
186 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
187 |
def draw(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
188 |
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
|
189 |
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
|
190 |
self.canv.line(0, self.height, self.width, 0) |
253 | 191 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
192 |
#centre the text |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
193 |
self.canv.setFont('Times-Roman',12) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
194 |
self.canv.drawCentredString(0.5*self.width, 0.5*self.height, self.text) |
253 | 195 |
|
445 | 196 |
def _trimEmptyLines(lines): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
197 |
#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
|
198 |
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
|
199 |
lines = lines[1:] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
200 |
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
|
201 |
lines = lines[:-1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
202 |
return lines |
445 | 203 |
|
442 | 204 |
def _dedenter(text,dedent=0): |
1677
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 |
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
|
207 |
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
|
208 |
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
|
209 |
left edge intact. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
210 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
211 |
lines = string.split(text, '\n') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
212 |
if dedent>0: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
213 |
templines = _trimEmptyLines(lines) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
214 |
lines = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
215 |
for line in templines: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
216 |
line = string.rstrip(line[dedent:]) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
217 |
lines.append(line) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
218 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
219 |
lines = _trimEmptyLines(lines) |
445 | 220 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
221 |
return lines |
442 | 222 |
|
253 | 223 |
class Preformatted(Flowable): |
1683 | 224 |
"""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
|
225 |
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
|
226 |
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
|
227 |
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
|
228 |
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
|
229 |
"""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
|
230 |
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
|
231 |
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
|
232 |
""" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
233 |
self.style = style |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
234 |
self.bulletText = bulletText |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
235 |
self.lines = _dedenter(text,dedent) |
253 | 236 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
237 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
238 |
bT = self.bulletText |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
239 |
H = "Preformatted(" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
240 |
if bT is not None: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
241 |
H = "Preformatted(bulletText=%s," % repr(bT) |
2023 | 242 |
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
|
243 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
244 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
245 |
self.width = availWidth |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
246 |
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
|
247 |
return (self.width, self.height) |
253 | 248 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
249 |
def split(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
250 |
#returns two Preformatted objects |
253 | 251 |
|
1683 | 252 |
#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
|
253 |
if availHeight < self.style.leading: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
254 |
return [] |
1683 | 255 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
256 |
linesThatFit = int(availHeight * 1.0 / self.style.leading) |
1683 | 257 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
258 |
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
|
259 |
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
|
260 |
style = self.style |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
261 |
if style.firstLineIndent != 0: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
262 |
style = deepcopy(style) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
263 |
style.firstLineIndent = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
264 |
return [Preformatted(text1, self.style), Preformatted(text2, style)] |
1683 | 265 |
|
253 | 266 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
267 |
def draw(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
268 |
#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
|
269 |
#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
|
270 |
#so not doing it here makes it easier to switch. |
253 | 271 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
272 |
cur_x = self.style.leftIndent |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
273 |
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
|
274 |
self.canv.addLiteral('%PreformattedPara') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
275 |
if self.style.textColor: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
276 |
self.canv.setFillColor(self.style.textColor) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
277 |
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
|
278 |
#set up the font etc. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
279 |
tx.setFont( self.style.fontName, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
280 |
self.style.fontSize, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
281 |
self.style.leading) |
253 | 282 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
283 |
for text in self.lines: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
284 |
tx.textLine(text) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
285 |
self.canv.drawText(tx) |
253 | 286 |
|
287 |
class Image(Flowable): |
|
2045 | 288 |
"""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
|
289 |
are supported. At the present time images as flowables are always centered horozontally |
2080 | 290 |
in the frame. We allow for two kinds of lazyness to allow for many images in a document |
291 |
which could lead to file handle starvation. |
|
292 |
lazy=1 don't open image until required. |
|
293 |
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
|
294 |
""" |
1917 | 295 |
_fixedWidth = 1 |
296 |
_fixedHeight = 1 |
|
2080 | 297 |
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
|
298 |
"""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
|
299 |
self.hAlign = 'CENTER' |
1880 | 300 |
self._mask = mask |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
301 |
# 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
|
302 |
# but we still need to know its size now |
2080 | 303 |
fp = hasattr(filename,'read') |
304 |
if fp: |
|
2237 | 305 |
self._file = filename |
2080 | 306 |
self.filename = `filename` |
307 |
else: |
|
2237 | 308 |
self._file = self.filename = filename |
2080 | 309 |
if not fp and os.path.splitext(filename)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']: |
2241 | 310 |
from reportlab.lib.utils import open_for_read |
311 |
f = open_for_read(filename, 'b') |
|
2080 | 312 |
info = pdfutils.readJPEGInfo(f) |
313 |
f.close() |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
314 |
self.imageWidth = info[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
315 |
self.imageHeight = info[1] |
2404
8bc8743cc5ec
flowables.py: fix no PIL jpg problem reported by Martin.Zohlhuber@tttech.com
rgbecker
parents:
2403
diff
changeset
|
316 |
self._img = None |
2080 | 317 |
self._setup(width,height,kind,0) |
318 |
elif fp: |
|
319 |
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
|
320 |
else: |
2080 | 321 |
self._setup(width,height,kind,lazy) |
1477 | 322 |
|
2080 | 323 |
def _setup(self,width,height,kind,lazy): |
324 |
self._lazy = lazy |
|
325 |
self._width = width |
|
326 |
self._height = height |
|
327 |
self._kind = kind |
|
328 |
if lazy<=0: self._setup_inner() |
|
329 |
||
330 |
def _setup_inner(self): |
|
331 |
width = self._width |
|
332 |
height = self._height |
|
333 |
kind = self._kind |
|
2404
8bc8743cc5ec
flowables.py: fix no PIL jpg problem reported by Martin.Zohlhuber@tttech.com
rgbecker
parents:
2403
diff
changeset
|
334 |
img = self._img |
8bc8743cc5ec
flowables.py: fix no PIL jpg problem reported by Martin.Zohlhuber@tttech.com
rgbecker
parents:
2403
diff
changeset
|
335 |
if img: self.imageWidth, self.imageHeight = img.getSize() |
2080 | 336 |
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
|
337 |
if kind in ['direct','absolute']: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
338 |
self.drawWidth = width or self.imageWidth |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
339 |
self.drawHeight = height or self.imageHeight |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
340 |
elif kind in ['percentage','%']: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
341 |
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
|
342 |
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
|
343 |
elif kind in ['bound','proportional']: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
344 |
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
|
345 |
self.drawWidth = self.imageWidth*factor |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
346 |
self.drawHeight = self.imageHeight*factor |
253 | 347 |
|
2080 | 348 |
def __getattr__(self,a): |
349 |
if a=='_img': |
|
350 |
from reportlab.lib.utils import ImageReader #this may raise an error |
|
2237 | 351 |
self._img = ImageReader(self._file) |
352 |
del self._file |
|
2080 | 353 |
return self._img |
354 |
elif a in ('drawWidth','drawHeight','imageWidth','imageHeight'): |
|
355 |
self._setup_inner() |
|
356 |
return self.__dict__[a] |
|
357 |
raise AttributeError(a) |
|
358 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
359 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
360 |
#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
|
361 |
return (self.drawWidth, self.drawHeight) |
253 | 362 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
363 |
def draw(self): |
2080 | 364 |
lazy = self._lazy |
365 |
if lazy>=2: self._lazy = 1 |
|
366 |
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
|
367 |
0, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
368 |
0, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
369 |
self.drawWidth, |
1880 | 370 |
self.drawHeight, |
1882 | 371 |
mask=self._mask, |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
372 |
) |
2080 | 373 |
if lazy>=2: |
374 |
self._img = None |
|
375 |
self._lazy = lazy |
|
1103 | 376 |
|
2192 | 377 |
def identity(self,maxLen=None): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
378 |
r = Flowable.identity(self,maxLen) |
2080 | 379 |
if r[-4:]=='>...' and type(self.filename) is StringType: |
380 |
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
|
381 |
return r |
1103 | 382 |
|
253 | 383 |
class Spacer(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
384 |
"""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
|
385 |
a gap between objects.""" |
1917 | 386 |
_fixedWidth = 1 |
387 |
_fixedHeight = 1 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
388 |
def __init__(self, width, height): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
389 |
self.width = width |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
390 |
self.height = height |
253 | 391 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
392 |
def __repr__(self): |
2387 | 393 |
return "%s(%s, %s)" % (self.__class__.__name__,self.width, self.height) |
253 | 394 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
395 |
def draw(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
396 |
pass |
253 | 397 |
|
307 | 398 |
class PageBreak(Spacer): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
399 |
"""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
|
400 |
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
|
401 |
def __init__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
402 |
pass |
1683 | 403 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
404 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
405 |
return "PageBreak()" |
253 | 406 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
407 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
408 |
self.width = availWidth |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
409 |
self.height = availHeight |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
410 |
return (availWidth,availHeight) #step back a point |
253 | 411 |
|
2408 | 412 |
class SlowPageBreak(PageBreak): |
413 |
pass |
|
414 |
||
307 | 415 |
class CondPageBreak(Spacer): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
416 |
"""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
|
417 |
def __init__(self, height): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
418 |
self.height = height |
1683 | 419 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
420 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
421 |
return "CondPageBreak(%s)" %(self.height,) |
307 | 422 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
423 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
424 |
if availHeight<self.height: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
425 |
return (availWidth, availHeight) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
426 |
return (0, 0) |
253 | 427 |
|
2525 | 428 |
def _listWrapOn(F,availWidth,canv,mergeSpace=1,obj=None): |
2375 | 429 |
'''return max width, required height for a list of flowables F''' |
430 |
W = 0 |
|
431 |
H = 0 |
|
432 |
pS = 0 |
|
2525 | 433 |
atTop = 1 |
434 |
for f in F: |
|
2375 | 435 |
w,h = f.wrapOn(canv,availWidth,0xfffffff) |
2525 | 436 |
if w<=_FUZZ or h<=_FUZZ: continue |
2375 | 437 |
W = max(W,w) |
2525 | 438 |
H += h |
439 |
if not atTop: |
|
2387 | 440 |
h = f.getSpaceBefore() |
441 |
if mergeSpace: H += max(h-pS,0) |
|
442 |
else: H += h |
|
2525 | 443 |
else: |
444 |
if obj is not None: obj._spaceBefore = f.getSpaceBefore() |
|
445 |
atTop = 0 |
|
446 |
pS = f.getSpaceAfter() |
|
447 |
H += pS |
|
448 |
if obj is not None: obj._spaceAfter = pS |
|
449 |
return W, H-pS |
|
2375 | 450 |
|
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2408
diff
changeset
|
451 |
def _flowableSublist(V): |
2392 | 452 |
"if it isn't a list or tuple, wrap it in a list" |
2375 | 453 |
if type(V) not in (ListType, TupleType): V = V is not None and [V] or [] |
2450 | 454 |
from doctemplate import LCActionFlowable |
2451 | 455 |
assert not [x for x in V if isinstance(x,LCActionFlowable)],'LCActionFlowables not allowed in sublists' |
2375 | 456 |
return V |
558 | 457 |
|
367 | 458 |
class KeepTogether(Flowable): |
1994 | 459 |
def __init__(self,flowables,maxHeight=None): |
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2408
diff
changeset
|
460 |
self._flowables = _flowableSublist(flowables) |
1994 | 461 |
self._maxHeight = maxHeight |
367 | 462 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
463 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
464 |
f = self._flowables |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
465 |
L = map(repr,f) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
466 |
import string |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
467 |
L = "\n"+string.join(L, "\n") |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
468 |
L = string.replace(L, "\n", "\n ") |
1994 | 469 |
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
|
470 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
471 |
def wrap(self, aW, aH): |
2375 | 472 |
W,H = _listWrapOn(self._flowables,aW,self.canv) |
1994 | 473 |
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
|
474 |
return W, 0xffffff # force a split |
367 | 475 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
476 |
def split(self, aW, aH): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
477 |
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
|
478 |
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
|
479 |
return S |
367 | 480 |
|
2461 | 481 |
def identity(self): |
482 |
return "<KeepTogether at %d> containing :" + "\n".join([f.identity() for f in self._flowables]) |
|
483 |
||
253 | 484 |
class Macro(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
485 |
"""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
|
486 |
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
|
487 |
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
|
488 |
def __init__(self, command): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
489 |
self.command = command |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
490 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
491 |
return "Macro(%s)" % repr(self.command) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
492 |
def wrap(self, availWidth, availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
493 |
return (0,0) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1554
diff
changeset
|
494 |
def draw(self): |
1768 | 495 |
exec self.command in globals(), {'canvas':self.canv} |
496 |
||
2403 | 497 |
class CallerMacro(Flowable): |
498 |
''' |
|
499 |
like Macro, but with callable command(s) |
|
500 |
drawCallable(self) |
|
501 |
wrapCallable(self,aW,aH) |
|
502 |
''' |
|
503 |
def __init__(self, drawCallable=None, wrapCallable=None): |
|
504 |
_ = lambda *args: None |
|
505 |
self._drawCallable = drawCallable or _ |
|
506 |
self._wrapCallable = wrapCallable or _ |
|
507 |
def __repr__(self): |
|
508 |
return "CallerMacro(%s)" % repr(self.command) |
|
509 |
def wrap(self, aW, aH): |
|
510 |
self._wrapCallable(self,aW,aH) |
|
511 |
return (0,0) |
|
512 |
def draw(self): |
|
513 |
self._drawCallable(self) |
|
514 |
||
1768 | 515 |
class ParagraphAndImage(Flowable): |
516 |
'''combine a Paragraph and an Image''' |
|
517 |
def __init__(self,P,I,xpad=3,ypad=3): |
|
518 |
self.P, self.style, self.I, self.xpad, self.ypad = P,P.style,I,xpad,ypad |
|
519 |
||
520 |
def wrap(self,availWidth,availHeight): |
|
521 |
wI, hI = self.I.wrap(availWidth,availHeight) |
|
522 |
self.wI, self.hI = wI, hI |
|
523 |
# work out widths array for breaking |
|
524 |
self.width = availWidth |
|
525 |
P, style, xpad, ypad = self.P, self.style, self.xpad, self.ypad |
|
526 |
leading = style.leading |
|
527 |
leftIndent = style.leftIndent |
|
528 |
later_widths = availWidth - leftIndent - style.rightIndent |
|
529 |
intermediate_widths = later_widths - xpad - wI |
|
530 |
first_line_width = intermediate_widths - style.firstLineIndent |
|
531 |
P.width = 0 |
|
532 |
P.blPara = P.breakLines([first_line_width] + int((hI+ypad)/leading)*[intermediate_widths]+[later_widths]) |
|
533 |
P.height = len(P.blPara.lines)*leading |
|
534 |
self.height = max(hI,P.height) |
|
535 |
return (self.width, self.height) |
|
536 |
||
537 |
def split(self,availWidth, availHeight): |
|
538 |
P, wI, hI, ypad = self.P, self.wI, self.hI, self.ypad |
|
539 |
if hI+ypad>availHeight or len(P.frags)<=0: return [] |
|
540 |
S = P.split(availWidth,availHeight) |
|
541 |
if not S: return S |
|
542 |
P = self.P = S[0] |
|
543 |
del S[0] |
|
544 |
style = self.style = P.style |
|
545 |
P.height = len(self.P.blPara.lines)*style.leading |
|
546 |
self.height = max(hI,P.height) |
|
547 |
return [self]+S |
|
548 |
||
549 |
def draw(self): |
|
550 |
canv = self.canv |
|
551 |
self.I.drawOn(canv,self.width-self.wI-self.xpad,self.height-self.hI) |
|
552 |
self.P.drawOn(canv,0,0) |
|
2113
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
553 |
|
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
554 |
class FailOnWrap(Flowable): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
555 |
def wrap(self, availWidth, availHeight): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
556 |
raise ValueError("FailOnWrap flowable wrapped and failing as ordered!") |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2192
diff
changeset
|
557 |
|
2113
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
558 |
def draw(self): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
559 |
pass |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
560 |
|
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
561 |
class FailOnDraw(Flowable): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
562 |
def wrap(self, availWidth, availHeight): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
563 |
return (0,0) |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2192
diff
changeset
|
564 |
|
2113
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
565 |
def draw(self): |
e82d8b3880d8
Preliminary support for tracing through doc builds
andy_robinson
parents:
2080
diff
changeset
|
566 |
raise ValueError("FailOnDraw flowable drawn, and failing as ordered!") |
2276 | 567 |
|
568 |
class HRFlowable(Flowable): |
|
569 |
'''Like the hr tag''' |
|
570 |
def __init__(self, |
|
571 |
width="80%", |
|
572 |
thickness=1, |
|
573 |
lineCap='round', |
|
574 |
color=lightgrey, |
|
575 |
spaceBefore=1, spaceAfter=1, |
|
2523
0473810aff11
platypus: fix up None defaults for table line commands, add HRFlowable dash arg
rgbecker
parents:
2461
diff
changeset
|
576 |
hAlign='CENTER', vAlign='BOTTOM', |
0473810aff11
platypus: fix up None defaults for table line commands, add HRFlowable dash arg
rgbecker
parents:
2461
diff
changeset
|
577 |
dash=None): |
2276 | 578 |
Flowable.__init__(self) |
579 |
self.width = width |
|
580 |
self.lineWidth = thickness |
|
581 |
self.lineCap=lineCap |
|
582 |
self.spaceBefore = spaceBefore |
|
583 |
self.spaceAfter = spaceAfter |
|
584 |
self.color = color |
|
585 |
self.hAlign = hAlign |
|
586 |
self.vAlign = vAlign |
|
2523
0473810aff11
platypus: fix up None defaults for table line commands, add HRFlowable dash arg
rgbecker
parents:
2461
diff
changeset
|
587 |
self.dash = dash |
2276 | 588 |
|
589 |
def __repr__(self): |
|
2284 | 590 |
return "HRFlowable(width=%s, height=%s)" % (self.width, self.height) |
2276 | 591 |
|
592 |
def wrap(self, availWidth, availHeight): |
|
593 |
w = self.width |
|
594 |
if type(w) is type(''): |
|
595 |
w = w.strip() |
|
596 |
if w.endswith('%'): w = availWidth*float(w[:-1])*0.01 |
|
597 |
else: w = float(w) |
|
598 |
w = min(w,availWidth) |
|
599 |
self._width = w |
|
600 |
return w, self.lineWidth |
|
601 |
||
602 |
def draw(self): |
|
603 |
canv = self.canv |
|
604 |
canv.saveState() |
|
605 |
canv.setLineWidth(self.lineWidth) |
|
606 |
canv.setLineCap({'butt':0,'round':1, 'square': 2}[self.lineCap.lower()]) |
|
607 |
canv.setStrokeColor(self.color) |
|
2523
0473810aff11
platypus: fix up None defaults for table line commands, add HRFlowable dash arg
rgbecker
parents:
2461
diff
changeset
|
608 |
if self.dash: canv.setDash(self.dash) |
2276 | 609 |
canv.line(0, 0, self._width, self.height) |
610 |
canv.restoreState() |
|
2375 | 611 |
|
2387 | 612 |
class _PTOInfo: |
613 |
def __init__(self,trailer,header): |
|
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2408
diff
changeset
|
614 |
self.trailer = _flowableSublist(trailer) |
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2408
diff
changeset
|
615 |
self.header = _flowableSublist(header) |
2387 | 616 |
|
2525 | 617 |
class _Container: #Abstract some common container like behaviour |
618 |
def getSpaceBefore(self): |
|
619 |
for c in self._content: |
|
620 |
if not hasattr(c,'frameAction'): |
|
621 |
return c.getSpaceBefore() |
|
622 |
return 0 |
|
623 |
||
624 |
def getSpaceAfter(self): |
|
625 |
for c in reversed(self._content): |
|
626 |
if not hasattr(c,'frameAction'): |
|
627 |
return c.getSpaceAfter() |
|
628 |
return 0 |
|
629 |
||
630 |
def drawOn(self, canv, x, y, _sW=0,scale=1.0): |
|
631 |
'''we simulate being added to a frame''' |
|
632 |
pS = 0 |
|
633 |
aW = scale*(self.width+_sW) |
|
634 |
C = self._content |
|
635 |
y += self.height*scale |
|
636 |
for c in C: |
|
637 |
w, h = c.wrapOn(canv,aW,0xfffffff) |
|
638 |
if w<_FUZZ or h<_FUZZ: continue |
|
639 |
if c is not C[0]: h += max(c.getSpaceBefore()-pS,0) |
|
640 |
y -= h |
|
641 |
c.drawOn(canv,x,y,_sW=aW-w) |
|
642 |
if c is not C[-1]: |
|
643 |
pS = c.getSpaceAfter() |
|
644 |
y -= pS |
|
645 |
||
646 |
class PTOContainer(_Container,Flowable): |
|
2375 | 647 |
'''PTOContainer(contentList,trailerList,headerList) |
648 |
||
649 |
A container for flowables decorated with trailer & header lists. |
|
650 |
If the split operation would be called then the trailer and header |
|
651 |
lists are injected before and after the split. This allows specialist |
|
652 |
"please turn over" and "continued from previous" like behaviours.''' |
|
653 |
def __init__(self,content,trailer=None,header=None): |
|
2387 | 654 |
I = _PTOInfo(trailer,header) |
655 |
self._content = C = [] |
|
2449
47b15f941325
platypus: attempt to make KeepTogether/keepWithNext more robust
rgbecker
parents:
2408
diff
changeset
|
656 |
for _ in _flowableSublist(content): |
2387 | 657 |
if isinstance(_,PTOContainer): |
658 |
C.extend(_._content) |
|
659 |
else: |
|
660 |
C.append(_) |
|
661 |
if not hasattr(_,'_ptoinfo'): _._ptoinfo = I |
|
2375 | 662 |
|
663 |
def wrap(self,availWidth,availHeight): |
|
664 |
self.width, self.height = _listWrapOn(self._content,availWidth,self.canv) |
|
2384 | 665 |
return self.width,self.height |
2375 | 666 |
|
667 |
def split(self, availWidth, availHeight): |
|
668 |
canv = self.canv |
|
669 |
C = self._content |
|
2387 | 670 |
x = i = H = pS = 0 |
671 |
n = len(C) |
|
672 |
I2W = {} |
|
673 |
for x in xrange(n): |
|
674 |
c = C[x] |
|
675 |
I = c._ptoinfo |
|
2388 | 676 |
if I not in I2W.keys(): |
2387 | 677 |
T = I.trailer |
678 |
Hdr = I.header |
|
679 |
tW, tH = _listWrapOn(T, availWidth, self.canv) |
|
680 |
tSB = T[0].getSpaceBefore() |
|
681 |
I2W[I] = T,tW,tH,tSB |
|
682 |
else: |
|
683 |
T,tW,tH,tSB = I2W[I] |
|
2384 | 684 |
_, h = c.wrapOn(canv,availWidth,0xfffffff) |
2387 | 685 |
if x: h += max(c.getSpaceBefore()-pS,0) |
2384 | 686 |
pS = c.getSpaceAfter() |
687 |
H += h+pS |
|
2375 | 688 |
if H+tH+max(tSB,pS)>=availHeight-_FUZZ: break |
2384 | 689 |
i += 1 |
2375 | 690 |
|
691 |
#first retract last thing we tried |
|
2384 | 692 |
H -= (h+pS) |
2375 | 693 |
|
694 |
#attempt a sub split on the last one we have |
|
2384 | 695 |
aH = (availHeight - H - max(pS,tSB) - tH)*0.99 |
2375 | 696 |
if aH>=0.05*availHeight: |
2387 | 697 |
SS = c.splitOn(canv,availWidth,aH) |
2375 | 698 |
else: |
699 |
SS = [] |
|
2387 | 700 |
if SS: |
701 |
from doctemplate import FrameBreak |
|
702 |
F = [FrameBreak()] |
|
2376 | 703 |
|
2375 | 704 |
if SS: |
2387 | 705 |
R1 = C[:i] + SS[:1] + T + F |
2376 | 706 |
R2 = Hdr + SS[1:]+C[i+1:] |
2375 | 707 |
elif not i: |
708 |
return [] |
|
709 |
else: |
|
710 |
R1 = C[:i-1]+T |
|
2376 | 711 |
R2 = Hdr + C[i:] |
2387 | 712 |
return R1 + [PTOContainer(R2,deepcopy(I.trailer),deepcopy(I.header))] |
2375 | 713 |
|
2525 | 714 |
#utility functions used by FrameFlowable |
715 |
def _hmodel(s0,s1,h0,h1): |
|
716 |
# calculate the parameters in the model |
|
717 |
# h = a/s**2 + b/s |
|
718 |
a11 = 1./s0**2 |
|
719 |
a12 = 1./s0 |
|
720 |
a21 = 1./s1**2 |
|
721 |
a22 = 1./s1 |
|
722 |
det = a11*a22-a12*a21 |
|
723 |
b11 = a22/det |
|
724 |
b12 = -a12/det |
|
725 |
b21 = -a21/det |
|
726 |
b22 = a11/det |
|
727 |
a = b11*h0+b12*h1 |
|
728 |
b = b21*h0+b22*h1 |
|
729 |
return a,b |
|
730 |
||
731 |
def _qsolve(h,(a,b)): |
|
732 |
'''solve the model v = a/s**2 + b/s for an s which gives us v==h''' |
|
733 |
t = 0.5*b/a |
|
734 |
from math import sqrt |
|
735 |
f = -h/a |
|
736 |
r = t*t-f |
|
737 |
if r<0: return None |
|
738 |
r = sqrt(r) |
|
739 |
if t>=0: |
|
740 |
s1 = -t - r |
|
741 |
else: |
|
742 |
s1 = -t + r |
|
743 |
s2 = f/s1 |
|
744 |
return max(1./s1, 1./s2) |
|
745 |
||
746 |
class FrameFlowable(_Container,Flowable): |
|
747 |
def __init__(self, maxWidth, maxHeight, content=[], mergeSpace=None, mode=0, id=None): |
|
748 |
'''mode describes the action to take when overflowing |
|
749 |
0 raise an error in the normal way |
|
750 |
1 ignore ie just draw it and report maxWidth, maxHeight |
|
751 |
2 shrinkToFit |
|
752 |
''' |
|
753 |
self.id = id |
|
754 |
self.maxWidth = maxWidth |
|
755 |
self.maxHeight = maxHeight |
|
756 |
self.mode = mode |
|
757 |
assert mode in (0,1,2), '%s invalid mode value %s' % (self.identity(),mode) |
|
758 |
if mergeSpace is None: mergeSpace = overlapAttachedSpace |
|
759 |
self.mergespace = mergeSpace |
|
760 |
self._content = content |
|
761 |
||
762 |
def _getAvailableWidth(self): |
|
763 |
return self.maxWidth - self._leftExtraIndent - self._rightExtraIndent |
|
764 |
||
765 |
def identity(self, maxLen=None): |
|
766 |
return "<%s at %d%s> size=%sx%s" % (self.__class__.__name__, id(self), self.id and ' id="%s"'%self.id, fp_str(self.maxWidth),fp_str(self.maxHeight)) |
|
767 |
||
768 |
def wrap(self,availWidth,availHeight): |
|
769 |
mode = self.mode |
|
770 |
maxWidth = float(self.maxWidth) |
|
771 |
maxHeight = float(self.maxHeight) |
|
772 |
W, H = _listWrapOn(self._content,availWidth,self.canv) |
|
773 |
if mode==0 or (W<=maxWidth and H<=maxHeight): |
|
774 |
self.width = W #we take what we get |
|
775 |
self.height = H |
|
776 |
elif mode==1: #we lie |
|
777 |
self.width = min(maxWidth,W)-_FUZZ |
|
778 |
self.height = min(maxHeight,H)-_FUZZ |
|
779 |
else: |
|
780 |
def func(x): |
|
781 |
W, H = _listWrapOn(self._content,x*availWidth,self.canv) |
|
782 |
W /= x |
|
783 |
H /= x |
|
784 |
return W, H |
|
785 |
W0 = W |
|
786 |
H0 = H |
|
787 |
s0 = 1 |
|
788 |
if W>maxWidth: |
|
789 |
#squeeze out the excess width |
|
790 |
s1 = W/maxWidth |
|
791 |
W, H = func(s1) |
|
792 |
if H<=maxHeight: |
|
793 |
self.width = W |
|
794 |
self.height = H |
|
795 |
self._scale = s1 |
|
796 |
return W,H |
|
797 |
s0 = s1 |
|
798 |
H0 = H |
|
799 |
W0 = W |
|
800 |
s1 = H/maxHeight |
|
801 |
W, H = func(s1) |
|
802 |
self.width = W |
|
803 |
self.height = H |
|
804 |
self._scale = s1 |
|
805 |
if H<min(0.95*maxHeight,maxHeight-10): |
|
806 |
#the standard case W should be OK, H is short we want |
|
807 |
#to find the smallest s with H<=maxHeight |
|
808 |
H1 = H |
|
809 |
for f in 0, 0.01, 0.05, 0.10, 0.15: |
|
810 |
#apply the quadratic model |
|
811 |
s = _qsolve(maxHeight*(1-f),_hmodel(s0,s1,H0,H1)) |
|
812 |
W, H = func(s) |
|
813 |
if H<=maxHeight: |
|
814 |
self.width = W |
|
815 |
self.height = H |
|
816 |
self._scale = s |
|
817 |
break |
|
818 |
||
819 |
return self.width, self.height |
|
820 |
||
2375 | 821 |
def drawOn(self, canv, x, y, _sW=0): |
2525 | 822 |
scale = getattr(self,'_scale',1.0) |
823 |
if scale!=1.0: |
|
824 |
canv.saveState() |
|
825 |
canv.translate(x,y) |
|
826 |
x=y=0 |
|
827 |
canv.scale(1.0/scale, 1.0/scale) |
|
828 |
_Container.drawOn(self, canv, x, y, _sW=_sW, scale=scale) |
|
829 |
if scale!=1.0: |
|
830 |
canv.restoreState() |