author | rgbecker |
Tue, 12 Apr 2005 17:23:17 +0000 | |
changeset 2475 | 24a8e6ffc36a |
parent 2474 | 5462fb8b792d |
child 2481 | 5620fd504c16 |
permissions | -rwxr-xr-x |
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/tables.py |
4 |
__version__=''' $Id$ ''' |
|
2190 | 5 |
|
16 | 6 |
__doc__=""" |
6 | 7 |
Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in |
8 |
row order. Drawing of the table can be controlled by using a TableStyle instance. This allows control of the |
|
9 |
color and weight of the lines (if any), and the font, alignment and padding of the text. |
|
268 | 10 |
|
327 | 11 |
None values in the sequence of row heights or column widths, mean that the corresponding rows |
12 |
or columns should be automatically sized. |
|
13 |
||
14 |
All the cell values should be convertible to strings; embedded newline '\\n' characters |
|
15 |
cause the value to wrap (ie are like a traditional linefeed). |
|
16 |
||
268 | 17 |
See the test output from running this module as a script for a discussion of the method for constructing |
18 |
tables and table styles. |
|
6 | 19 |
""" |
2289 | 20 |
from reportlab.platypus.flowables import Flowable, Preformatted |
1207
49a514d10cb0
make tables one point off not bomb mysteriously in paid production installations.
aaron_watters
parents:
1169
diff
changeset
|
21 |
from reportlab import rl_config |
2384 | 22 |
from reportlab.lib.styles import PropertySet, ParagraphStyle |
168
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
23 |
from reportlab.lib import colors |
1103 | 24 |
from reportlab.lib.utils import fp_str |
403 | 25 |
from reportlab.pdfbase import pdfmetrics |
312 | 26 |
import operator, string |
2468 | 27 |
from types import TupleType, ListType, StringType, FloatType, IntType |
6 | 28 |
|
128 | 29 |
class CellStyle(PropertySet): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
30 |
defaults = { |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
31 |
'fontname':'Times-Roman', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
32 |
'fontsize':10, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
33 |
'leading':12, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
34 |
'leftPadding':6, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
35 |
'rightPadding':6, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
36 |
'topPadding':3, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
37 |
'bottomPadding':3, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
38 |
'firstLineIndent':0, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
39 |
'color':colors.black, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
40 |
'alignment': 'LEFT', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
41 |
'background': (1,1,1), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
42 |
'valign': 'BOTTOM', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
43 |
} |
6 | 44 |
|
2469 | 45 |
LINECAPS={None: None, 'butt':0,'round':1,'projecting':2,'squared':2} |
46 |
LINEJOINS={None: None, 'miter':0, 'mitre':0, 'round':1,'bevel':2} |
|
1699 | 47 |
|
730
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
48 |
# experimental replacement |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
49 |
class CellStyle1(PropertySet): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
50 |
fontname = "Times-Roman" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
51 |
fontsize = 10 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
52 |
leading = 12 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
53 |
leftPadding = 6 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
54 |
rightPadding = 6 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
55 |
topPadding = 3 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
56 |
bottomPadding = 3 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
57 |
firstLineIndent = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
58 |
color = colors.black |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
59 |
alignment = 'LEFT' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
60 |
background = (1,1,1) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
61 |
valign = "BOTTOM" |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
62 |
def __init__(self, name, parent=None): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
63 |
self.name = name |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
64 |
if parent is not None: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
65 |
parent.copy(self) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
66 |
def copy(self, result=None): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
67 |
if result is None: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
68 |
result = CellStyle1() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
69 |
for name in dir(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
70 |
setattr(result, name, gettattr(self, name)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
71 |
return result |
730
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
72 |
CellStyle = CellStyle1 |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
73 |
|
6 | 74 |
class TableStyle: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
75 |
def __init__(self, cmds=None, parent=None, **kw): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
76 |
#handle inheritance from parent first. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
77 |
commands = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
78 |
if parent: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
79 |
# copy the parents list at construction time |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
80 |
commands = commands + parent.getCommands() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
81 |
self._opts = parent._opts |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
82 |
if cmds: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
83 |
commands = commands + list(cmds) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
84 |
self._cmds = commands |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
85 |
self._opts={} |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
86 |
self._opts.update(kw) |
1435
4d0f57749e18
Added _opts to TableStyle for possible keepWithNext
rgbecker
parents:
1253
diff
changeset
|
87 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
88 |
def add(self, *cmd): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
89 |
self._cmds.append(cmd) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
90 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
91 |
L = map(repr, self._cmds) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
92 |
import string |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
93 |
L = string.join(L, " \n") |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
94 |
return "TableStyle(\n%s\n) # end TableStyle" % L |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
95 |
def getCommands(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
96 |
return self._cmds |
338 | 97 |
|
98 |
TableStyleType = type(TableStyle()) |
|
419 | 99 |
_SeqTypes = (TupleType, ListType) |
356 | 100 |
|
101 |
def _rowLen(x): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
102 |
return type(x) not in _SeqTypes and 1 or len(x) |
419 | 103 |
|
2271 | 104 |
def _calc_pc(V,avail): |
105 |
'''check list V for percentage or * values |
|
106 |
1) absolute values go through unchanged |
|
107 |
2) percentages are used as weights for unconsumed space |
|
108 |
3) if no None values were seen '*' weights are |
|
109 |
set equally with unclaimed space |
|
110 |
otherwise * weights are assigned as None''' |
|
111 |
R = [] |
|
112 |
r = R.append |
|
113 |
I = [] |
|
114 |
i = I.append |
|
115 |
J = [] |
|
116 |
j = J.append |
|
117 |
s = avail |
|
118 |
w = n = 0. |
|
119 |
for v in V: |
|
120 |
if type(v) is type(""): |
|
121 |
v = v.strip() |
|
122 |
if not v: |
|
123 |
v = None |
|
124 |
n += 1 |
|
125 |
elif v.endswith('%'): |
|
126 |
v = float(v[:-1]) |
|
127 |
w += v |
|
128 |
i(len(R)) |
|
129 |
elif v=='*': |
|
130 |
j(len(R)) |
|
131 |
else: |
|
132 |
v = float(v) |
|
2284 | 133 |
s -= v |
2271 | 134 |
elif v is None: |
135 |
n += 1 |
|
136 |
else: |
|
137 |
s -= v |
|
138 |
r(v) |
|
139 |
s = max(0.,s) |
|
140 |
f = s/max(100.,w) |
|
141 |
for i in I: |
|
142 |
R[i] *= f |
|
143 |
s -= R[i] |
|
144 |
s = max(0.,s) |
|
145 |
m = len(J) |
|
146 |
if m: |
|
147 |
v = n==0 and s/m or None |
|
148 |
for j in J: |
|
149 |
R[j] = v |
|
150 |
return R |
|
356 | 151 |
|
2412
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
152 |
def _hLine(canvLine, scp, ecp, y, hBlocks, FUZZ=rl_config._FUZZ): |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
153 |
''' |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
154 |
Draw horizontal lines; do not draw through regions specified in hBlocks |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
155 |
This also serves for vertical lines with a suitable canvLine |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
156 |
''' |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
157 |
if hBlocks: hBlocks = hBlocks.get(y,None) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
158 |
if not hBlocks or scp>=hBlocks[-1][1]-FUZZ or ecp<=hBlocks[0][0]+FUZZ: |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
159 |
canvLine(scp,y,ecp,y) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
160 |
else: |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
161 |
i = 0 |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
162 |
n = len(hBlocks) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
163 |
while scp<ecp-FUZZ and i<n: |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
164 |
x0, x1 = hBlocks[i] |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
165 |
if x1<=scp+FUZZ or x0>=ecp-FUZZ: |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
166 |
i += 1 |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
167 |
continue |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
168 |
i0 = max(scp,x0) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
169 |
i1 = min(ecp,x1) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
170 |
if i0>scp: canvLine(scp,y,i0,y) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
171 |
scp = i1 |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
172 |
if scp<ecp-FUZZ: canvLine(scp,y,ecp,y) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
173 |
|
2414
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
174 |
def _multiLine(scp,ecp,y,canvLine,ws,count): |
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
175 |
offset = 0.5*(count-1)*ws |
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
176 |
y += offset |
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
177 |
for idx in xrange(count): |
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
178 |
canvLine(scp, y, ecp, y) |
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
179 |
y -= ws |
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
180 |
|
2469 | 181 |
def _convert2int(value, map, low, high, name, cmd): |
182 |
'''private converter tries map(value) low<=int(value)<=high or finally an error''' |
|
183 |
try: |
|
184 |
return map[value] |
|
185 |
except KeyError: |
|
186 |
try: |
|
187 |
ivalue = int(value) |
|
188 |
if low<=ivalue<=high: return ivalue |
|
189 |
except: |
|
190 |
pass |
|
191 |
raise ValueError('Bad %s value %s in %s'%(name,value,str(cmd))) |
|
192 |
||
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
193 |
def _endswith(obj,s): |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
194 |
try: |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
195 |
return obj.endswith(s) |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
196 |
except: |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
197 |
return 0 |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
198 |
|
221 | 199 |
class Table(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
200 |
def __init__(self, data, colWidths=None, rowHeights=None, style=None, |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
201 |
repeatRows=0, repeatCols=0, splitByRow=1, emptyTableAction=None, ident=None): |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
202 |
self.ident = ident |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
203 |
self.hAlign = 'CENTER' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
204 |
self.vAlign = 'MIDDLE' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
205 |
if type(data) not in _SeqTypes: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
206 |
raise ValueError, "%s invalid data type" % self.identity() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
207 |
self._nrows = nrows = len(data) |
2221 | 208 |
self._cellvalues = [] |
2401 | 209 |
_seqCW = type(colWidths) in _SeqTypes |
210 |
_seqRH = type(rowHeights) in _SeqTypes |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
211 |
if nrows: self._ncols = ncols = max(map(_rowLen,data)) |
2401 | 212 |
elif colWidths and _seqCW: ncols = len(colWidths) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
213 |
else: ncols = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
214 |
if not emptyTableAction: emptyTableAction = rl_config.emptyTableAction |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
215 |
if not (nrows and ncols): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
216 |
if emptyTableAction=='error': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
217 |
raise ValueError, "%s must have at least a row and column" % self.identity() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
218 |
elif emptyTableAction=='indicate': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
219 |
self.__class__ = Preformatted |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
220 |
global _emptyTableStyle |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
221 |
if '_emptyTableStyle' not in globals().keys(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
222 |
_emptyTableStyle = ParagraphStyle('_emptyTableStyle') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
223 |
_emptyTableStyle.textColor = colors.red |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
224 |
_emptyTableStyle.backColor = colors.yellow |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
225 |
Preformatted.__init__(self,'%s(%d,%d)' % (self.__class__.__name__,nrows,ncols), _emptyTableStyle) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
226 |
elif emptyTableAction=='ignore': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
227 |
self.__class__ = Spacer |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
228 |
Spacer.__init__(self,0,0) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
229 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
230 |
raise ValueError, '%s bad emptyTableAction: "%s"' % (self.identity(),emptyTableAction) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
231 |
return |
1533 | 232 |
|
2299
d757841f2006
Fix proposed by Derik Barclay <dbarclay@givex.com>
rgbecker
parents:
2289
diff
changeset
|
233 |
self._cellvalues = data |
2401 | 234 |
if not _seqCW: colWidths = ncols*[colWidths] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
235 |
elif len(colWidths) != ncols: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
236 |
raise ValueError, "%s data error - %d columns in data but %d in grid" % (self.identity(),ncols, len(colWidths)) |
2401 | 237 |
if not _seqRH: rowHeights = nrows*[rowHeights] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
238 |
elif len(rowHeights) != nrows: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
239 |
raise ValueError, "%s data error - %d rows in data but %d in grid" % (self.identity(),nrows, len(rowHeights)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
240 |
for i in range(nrows): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
241 |
if len(data[i]) != ncols: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
242 |
raise ValueError, "%s not enough data points in row %d!" % (self.identity(),i) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
243 |
self._rowHeights = self._argH = rowHeights |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
244 |
self._colWidths = self._argW = colWidths |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
245 |
cellrows = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
246 |
for i in range(nrows): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
247 |
cellcols = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
248 |
for j in range(ncols): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
249 |
cellcols.append(CellStyle(`(i,j)`)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
250 |
cellrows.append(cellcols) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
251 |
self._cellStyles = cellrows |
350 | 252 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
253 |
self._bkgrndcmds = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
254 |
self._linecmds = [] |
1883 | 255 |
self._spanCmds = [] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
256 |
self.repeatRows = repeatRows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
257 |
self.repeatCols = repeatCols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
258 |
self.splitByRow = splitByRow |
6 | 259 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
260 |
if style: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
261 |
self.setStyle(style) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
262 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
263 |
"incomplete, but better than nothing" |
2389 | 264 |
r = getattr(self,'_rowHeights','[unknown]') |
265 |
c = getattr(self,'_colWidths','[unknown]') |
|
266 |
cv = getattr(self,'_cellvalues','[unknown]') |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
267 |
import pprint, string |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
268 |
cv = pprint.pformat(cv) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
269 |
cv = string.replace(cv, "\n", "\n ") |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
270 |
return "%s(\n rowHeights=%s,\n colWidths=%s,\n%s\n) # end table" % (self.__class__.__name__,r,c,cv) |
342 | 271 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
272 |
def identity(self, maxLen=30): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
273 |
'''Identify our selves as well as possible''' |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
274 |
if self.ident: return self.ident |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
275 |
vx = None |
1989 | 276 |
nr = getattr(self,'_nrows','unknown') |
277 |
nc = getattr(self,'_ncols','unknown') |
|
2299
d757841f2006
Fix proposed by Derik Barclay <dbarclay@givex.com>
rgbecker
parents:
2289
diff
changeset
|
278 |
cv = getattr(self,'_cellvalues',None) |
1989 | 279 |
if cv and 'unknown' not in (nr,nc): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
280 |
b = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
281 |
for i in xrange(nr): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
282 |
for j in xrange(nc): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
283 |
v = cv[i][j] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
284 |
t = type(v) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
285 |
if t in _SeqTypes or isinstance(v,Flowable): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
286 |
if not t in _SeqTypes: v = (v,) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
287 |
r = '' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
288 |
for vij in v: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
289 |
r = vij.identity(maxLen) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
290 |
if r and r[-4:]!='>...': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
291 |
break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
292 |
if r and r[-4:]!='>...': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
293 |
ix, jx, vx, b = i, j, r, 1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
294 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
295 |
v = v is None and '' or str(v) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
296 |
ix, jx, vx = i, j, v |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
297 |
b = (vx and t is StringType) and 1 or 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
298 |
if maxLen: vx = vx[:maxLen] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
299 |
if b: break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
300 |
if b: break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
301 |
if vx: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
302 |
vx = ' with cell(%d,%d) containing\n%s' % (ix,jx,repr(vx)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
303 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
304 |
vx = '...' |
1103 | 305 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
306 |
return "<%s at %d %d rows x %s cols>%s" % (self.__class__.__name__, id(self), nr, nc, vx) |
1103 | 307 |
|
2189 | 308 |
def _listCellGeom(self, V,w,s,W=None,H=None,aH=72000): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
309 |
aW = w-s.leftPadding-s.rightPadding |
2189 | 310 |
aH = aH - s.topPadding - s.bottomPadding |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
311 |
t = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
312 |
w = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
313 |
canv = getattr(self,'canv',None) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
314 |
for v in V: |
2189 | 315 |
vw, vh = v.wrapOn(canv,aW, aH) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
316 |
if W is not None: W.append(vw) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
317 |
if H is not None: H.append(vh) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
318 |
w = max(w,vw) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
319 |
t = t + vh + v.getSpaceBefore()+v.getSpaceAfter() |
2463
67935cd21f54
tables.py: empty cellval patch from Jean-Francois Gosset
rgbecker
parents:
2443
diff
changeset
|
320 |
else: |
67935cd21f54
tables.py: empty cellval patch from Jean-Francois Gosset
rgbecker
parents:
2443
diff
changeset
|
321 |
return w, t |
1683 | 322 |
return w, t - V[0].getSpaceBefore()-V[-1].getSpaceAfter() |
1492 | 323 |
|
2271 | 324 |
def _calc_width(self,availWidth,W=None): |
2289 | 325 |
if getattr(self,'_width_calculated_once',None): return |
326 |
#comments added by Andy to Robin's slightly terse variable names |
|
2271 | 327 |
if not W: W = _calc_pc(self._argW,availWidth) #widths array |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
328 |
if None in W: #some column widths are not given |
2289 | 329 |
canv = getattr(self,'canv',None) |
330 |
saved = None |
|
331 |
colSpanCells = self._spanCmds and self._colSpanCells or () |
|
2277 | 332 |
if W is self._argW: W = W[:] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
333 |
while None in W: |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
334 |
j = W.index(None) #find first unspecified column |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
335 |
f = lambda x,j=j: operator.getitem(x,j) |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
336 |
V = map(f,self._cellvalues) #values for this column |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
337 |
S = map(f,self._cellStyles) #styles for this column |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
338 |
w = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
339 |
i = 0 |
2190 | 340 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
341 |
for v, s in map(None, V, S): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
342 |
#if the current cell is part of a spanned region, |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
343 |
#assume a zero size. |
2289 | 344 |
if (j, i) in colSpanCells: |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
345 |
t = 0.0 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
346 |
else:#work out size |
1917 | 347 |
t = self._elementWidth(v,s) |
348 |
if t is None: |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
349 |
raise ValueError, "Flowable %s in cell(%d,%d) can't have auto width\n%s" % (v.identity(30),i,j,self.identity(30)) |
1917 | 350 |
t = t + s.leftPadding+s.rightPadding |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
351 |
if t>w: w = t #record a new maximum |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
352 |
i = i + 1 |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
353 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
354 |
W[j] = w |
1596
753865895a6f
Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents:
1536
diff
changeset
|
355 |
|
2277 | 356 |
self._colWidths = W |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
357 |
width = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
358 |
self._colpositions = [0] #index -1 is right side boundary; we skip when processing cells |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
359 |
for w in W: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
360 |
width = width + w |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
361 |
self._colpositions.append(width) |
1683 | 362 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
363 |
self._width = width |
2289 | 364 |
self._width_calculated_once = 1 |
1596
753865895a6f
Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents:
1536
diff
changeset
|
365 |
|
1917 | 366 |
def _elementWidth(self,v,s): |
367 |
t = type(v) |
|
368 |
if t in _SeqTypes: |
|
369 |
w = 0 |
|
370 |
for e in v: |
|
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
371 |
ew = self._elementWidth(e,s) |
1917 | 372 |
if ew is None: return None |
373 |
w = max(w,ew) |
|
374 |
return w |
|
375 |
elif isinstance(v,Flowable) and v._fixedWidth: |
|
2471
76c745e075b2
tables.py: be evn more robust about width/drawWidth etc
rgbecker
parents:
2470
diff
changeset
|
376 |
if hasattr(v, 'width') and type(v.width) in (IntType,FloatType): return v.width |
76c745e075b2
tables.py: be evn more robust about width/drawWidth etc
rgbecker
parents:
2470
diff
changeset
|
377 |
if hasattr(v, 'drawWidth') and type(v.drawWidth) in (IntType,FloatType): return v.drawWidth |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
378 |
# Even if something is fixedWidth, the attribute to check is not |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
379 |
# necessarily consistent (cf. Image.drawWidth). Therefore, we'll |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
380 |
# be extra-careful and fall through to this code if necessary. |
2470
74a7b9da41e4
tables.py: fix for case when minWidth fails du eot attribute error
rgbecker
parents:
2469
diff
changeset
|
381 |
if hasattr(v, 'minWidth'): |
74a7b9da41e4
tables.py: fix for case when minWidth fails du eot attribute error
rgbecker
parents:
2469
diff
changeset
|
382 |
try: |
2471
76c745e075b2
tables.py: be evn more robust about width/drawWidth etc
rgbecker
parents:
2470
diff
changeset
|
383 |
w = v.minWidth() # should be all flowables |
76c745e075b2
tables.py: be evn more robust about width/drawWidth etc
rgbecker
parents:
2470
diff
changeset
|
384 |
if type(w) in (FloatType,IntType): return w |
2470
74a7b9da41e4
tables.py: fix for case when minWidth fails du eot attribute error
rgbecker
parents:
2469
diff
changeset
|
385 |
except AttributeError: |
74a7b9da41e4
tables.py: fix for case when minWidth fails du eot attribute error
rgbecker
parents:
2469
diff
changeset
|
386 |
pass |
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
387 |
v = string.split(v is not None and str(v) or '', "\n") |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
388 |
return max(map(lambda a, b=s.fontname, c=s.fontsize,d=pdfmetrics.stringWidth: d(a,b,c), v)) |
1917 | 389 |
|
2271 | 390 |
def _calc_height(self, availHeight, availWidth, H=None, W=None): |
333 | 391 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
392 |
H = self._argH |
2271 | 393 |
if not W: W = _calc_pc(self._argW,availWidth) #widths array |
6 | 394 |
|
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
395 |
hmax = lim = len(H) |
2436 | 396 |
longTable = getattr(self,'_longTableOptimize',rl_config.longTableOptimize) |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
397 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
398 |
if None in H: |
2289 | 399 |
canv = getattr(self,'canv',None) |
400 |
saved = None |
|
401 |
#get a handy list of any cells which span rows. should be ignored for sizing |
|
402 |
if self._spanCmds: |
|
403 |
rowSpanCells = self._rowSpanCells |
|
404 |
colSpanCells = self._colSpanCells |
|
405 |
spanRanges = self._spanRanges |
|
406 |
colpositions = self._colpositions |
|
407 |
else: |
|
408 |
rowSpanCells = colSpanCells = () |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
409 |
if canv: saved = canv._fontname, canv._fontsize, canv._leading |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
410 |
H = H[:] #make a copy as we'll change it |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
411 |
self._rowHeights = H |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
412 |
while None in H: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
413 |
i = H.index(None) |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
414 |
if longTable: |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
415 |
hmax = i |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
416 |
height = reduce(operator.add, H[:i], 0) |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
417 |
# we can stop if we have filled up all available room |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
418 |
if height > availHeight: break |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
419 |
V = self._cellvalues[i] # values for row i |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
420 |
S = self._cellStyles[i] # styles for row i |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
421 |
h = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
422 |
j = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
423 |
for v, s, w in map(None, V, S, W): # value, style, width (lengths must match) |
2289 | 424 |
ji = j,i |
425 |
if ji in rowSpanCells: |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
426 |
t = 0.0 # don't count it, it's either occluded or unreliable |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
427 |
else: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
428 |
t = type(v) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
429 |
if t in _SeqTypes or isinstance(v,Flowable): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
430 |
if not t in _SeqTypes: v = (v,) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
431 |
if w is None: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
432 |
raise ValueError, "Flowable %s in cell(%d,%d) can't have auto width in\n%s" % (v[0].identity(30),i,j,self.identity(30)) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
433 |
if canv: canv._fontname, canv._fontsize, canv._leading = s.fontname, s.fontsize, s.leading or 1.2*s.fontsize |
2289 | 434 |
if ji in colSpanCells: |
435 |
t = spanRanges[ji] |
|
436 |
w = max(colpositions[t[2]+1]-colpositions[t[0]],w) |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
437 |
dW,t = self._listCellGeom(v,w,s) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
438 |
if canv: canv._fontname, canv._fontsize, canv._leading = saved |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
439 |
dW = dW + s.leftPadding + s.rightPadding |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
440 |
if not rl_config.allowTableBoundsErrors and dW>w: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
441 |
raise "LayoutError", "Flowable %s (%sx%s points) too wide for cell(%d,%d) (%sx* points) in\n%s" % (v[0].identity(30),fp_str(dW),fp_str(t),i,j, fp_str(w), self.identity(30)) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
442 |
else: |
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
443 |
v = string.split(v is not None and str(v) or '', "\n") |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
444 |
t = s.leading*len(v) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
445 |
t = t+s.bottomPadding+s.topPadding |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
446 |
if t>h: h = t #record a new maximum |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
447 |
j = j + 1 |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
448 |
H[i] = h |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2194
diff
changeset
|
449 |
if None not in H: hmax = lim |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
450 |
|
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
451 |
height = self._height = reduce(operator.add, H[:hmax], 0) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
452 |
self._rowpositions = [height] # index 0 is actually topline; we skip when processing cells |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
453 |
for h in H[:hmax]: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
454 |
height = height - h |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
455 |
self._rowpositions.append(height) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
456 |
assert abs(height)<1e-8, 'Internal height error' |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
457 |
self._hmax = hmax |
1596
753865895a6f
Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents:
1536
diff
changeset
|
458 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
459 |
def _calc(self, availWidth, availHeight): |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
460 |
#if hasattr(self,'_width'): return |
1596
753865895a6f
Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents:
1536
diff
changeset
|
461 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
462 |
#in some cases there are unsizable things in |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
463 |
#cells. If so, apply a different algorithm |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
464 |
#and assign some withs in a less (thanks to Gary Poster) dumb way. |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
465 |
#this CHANGES the widths array. |
2284 | 466 |
if (None in self._colWidths or '*' in self._colWidths) and self._hasVariWidthElements(): |
2271 | 467 |
W = self._calcPreliminaryWidths(availWidth) #widths |
468 |
else: |
|
469 |
W = None |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
470 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
471 |
# need to know which cells are part of spanned |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
472 |
# ranges, so _calc_height and _calc_width can ignore them |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
473 |
# in sizing |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
474 |
if self._spanCmds: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
475 |
self._calcSpanRanges() |
2289 | 476 |
if None in self._argH: |
477 |
self._calc_width(availWidth,W=W) |
|
2190 | 478 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
479 |
# calculate the full table height |
2271 | 480 |
self._calc_height(availHeight,availWidth,W=W) |
1596
753865895a6f
Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents:
1536
diff
changeset
|
481 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
482 |
# calculate the full table width |
2271 | 483 |
self._calc_width(availWidth,W=W) |
2190 | 484 |
|
1883 | 485 |
if self._spanCmds: |
2289 | 486 |
#now work out the actual rect for each spanned cell from the underlying grid |
1883 | 487 |
self._calcSpanRects() |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
488 |
|
1917 | 489 |
def _hasVariWidthElements(self, upToRow=None): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
490 |
"""Check for flowables in table cells and warn up front. |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
491 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
492 |
Allow a couple which we know are fixed size such as |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
493 |
images and graphics.""" |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
494 |
bad = 0 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
495 |
if upToRow is None: upToRow = self._nrows |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
496 |
for row in range(min(self._nrows, upToRow)): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
497 |
for col in range(self._ncols): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
498 |
value = self._cellvalues[row][col] |
1917 | 499 |
if not self._canGetWidth(value): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
500 |
bad = 1 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
501 |
#raise Exception('Unsizable elements found at row %d column %d in table with content:\n %s' % (row, col, value)) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
502 |
return bad |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
503 |
|
1917 | 504 |
def _canGetWidth(self, thing): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
505 |
"Can we work out the width quickly?" |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
506 |
if type(thing) in (ListType, TupleType): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
507 |
for elem in thing: |
1917 | 508 |
if not self._canGetWidth(elem): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
509 |
return 0 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
510 |
return 1 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
511 |
elif isinstance(thing, Flowable): |
1917 | 512 |
return thing._fixedWidth # must loosen this up |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
513 |
else: #string, number, None etc. |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
514 |
#anything else gets passed to str(...) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
515 |
# so should be sizable |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
516 |
return 1 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
517 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
518 |
def _calcPreliminaryWidths(self, availWidth): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
519 |
"""Fallback algorithm for when main one fails. |
1883 | 520 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
521 |
Where exact width info not given but things like |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
522 |
paragraphs might be present, do a preliminary scan |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
523 |
and assign some best-guess values.""" |
2271 | 524 |
|
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
525 |
W = list(self._argW) # _calc_pc(self._argW,availWidth) |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
526 |
verbose = 0 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
527 |
totalDefined = 0.0 |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
528 |
percentDefined = 0 |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
529 |
percentTotal = 0 |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
530 |
numberUndefined = 0 |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
531 |
numberGreedyUndefined = 0 |
2271 | 532 |
for w in W: |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
533 |
if w is None: |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
534 |
numberUndefined += 1 |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
535 |
elif w == '*': |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
536 |
numberUndefined += 1 |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
537 |
numberGreedyUndefined += 1 |
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
538 |
elif _endswith(w,'%'): |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
539 |
percentDefined += 1 |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
540 |
percentTotal += float(w[:-1]) |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
541 |
else: |
2468 | 542 |
assert type(w) in (IntType, FloatType) |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
543 |
totalDefined = totalDefined + w |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
544 |
if verbose: print 'prelim width calculation. %d columns, %d undefined width, %0.2f units remain' % ( |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
545 |
self._ncols, numberUndefined, availWidth - totalDefined) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
546 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
547 |
#check columnwise in each None column to see if they are sizable. |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
548 |
given = [] |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
549 |
sizeable = [] |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
550 |
unsizeable = [] |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
551 |
minimums = {} |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
552 |
totalMinimum = 0 |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
553 |
elementWidth = self._elementWidth |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
554 |
for colNo in range(self._ncols): |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
555 |
w = W[colNo] |
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
556 |
if w is None or w=='*' or _endswith(w,'%'): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
557 |
siz = 1 |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
558 |
current = final = None |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
559 |
for rowNo in range(self._nrows): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
560 |
value = self._cellvalues[rowNo][colNo] |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
561 |
style = self._cellStyles[rowNo][colNo] |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
562 |
new = elementWidth(value,style)+style.leftPadding+style.rightPadding |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
563 |
final = max(current, new) |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
564 |
current = new |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
565 |
siz = siz and self._canGetWidth(value) # irrelevant now? |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
566 |
if siz: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
567 |
sizeable.append(colNo) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
568 |
else: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
569 |
unsizeable.append(colNo) |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
570 |
minimums[colNo] = final |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
571 |
totalMinimum += final |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
572 |
else: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
573 |
given.append(colNo) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
574 |
if len(given) == self._ncols: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
575 |
return |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
576 |
if verbose: print 'predefined width: ',given |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
577 |
if verbose: print 'uncomputable width: ',unsizeable |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
578 |
if verbose: print 'computable width: ',sizeable |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
579 |
|
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
580 |
# how much width is left: |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
581 |
remaining = availWidth - (totalMinimum + totalDefined) |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
582 |
if remaining > 0: |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
583 |
# we have some room left; fill it. |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
584 |
definedPercentage = (totalDefined/availWidth)*100 |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
585 |
percentTotal += definedPercentage |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
586 |
if numberUndefined and percentTotal < 100: |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
587 |
undefined = numberGreedyUndefined or numberUndefined |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
588 |
defaultWeight = (100-percentTotal)/undefined |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
589 |
percentTotal = 100 |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
590 |
defaultDesired = (defaultWeight/percentTotal)*availWidth |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
591 |
else: |
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
592 |
defaultWeight = defaultDesired = 1 |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
593 |
# we now calculate how wide each column wanted to be, and then |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
594 |
# proportionately shrink that down to fit the remaining available |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
595 |
# space. A column may not shrink less than its minimum width, |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
596 |
# however, which makes this a bit more complicated. |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
597 |
desiredWidths = [] |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
598 |
totalDesired = 0 |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
599 |
effectiveRemaining = remaining |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
600 |
for colNo, minimum in minimums.items(): |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
601 |
w = W[colNo] |
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
602 |
if _endswith(w,'%'): |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
603 |
desired = (float(w[:-1])/percentTotal)*availWidth |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
604 |
elif w == '*': |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
605 |
desired = defaultDesired |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
606 |
else: |
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
607 |
desired = not numberGreedyUndefined and defaultDesired or 1 |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
608 |
if desired <= minimum: |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
609 |
W[colNo] = minimum |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
610 |
else: |
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
611 |
desiredWidths.append( |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
612 |
(desired-minimum, minimum, desired, colNo)) |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
613 |
totalDesired += desired |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
614 |
effectiveRemaining += minimum |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
615 |
if desiredWidths: # else we're done |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
616 |
# let's say we have two variable columns. One wanted |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
617 |
# 88 points, and one wanted 264 points. The first has a |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
618 |
# minWidth of 66, and the second of 55. We have 71 points |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
619 |
# to divide up in addition to the totalMinimum (i.e., |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
620 |
# remaining==71). Our algorithm tries to keep the proportion |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
621 |
# of these variable columns. |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
622 |
# |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
623 |
# To do this, we add up the minimum widths of the variable |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
624 |
# columns and the remaining width. That's 192. We add up the |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
625 |
# totalDesired width. That's 352. That means we'll try to |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
626 |
# shrink the widths by a proportion of 192/352--.545454. |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
627 |
# That would make the first column 48 points, and the second |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
628 |
# 144 points--adding up to the desired 192. |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
629 |
# |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
630 |
# Unfortunately, that's too small for the first column. It |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
631 |
# must be 66 points. Therefore, we go ahead and save that |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
632 |
# column width as 88 points. That leaves (192-88==) 104 |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
633 |
# points remaining. The proportion to shrink the remaining |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
634 |
# column is (104/264), which, multiplied by the desired |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
635 |
# width of 264, is 104: the amount assigned to the remaining |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
636 |
# column. |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
637 |
proportion = effectiveRemaining/totalDesired |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
638 |
# we sort the desired widths by difference between desired and |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
639 |
# and minimum values, a value called "disappointment" in the |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
640 |
# code. This means that the columns with a bigger |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
641 |
# disappointment will have a better chance of getting more of |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
642 |
# the available space. |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
643 |
desiredWidths.sort() |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
644 |
finalSet = [] |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
645 |
for disappointment, minimum, desired, colNo in desiredWidths: |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
646 |
adjusted = proportion * desired |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
647 |
if adjusted < minimum: |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
648 |
W[colNo] = minimum |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
649 |
totalDesired -= desired |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
650 |
effectiveRemaining -= minimum |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
651 |
if totalDesired: |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
652 |
proportion = effectiveRemaining/totalDesired |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
653 |
else: |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
654 |
finalSet.append((minimum, desired, colNo)) |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
655 |
for minimum, desired, colNo in finalSet: |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
656 |
adjusted = proportion * desired |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
657 |
assert adjusted >= minimum |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
658 |
W[colNo] = adjusted |
2467
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
659 |
else: |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
660 |
for colNo, minimum in minimums.items(): |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
661 |
W[colNo] = minimum |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
662 |
if verbose: print 'new widths are:', W |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
663 |
self._argW = self._colWidths = W |
5630a2b21a90
tables.py, paragraph.py: Gary Poster's layout improvement patch
rgbecker
parents:
2465
diff
changeset
|
664 |
return W |
2190 | 665 |
|
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
666 |
def minWidth(self): |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
667 |
W = list(self._argW) |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
668 |
width = 0 |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
669 |
elementWidth = self._elementWidth |
2474 | 670 |
rowNos = xrange(self._nrows) |
671 |
values = self._cellvalues |
|
672 |
styles = self._cellStyles |
|
673 |
for colNo in xrange(len(W)): |
|
674 |
w = W[colNo] |
|
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
675 |
if w is None or w=='*' or _endswith(w,'%'): |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
676 |
final = 0 |
2474 | 677 |
for rowNo in rowNos: |
678 |
value = values[rowNo][colNo] |
|
679 |
style = styles[rowNo][colNo] |
|
2472
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
680 |
new = (elementWidth(value,style)+ |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
681 |
style.leftPadding+style.rightPadding) |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
682 |
final = max(final, new) |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
683 |
width += final |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
684 |
else: |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
685 |
width += float(w) |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
686 |
return width # XXX + 1/2*(left and right border widths) |
6795e616cdbe
tables.py: added in Gary Poster's gary@zope.com latest patch
rgbecker
parents:
2471
diff
changeset
|
687 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
688 |
def _calcSpanRanges(self): |
1883 | 689 |
"""Work out rects for tables which do row and column spanning. |
690 |
||
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
691 |
This creates some mappings to let the later code determine |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
692 |
if a cell is part of a "spanned" range. |
1883 | 693 |
self._spanRanges shows the 'coords' in integers of each |
694 |
'cell range', or None if it was clobbered: |
|
695 |
(col, row) -> (col0, row0, col1, row1) |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
696 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
697 |
Any cell not in the key is not part of a spanned region |
1883 | 698 |
""" |
2289 | 699 |
self._spanRanges = spanRanges = {} |
700 |
for x in xrange(self._ncols): |
|
701 |
for y in xrange(self._nrows): |
|
702 |
spanRanges[x,y] = (x, y, x, y) |
|
703 |
self._colSpanCells = [] |
|
704 |
self._rowSpanCells = [] |
|
705 |
csa = self._colSpanCells.append |
|
706 |
rsa = self._rowSpanCells.append |
|
1883 | 707 |
for (cmd, start, stop) in self._spanCmds: |
708 |
x0, y0 = start |
|
709 |
x1, y1 = stop |
|
710 |
||
711 |
#normalize |
|
712 |
if x0 < 0: x0 = x0 + self._ncols |
|
713 |
if x1 < 0: x1 = x1 + self._ncols |
|
714 |
if y0 < 0: y0 = y0 + self._nrows |
|
715 |
if y1 < 0: y1 = y1 + self._nrows |
|
2289 | 716 |
if x0 > x1: x0, x1 = x1, x0 |
717 |
if y0 > y1: y0, y1 = y1, y0 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
718 |
|
2289 | 719 |
if x0!=x1 or y0!=y1: |
720 |
#column span |
|
721 |
if x0!=x1: |
|
722 |
for y in xrange(y0, y1+1): |
|
723 |
for x in xrange(x0,x1+1): |
|
724 |
csa((x,y)) |
|
725 |
#row span |
|
726 |
if y0!=y1: |
|
727 |
for y in xrange(y0, y1+1): |
|
728 |
for x in xrange(x0,x1+1): |
|
729 |
rsa((x,y)) |
|
2190 | 730 |
|
2289 | 731 |
for y in xrange(y0, y1+1): |
732 |
for x in xrange(x0,x1+1): |
|
733 |
spanRanges[x,y] = None |
|
734 |
# set the main entry |
|
735 |
spanRanges[x0,y0] = (x0, y0, x1, y1) |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
736 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
737 |
def _calcSpanRects(self): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
738 |
"""Work out rects for tables which do row and column spanning. |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
739 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
740 |
Based on self._spanRanges, which is already known, |
2190 | 741 |
and the widths which were given or previously calculated, |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
742 |
self._spanRects shows the real coords for drawing: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
743 |
(col, row) -> (x, y, width, height) |
2190 | 744 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
745 |
for each cell. Any cell which 'does not exist' as another |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
746 |
has spanned over it will get a None entry on the right |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
747 |
""" |
2289 | 748 |
if getattr(self,'_spanRects',None): return |
2412
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
749 |
colpositions = self._colpositions |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
750 |
rowpositions = self._rowpositions |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
751 |
self._spanRects = spanRects = {} |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
752 |
self._vBlocks = vBlocks = {} |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
753 |
self._hBlocks = hBlocks = {} |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
754 |
for (coord, value) in self._spanRanges.items(): |
1883 | 755 |
if value is None: |
756 |
spanRects[coord] = None |
|
757 |
else: |
|
758 |
col,row = coord |
|
759 |
col0, row0, col1, row1 = value |
|
2412
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
760 |
if col1-col0>0: |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
761 |
for _ in xrange(col0+1,col1+1): |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
762 |
vBlocks.setdefault(colpositions[_],[]).append((rowpositions[row1+1],rowpositions[row0])) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
763 |
if row1-row0>0: |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
764 |
for _ in xrange(row0+1,row1+1): |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
765 |
hBlocks.setdefault(rowpositions[_],[]).append((colpositions[col0],colpositions[col1+1])) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
766 |
x = colpositions[col0] |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
767 |
y = rowpositions[row1+1] |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
768 |
width = colpositions[col1+1] - x |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
769 |
height = rowpositions[row0] - y |
1883 | 770 |
spanRects[coord] = (x, y, width, height) |
2190 | 771 |
|
2412
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
772 |
for _ in hBlocks, vBlocks: |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
773 |
for value in _.values(): |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
774 |
value.sort() |
2190 | 775 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
776 |
def setStyle(self, tblstyle): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
777 |
if type(tblstyle) is not TableStyleType: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
778 |
tblstyle = TableStyle(tblstyle) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
779 |
for cmd in tblstyle.getCommands(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
780 |
self._addCommand(cmd) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
781 |
for k,v in tblstyle._opts.items(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
782 |
setattr(self,k,v) |
350 | 783 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
784 |
def _addCommand(self,cmd): |
2393 | 785 |
if cmd[0] in ('BACKGROUND','ROWBACKGROUNDS','COLBACKGROUNDS'): |
2392 | 786 |
self._bkgrndcmds.append(cmd) |
1883 | 787 |
elif cmd[0] == 'SPAN': |
788 |
self._spanCmds.append(cmd) |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
789 |
elif _isLineCommand(cmd): |
1699 | 790 |
# we expect op, start, stop, weight, colour, cap, dashes, join |
791 |
cmd = tuple(cmd) |
|
792 |
if len(cmd)<5: raise ValueError('bad line command '+str(cmd)) |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
793 |
|
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
794 |
#determine line cap value at position 5. This can be string or numeric. |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
795 |
if len(cmd)<6: |
2414
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
796 |
cmd = cmd+(1,) |
1699 | 797 |
else: |
2469 | 798 |
cap = _convert2int(cmd[5], LINECAPS, 0, 2, 'cap', cmd) |
799 |
cmd = cmd[:5]+(cap,)+cmd[6:] |
|
2464
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
800 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
801 |
#dashes at index 6 - this is a dash array: |
2464
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
802 |
if len(cmd)<7: cmd += (None,) |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
803 |
|
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
804 |
#join mode at index 7 - can be string or numeric, look up as for caps |
1699 | 805 |
if len(cmd)<8: cmd = cmd+(1,) |
806 |
else: |
|
2469 | 807 |
join = _convert2int(cmd[7], LINEJOINS, 0, 2, 'join', cmd) |
808 |
cmd = cmd[:7]+(join,)+cmd[8:] |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
809 |
|
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
810 |
#linecount at index 8. Default is 1, set to 2 for double line. |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
811 |
if len(cmd)<9: |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
812 |
lineCount = 1 |
2464
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
813 |
cmd += (lineCount,) |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
814 |
else: |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
815 |
lineCount = cmd[8] |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
816 |
assert lineCount >= 1 |
2221 | 817 |
#linespacing at index 9. Not applicable unless 2+ lines, defaults to line |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
818 |
#width so you get a visible gap between centres |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
819 |
if len(cmd)<10: cmd = cmd + (cmd[3],) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
820 |
|
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
821 |
assert len(cmd) == 10 |
2414
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
822 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
823 |
self._linecmds.append(cmd) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
824 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
825 |
(op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
826 |
if sc < 0: sc = sc + self._ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
827 |
if ec < 0: ec = ec + self._ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
828 |
if sr < 0: sr = sr + self._nrows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
829 |
if er < 0: er = er + self._nrows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
830 |
for i in range(sr, er+1): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
831 |
for j in range(sc, ec+1): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
832 |
_setCellStyle(self._cellStyles, i, j, op, values) |
326 | 833 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
834 |
def _drawLines(self): |
1699 | 835 |
ccap, cdash, cjoin = None, None, None |
836 |
self.canv.saveState() |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
837 |
for op, (sc,sr), (ec,er), weight, color, cap, dash, join, count, space in self._linecmds: |
2185 | 838 |
if type(sr) is type('') and sr.startswith('split'): continue |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
839 |
if sc < 0: sc = sc + self._ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
840 |
if ec < 0: ec = ec + self._ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
841 |
if sr < 0: sr = sr + self._nrows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
842 |
if er < 0: er = er + self._nrows |
2414
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
843 |
if cap!=None and ccap!=cap: |
1699 | 844 |
self.canv.setLineCap(cap) |
845 |
ccap = cap |
|
2464
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
846 |
if dash is None or dash == []: |
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
847 |
if cdash is not None: |
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
848 |
self.canv.setDash() |
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
849 |
cdash = None |
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
850 |
elif dash != cdash: |
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
851 |
self.canv.setDash(dash) |
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
852 |
cdash = dash |
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
853 |
if join is not None and cjoin!=join: |
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
854 |
self.canv.setLineJoin(join) |
09cfec86fa5a
tables.py: fixes to join/cap/dash from Gary Poster
rgbecker
parents:
2463
diff
changeset
|
855 |
cjoin = join |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
856 |
getattr(self,_LineOpMap.get(op, '_drawUnknown' ))( (sc, sr), (ec, er), weight, color, count, space) |
1699 | 857 |
self.canv.restoreState() |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
858 |
self._curcolor = None |
248 | 859 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
860 |
def _drawUnknown(self, (sc, sr), (ec, er), weight, color, count, space): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
861 |
raise ValueError, "Unknown line command '%s'" % op |
403 | 862 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
863 |
def _drawGrid(self, (sc, sr), (ec, er), weight, color, count, space): |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
864 |
self._drawBox( (sc, sr), (ec, er), weight, color, count, space) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
865 |
self._drawInnerGrid( (sc, sr), (ec, er), weight, color, count, space) |
403 | 866 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
867 |
def _drawBox(self, (sc, sr), (ec, er), weight, color, count, space): |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
868 |
self._drawHLines((sc, sr), (ec, sr), weight, color, count, space) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
869 |
self._drawHLines((sc, er+1), (ec, er+1), weight, color, count, space) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
870 |
self._drawVLines((sc, sr), (sc, er), weight, color, count, space) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
871 |
self._drawVLines((ec+1, sr), (ec+1, er), weight, color, count, space) |
350 | 872 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
873 |
def _drawInnerGrid(self, (sc, sr), (ec, er), weight, color, count, space): |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
874 |
self._drawHLines((sc, sr+1), (ec, er), weight, color, count, space) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
875 |
self._drawVLines((sc+1, sr), (ec, er), weight, color, count, space) |
350 | 876 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
877 |
def _prepLine(self, weight, color): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
878 |
if color != self._curcolor: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
879 |
self.canv.setStrokeColor(color) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
880 |
self._curcolor = color |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
881 |
if weight != self._curweight: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
882 |
self.canv.setLineWidth(weight) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
883 |
self._curweight = weight |
350 | 884 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
885 |
def _drawHLines(self, (sc, sr), (ec, er), weight, color, count, space): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
886 |
ecp = self._colpositions[sc:ec+2] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
887 |
rp = self._rowpositions[sr:er+1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
888 |
if len(ecp)<=1 or len(rp)<1: return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
889 |
self._prepLine(weight, color) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
890 |
scp = ecp[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
891 |
ecp = ecp[-1] |
2412
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
892 |
hBlocks = getattr(self,'_hBlocks',{}) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
893 |
canvLine = self.canv.line |
2220 | 894 |
if count == 1: |
2412
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
895 |
for y in rp: |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
896 |
_hLine(canvLine, scp, ecp, y, hBlocks) |
2220 | 897 |
else: |
2414
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
898 |
lf = lambda x0,y0,x1,y1,canvLine=canvLine, ws=weight+space, count=count: _multiLine(x0,x1,y0,canvLine,ws,count) |
2412
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
899 |
for y in rp: |
2414
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
900 |
_hLine(lf, scp, ecp, y, hBlocks) |
403 | 901 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
902 |
def _drawHLinesB(self, (sc, sr), (ec, er), weight, color, count, space): |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
903 |
self._drawHLines((sc, sr+1), (ec, er+1), weight, color, count, space) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
904 |
|
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
905 |
def _drawVLines(self, (sc, sr), (ec, er), weight, color, count, space): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
906 |
erp = self._rowpositions[sr:er+2] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
907 |
cp = self._colpositions[sc:ec+1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
908 |
if len(erp)<=1 or len(cp)<1: return |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
909 |
self._prepLine(weight, color) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
910 |
srp = erp[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
911 |
erp = erp[-1] |
2412
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
912 |
vBlocks = getattr(self,'_vBlocks',{}) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
913 |
canvLine = lambda y0, x0, y1, x1, _line=self.canv.line: _line(x0,y0,x1,y1) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
914 |
if count == 1: |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
915 |
for x in cp: |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
916 |
_hLine(canvLine, erp, srp, x, vBlocks) |
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
917 |
else: |
2414
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
918 |
lf = lambda x0,y0,x1,y1,canvLine=canvLine, ws=weight+space, count=count: _multiLine(x0,x1,y0,canvLine,ws,count) |
2412
b8bf2e639769
reportlab: table improvments and better documentation
rgbecker
parents:
2401
diff
changeset
|
919 |
for x in cp: |
2414
81be267fa92c
tables.py: extend span line blocking to multi lines
rgbecker
parents:
2412
diff
changeset
|
920 |
_hLine(lf, erp, srp, x, vBlocks) |
326 | 921 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
922 |
def _drawVLinesA(self, (sc, sr), (ec, er), weight, color, count, space): |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
923 |
self._drawVLines((sc+1, sr), (ec+1, er), weight, color, count, space) |
403 | 924 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
925 |
def wrap(self, availWidth, availHeight): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
926 |
self._calc(availWidth, availHeight) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
927 |
#nice and easy, since they are predetermined size |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
928 |
self.availWidth = availWidth |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
929 |
return (self._width, self._height) |
1495 | 930 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
931 |
def onSplit(self,T,byRow=1): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
932 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
933 |
This method will be called when the Table is split. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
934 |
Special purpose tables can override to do special stuff. |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
935 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
936 |
pass |
419 | 937 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
938 |
def _cr_0(self,n,cmds): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
939 |
for c in cmds: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
940 |
c = tuple(c) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
941 |
(sc,sr), (ec,er) = c[1:3] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
942 |
if sr>=n: continue |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
943 |
if er>=n: er = n-1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
944 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
350 | 945 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
946 |
def _cr_1_1(self,n,repeatRows, cmds): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
947 |
for c in cmds: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
948 |
c = tuple(c) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
949 |
(sc,sr), (ec,er) = c[1:3] |
2185 | 950 |
if sr in ('splitfirst','splitlast'): self._addCommand(c) |
951 |
else: |
|
952 |
if sr>=0 and sr>=repeatRows and sr<n and er>=0 and er<n: continue |
|
953 |
if sr>=repeatRows and sr<n: sr=repeatRows |
|
954 |
elif sr>=repeatRows and sr>=n: sr=sr+repeatRows-n |
|
955 |
if er>=repeatRows and er<n: er=repeatRows |
|
956 |
elif er>=repeatRows and er>=n: er=er+repeatRows-n |
|
957 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
|
350 | 958 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
959 |
def _cr_1_0(self,n,cmds): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
960 |
for c in cmds: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
961 |
c = tuple(c) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
962 |
(sc,sr), (ec,er) = c[1:3] |
2185 | 963 |
if sr in ('splitfirst','splitlast'): self._addCommand(c) |
964 |
else: |
|
965 |
if er>=0 and er<n: continue |
|
966 |
if sr>=0 and sr<n: sr=0 |
|
967 |
if sr>=n: sr = sr-n |
|
968 |
if er>=n: er = er-n |
|
969 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
|
350 | 970 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
971 |
def _splitRows(self,availHeight): |
2443
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
972 |
n=self._getFirstPossibleSplitRowPosition(availHeight) |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
973 |
if n<=self.repeatRows: return [] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
974 |
lim = len(self._rowHeights) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
975 |
if n==lim: return [self] |
350 | 976 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
977 |
repeatRows = self.repeatRows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
978 |
repeatCols = self.repeatCols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
979 |
splitByRow = self.splitByRow |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
980 |
data = self._cellvalues |
350 | 981 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
982 |
#we're going to split into two superRows |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
983 |
#R0 = slelf.__class__( data[:n], self._argW, self._argH[:n], |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
984 |
R0 = self.__class__( data[:n], self._colWidths, self._argH[:n], |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
985 |
repeatRows=repeatRows, repeatCols=repeatCols, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
986 |
splitByRow=splitByRow) |
350 | 987 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
988 |
#copy the styles and commands |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
989 |
R0._cellStyles = self._cellStyles[:n] |
419 | 990 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
991 |
A = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
992 |
# hack up the line commands |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
993 |
for op, (sc,sr), (ec,er), weight, color, cap, dash, join, count, space in self._linecmds: |
2185 | 994 |
if type(sr)is type('') and sr.startswith('split'): |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
995 |
A.append((op,(sc,sr), (ec,sr), weight, color, cap, dash, join, count, space)) |
2185 | 996 |
if sr=='splitlast': |
997 |
sr = er = n-1 |
|
998 |
elif sr=='splitfirst': |
|
999 |
sr = n |
|
1000 |
er = n |
|
1001 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1002 |
if sc < 0: sc = sc + self._ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1003 |
if ec < 0: ec = ec + self._ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1004 |
if sr < 0: sr = sr + self._nrows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1005 |
if er < 0: er = er + self._nrows |
419 | 1006 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1007 |
if op in ('BOX','OUTLINE','GRID'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1008 |
if sr<n and er>=n: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1009 |
# we have to split the BOX |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1010 |
A.append(('LINEABOVE',(sc,sr), (ec,sr), weight, color, cap, dash, join, count, space)) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1011 |
A.append(('LINEBEFORE',(sc,sr), (sc,er), weight, color, cap, dash, join, count, space)) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1012 |
A.append(('LINEAFTER',(ec,sr), (ec,er), weight, color, cap, dash, join, count, space)) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1013 |
A.append(('LINEBELOW',(sc,er), (ec,er), weight, color, cap, dash, join, count, space)) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1014 |
if op=='GRID': |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1015 |
A.append(('LINEBELOW',(sc,n-1), (ec,n-1), weight, color, cap, dash, join, count, space)) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1016 |
A.append(('LINEABOVE',(sc,n), (ec,n), weight, color, cap, dash, join, count, space)) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1017 |
A.append(('INNERGRID',(sc,sr), (ec,er), weight, color, cap, dash, join, count, space)) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1018 |
else: |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1019 |
A.append((op,(sc,sr), (ec,er), weight, color, cap, dash, join, count, space)) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1020 |
elif op in ('INNERGRID','LINEABOVE'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1021 |
if sr<n and er>=n: |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1022 |
A.append(('LINEBELOW',(sc,n-1), (ec,n-1), weight, color, cap, dash, join, count, space)) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1023 |
A.append(('LINEABOVE',(sc,n), (ec,n), weight, color, cap, dash, join, count, space)) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1024 |
A.append((op,(sc,sr), (ec,er), weight, color, cap, dash, join, count, space)) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1025 |
elif op == 'LINEBELOW': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1026 |
if sr<n and er>=(n-1): |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1027 |
A.append(('LINEABOVE',(sc,n), (ec,n), weight, color, cap, dash, join, count, space)) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1028 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1029 |
elif op == 'LINEABOVE': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1030 |
if sr<=n and er>=n: |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1031 |
A.append(('LINEBELOW',(sc,n-1), (ec,n-1), weight, color, cap, dash, join, count, space)) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1032 |
A.append((op,(sc,sr), (ec,er), weight, color, cap, dash, join, count, space)) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1033 |
else: |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1034 |
A.append((op,(sc,sr), (ec,er), weight, color, cap, dash, join, count, space)) |
419 | 1035 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1036 |
R0._cr_0(n,A) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1037 |
R0._cr_0(n,self._bkgrndcmds) |
2475 | 1038 |
R0._cr_0(n,self._spanCmds) |
350 | 1039 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1040 |
if repeatRows: |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
1041 |
#R1 = slelf.__class__(data[:repeatRows]+data[n:],self._argW, |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
1042 |
R1 = self.__class__(data[:repeatRows]+data[n:],self._colWidths, |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1043 |
self._argH[:repeatRows]+self._argH[n:], |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1044 |
repeatRows=repeatRows, repeatCols=repeatCols, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1045 |
splitByRow=splitByRow) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1046 |
R1._cellStyles = self._cellStyles[:repeatRows]+self._cellStyles[n:] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1047 |
R1._cr_1_1(n,repeatRows,A) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1048 |
R1._cr_1_1(n,repeatRows,self._bkgrndcmds) |
2475 | 1049 |
R1._cr_1_1(n,repeatRows,self._spanCmds) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1050 |
else: |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
1051 |
#R1 = slelf.__class__(data[n:], self._argW, self._argH[n:], |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
1052 |
R1 = self.__class__(data[n:], self._colWidths, self._argH[n:], |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1053 |
repeatRows=repeatRows, repeatCols=repeatCols, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1054 |
splitByRow=splitByRow) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1055 |
R1._cellStyles = self._cellStyles[n:] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1056 |
R1._cr_1_0(n,A) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1057 |
R1._cr_1_0(n,self._bkgrndcmds) |
2475 | 1058 |
R1._cr_1_0(n,self._spanCmds) |
350 | 1059 |
|
1498
2e7cfa1159cb
Copy alignments accross split (thanks R�diger M�hl <ruediger.maehl@web.de>
rgbecker
parents:
1495
diff
changeset
|
1060 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1061 |
R0.hAlign = R1.hAlign = self.hAlign |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1062 |
R0.vAlign = R1.vAlign = self.vAlign |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1063 |
self.onSplit(R0) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1064 |
self.onSplit(R1) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1065 |
return [R0,R1] |
350 | 1066 |
|
2443
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1067 |
def _getFirstPossibleSplitRowPosition(self,availHeight): |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1068 |
if self._spanCmds: |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1069 |
impossible={} |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1070 |
for xy in self._rowSpanCells: |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1071 |
r=self._spanRanges[xy] |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1072 |
if r!=None: |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1073 |
y1,y2=r[1],r[3] |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1074 |
if y1!=y2: |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1075 |
ymin=min(y1,y2) #normalize |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1076 |
ymax=max(y1,y2) #normalize |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1077 |
y=ymin+1 |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1078 |
while 1: |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1079 |
if y>ymax: break |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1080 |
impossible[y]=None #split at position y is impossible because of overlapping rowspan |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1081 |
y=y+1 |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1082 |
else: |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1083 |
impossible={} # any split possible because table does *not* have rowspans |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1084 |
h = 0 |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1085 |
n = 1 |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1086 |
split_at = 0 # from this point of view 0 is the first position where the table may *always* be splitted |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1087 |
for rh in self._rowHeights: |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1088 |
if h+rh>availHeight: |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1089 |
break |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1090 |
if not impossible.has_key(n): |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1091 |
split_at=n |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1092 |
h=h+rh |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1093 |
n=n+1 |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1094 |
return split_at |
41262322cc3d
tables.py: added span rows split handling & test contributed by Andre Reitz
rgbecker
parents:
2436
diff
changeset
|
1095 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1096 |
def split(self, availWidth, availHeight): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
1097 |
self._calc(availWidth, availHeight) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1098 |
if self.splitByRow: |
2012 | 1099 |
if not rl_config.allowTableBoundsErrors and self._width>availWidth: return [] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1100 |
return self._splitRows(availHeight) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1101 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1102 |
raise NotImplementedError |
403 | 1103 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1104 |
def draw(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1105 |
self._curweight = self._curcolor = self._curcellstyle = None |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1106 |
self._drawBkgrnd() |
1883 | 1107 |
if self._spanCmds == []: |
1108 |
# old fashioned case, no spanning, steam on and do each cell |
|
1109 |
for row, rowstyle, rowpos, rowheight in map(None, self._cellvalues, self._cellStyles, self._rowpositions[1:], self._rowHeights): |
|
1110 |
for cellval, cellstyle, colpos, colwidth in map(None, row, rowstyle, self._colpositions[:-1], self._colWidths): |
|
1111 |
self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)) |
|
1112 |
else: |
|
1113 |
# we have some row or col spans, need a more complex algorithm |
|
1114 |
# to find the rect for each |
|
1115 |
for rowNo in range(self._nrows): |
|
1116 |
for colNo in range(self._ncols): |
|
1117 |
cellRect = self._spanRects[colNo, rowNo] |
|
1118 |
if cellRect is not None: |
|
1119 |
(x, y, width, height) = cellRect |
|
1120 |
cellval = self._cellvalues[rowNo][colNo] |
|
1121 |
cellstyle = self._cellStyles[rowNo][colNo] |
|
1122 |
self._drawCell(cellval, cellstyle, (x, y), (width, height)) |
|
2374
66f754e27a01
tables.py: implement Eric Stephen's border drawing suggestion
rgbecker
parents:
2332
diff
changeset
|
1123 |
self._drawLines() |
2190 | 1124 |
|
326 | 1125 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1126 |
def _drawBkgrnd(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1127 |
nrows = self._nrows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1128 |
ncols = self._ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1129 |
for cmd, (sc, sr), (ec, er), arg in self._bkgrndcmds: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1130 |
if sc < 0: sc = sc + ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1131 |
if ec < 0: ec = ec + ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1132 |
if sr < 0: sr = sr + nrows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1133 |
if er < 0: er = er + nrows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1134 |
x0 = self._colpositions[sc] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1135 |
y0 = self._rowpositions[sr] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1136 |
x1 = self._colpositions[min(ec+1,ncols)] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1137 |
y1 = self._rowpositions[min(er+1,nrows)] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1138 |
w, h = x1-x0, y1-y0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1139 |
canv = self.canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1140 |
if callable(arg): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1141 |
apply(arg,(self,canv, x0, y0, w, h)) |
2392 | 1142 |
elif cmd == 'ROWBACKGROUNDS': |
1143 |
#Need a list of colors to cycle through. The arguments |
|
1144 |
#might be already colours, or convertible to colors, or |
|
1145 |
# None, or the string 'None'. |
|
1146 |
#It's very common to alternate a pale shade with None. |
|
1147 |
colorCycle = map(colors.toColorOrNone, arg) |
|
1148 |
count = len(colorCycle) |
|
2397
6b3ce9559beb
fixed col/row shading bug where last row or col never got shaded - reported R Revill
andy
parents:
2396
diff
changeset
|
1149 |
rowCount = er - sr + 1 |
2392 | 1150 |
for i in range(rowCount): |
1151 |
color = colorCycle[i%count] |
|
1152 |
h = self._rowHeights[sr + i] |
|
1153 |
if color: |
|
1154 |
canv.setFillColor(color) |
|
1155 |
canv.rect(x0, y0, w, -h, stroke=0,fill=1) |
|
1156 |
y0 = y0 - h |
|
1157 |
||
1158 |
elif cmd == 'COLBACKGROUNDS': |
|
1159 |
#cycle through colours columnwise |
|
1160 |
colorCycle = map(colors.toColorOrNone, arg) |
|
1161 |
count = len(colorCycle) |
|
2397
6b3ce9559beb
fixed col/row shading bug where last row or col never got shaded - reported R Revill
andy
parents:
2396
diff
changeset
|
1162 |
colCount = ec - sc + 1 |
2392 | 1163 |
for i in range(colCount): |
1164 |
color = colorCycle[i%count] |
|
1165 |
w = self._colWidths[sc + i] |
|
1166 |
if color: |
|
1167 |
canv.setFillColor(color) |
|
1168 |
canv.rect(x0, y0, w, h, stroke=0,fill=1) |
|
1169 |
x0 = x0 +w |
|
2393 | 1170 |
else: #cmd=='BACKGROUND' |
1171 |
canv.setFillColor(colors.toColor(arg)) |
|
1172 |
canv.rect(x0, y0, w, h, stroke=0,fill=1) |
|
326 | 1173 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1174 |
def _drawCell(self, cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1175 |
if self._curcellstyle is not cellstyle: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1176 |
cur = self._curcellstyle |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1177 |
if cur is None or cellstyle.color != cur.color: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1178 |
self.canv.setFillColor(cellstyle.color) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1179 |
if cur is None or cellstyle.leading != cur.leading or cellstyle.fontname != cur.fontname or cellstyle.fontsize != cur.fontsize: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1180 |
self.canv.setFont(cellstyle.fontname, cellstyle.fontsize, cellstyle.leading) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1181 |
self._curcellstyle = cellstyle |
419 | 1182 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1183 |
just = cellstyle.alignment |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1184 |
valign = cellstyle.valign |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1185 |
n = type(cellval) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1186 |
if n in _SeqTypes or isinstance(cellval,Flowable): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1187 |
if not n in _SeqTypes: cellval = (cellval,) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1188 |
# we assume it's a list of Flowables |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1189 |
W = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1190 |
H = [] |
2189 | 1191 |
w, h = self._listCellGeom(cellval,colwidth,cellstyle,W=W, H=H,aH=rowheight) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1192 |
if valign=='TOP': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1193 |
y = rowpos + rowheight - cellstyle.topPadding |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1194 |
elif valign=='BOTTOM': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1195 |
y = rowpos+cellstyle.bottomPadding + h |