author | andy |
Wed, 29 Sep 2004 10:18:32 +0000 | |
changeset 2392 | 6c59b0eb312d |
parent 2389 | 174fe506bf48 |
child 2393 | 6b3e47730acc |
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 |
421 | 27 |
from types import TupleType, ListType, StringType |
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 |
|
1699 | 45 |
LINECAPS={'butt':0,'round':1,'projecting':2,'squared':2} |
46 |
LINEJOINS={'miter':0,'round':1,'bevel':2} |
|
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 |
|
221 | 152 |
class Table(Flowable): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
153 |
def __init__(self, data, colWidths=None, rowHeights=None, style=None, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
154 |
repeatRows=0, repeatCols=0, splitByRow=1, emptyTableAction=None): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
155 |
self.hAlign = 'CENTER' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
156 |
self.vAlign = 'MIDDLE' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
157 |
if type(data) not in _SeqTypes: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
158 |
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
|
159 |
self._nrows = nrows = len(data) |
2221 | 160 |
self._cellvalues = [] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
161 |
if nrows: self._ncols = ncols = max(map(_rowLen,data)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
162 |
elif colWidths: ncols = len(colWidths) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
163 |
else: ncols = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
164 |
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
|
165 |
if not (nrows and ncols): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
166 |
if emptyTableAction=='error': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
167 |
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
|
168 |
elif emptyTableAction=='indicate': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
169 |
self.__class__ = Preformatted |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
170 |
global _emptyTableStyle |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
171 |
if '_emptyTableStyle' not in globals().keys(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
172 |
_emptyTableStyle = ParagraphStyle('_emptyTableStyle') |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
173 |
_emptyTableStyle.textColor = colors.red |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
174 |
_emptyTableStyle.backColor = colors.yellow |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
175 |
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
|
176 |
elif emptyTableAction=='ignore': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
177 |
self.__class__ = Spacer |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
178 |
Spacer.__init__(self,0,0) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
179 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
180 |
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
|
181 |
return |
1533 | 182 |
|
2299
d757841f2006
Fix proposed by Derik Barclay <dbarclay@givex.com>
rgbecker
parents:
2289
diff
changeset
|
183 |
self._cellvalues = data |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
184 |
if colWidths is None: colWidths = ncols*[None] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
185 |
elif len(colWidths) != ncols: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
186 |
raise ValueError, "%s data error - %d columns in data but %d in grid" % (self.identity(),ncols, len(colWidths)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
187 |
if rowHeights is None: rowHeights = nrows*[None] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
188 |
elif len(rowHeights) != nrows: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
189 |
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
|
190 |
for i in range(nrows): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
191 |
if len(data[i]) != ncols: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
192 |
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
|
193 |
self._rowHeights = self._argH = rowHeights |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
194 |
self._colWidths = self._argW = colWidths |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
195 |
cellrows = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
196 |
for i in range(nrows): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
197 |
cellcols = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
198 |
for j in range(ncols): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
199 |
cellcols.append(CellStyle(`(i,j)`)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
200 |
cellrows.append(cellcols) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
201 |
self._cellStyles = cellrows |
350 | 202 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
203 |
self._bkgrndcmds = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
204 |
self._linecmds = [] |
1883 | 205 |
self._spanCmds = [] |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
206 |
self.repeatRows = repeatRows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
207 |
self.repeatCols = repeatCols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
208 |
self.splitByRow = splitByRow |
6 | 209 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
210 |
if style: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
211 |
self.setStyle(style) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
212 |
def __repr__(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
213 |
"incomplete, but better than nothing" |
2389 | 214 |
r = getattr(self,'_rowHeights','[unknown]') |
215 |
c = getattr(self,'_colWidths','[unknown]') |
|
216 |
cv = getattr(self,'_cellvalues','[unknown]') |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
217 |
import pprint, string |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
218 |
cv = pprint.pformat(cv) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
219 |
cv = string.replace(cv, "\n", "\n ") |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
220 |
return "%s(\n rowHeights=%s,\n colWidths=%s,\n%s\n) # end table" % (self.__class__.__name__,r,c,cv) |
342 | 221 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
222 |
def identity(self, maxLen=30): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
223 |
'''Identify our selves as well as possible''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
224 |
vx = None |
1989 | 225 |
nr = getattr(self,'_nrows','unknown') |
226 |
nc = getattr(self,'_ncols','unknown') |
|
2299
d757841f2006
Fix proposed by Derik Barclay <dbarclay@givex.com>
rgbecker
parents:
2289
diff
changeset
|
227 |
cv = getattr(self,'_cellvalues',None) |
1989 | 228 |
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
|
229 |
b = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
230 |
for i in xrange(nr): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
231 |
for j in xrange(nc): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
232 |
v = cv[i][j] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
233 |
t = type(v) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
234 |
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
|
235 |
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
|
236 |
r = '' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
237 |
for vij in v: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
238 |
r = vij.identity(maxLen) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
239 |
if r and r[-4:]!='>...': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
240 |
break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
241 |
if r and r[-4:]!='>...': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
242 |
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
|
243 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
244 |
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
|
245 |
ix, jx, vx = i, j, v |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
246 |
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
|
247 |
if maxLen: vx = vx[:maxLen] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
248 |
if b: break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
249 |
if b: break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
250 |
if vx: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
251 |
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
|
252 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
253 |
vx = '...' |
1103 | 254 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
255 |
return "<%s at %d %d rows x %s cols>%s" % (self.__class__.__name__, id(self), nr, nc, vx) |
1103 | 256 |
|
2189 | 257 |
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
|
258 |
aW = w-s.leftPadding-s.rightPadding |
2189 | 259 |
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
|
260 |
t = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
261 |
w = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
262 |
canv = getattr(self,'canv',None) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
263 |
for v in V: |
2189 | 264 |
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
|
265 |
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
|
266 |
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
|
267 |
w = max(w,vw) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
268 |
t = t + vh + v.getSpaceBefore()+v.getSpaceAfter() |
1683 | 269 |
return w, t - V[0].getSpaceBefore()-V[-1].getSpaceAfter() |
1492 | 270 |
|
2271 | 271 |
def _calc_width(self,availWidth,W=None): |
2289 | 272 |
if getattr(self,'_width_calculated_once',None): return |
273 |
#comments added by Andy to Robin's slightly terse variable names |
|
2271 | 274 |
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
|
275 |
if None in W: #some column widths are not given |
2289 | 276 |
canv = getattr(self,'canv',None) |
277 |
saved = None |
|
278 |
colSpanCells = self._spanCmds and self._colSpanCells or () |
|
2277 | 279 |
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
|
280 |
while None in W: |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
w = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
286 |
i = 0 |
2190 | 287 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
288 |
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
|
289 |
#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
|
290 |
#assume a zero size. |
2289 | 291 |
if (j, i) in colSpanCells: |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
292 |
t = 0.0 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
293 |
else:#work out size |
1917 | 294 |
t = self._elementWidth(v,s) |
295 |
if t is None: |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
296 |
raise ValueError, "Flowable %s in cell(%d,%d) can't have auto width\n%s" % (v.identity(30),i,j,self.identity(30)) |
1917 | 297 |
t = t + s.leftPadding+s.rightPadding |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
298 |
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
|
299 |
i = i + 1 |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
300 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
301 |
W[j] = w |
1596
753865895a6f
Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents:
1536
diff
changeset
|
302 |
|
2277 | 303 |
self._colWidths = W |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
304 |
width = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
305 |
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
|
306 |
for w in W: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
307 |
width = width + w |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
308 |
self._colpositions.append(width) |
1683 | 309 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
310 |
self._width = width |
2289 | 311 |
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
|
312 |
|
1917 | 313 |
def _elementWidth(self,v,s): |
314 |
t = type(v) |
|
315 |
if t in _SeqTypes: |
|
316 |
w = 0 |
|
317 |
for e in v: |
|
318 |
ew = self._elementWidth(self,v) |
|
319 |
if ew is None: return None |
|
320 |
w = max(w,ew) |
|
321 |
return w |
|
322 |
elif isinstance(v,Flowable) and v._fixedWidth: |
|
323 |
return v.width |
|
324 |
else: |
|
325 |
if t is not StringType: v = v is None and '' or str(v) |
|
326 |
v = string.split(v, "\n") |
|
327 |
return max(map(lambda a, b=s.fontname, c=s.fontsize,d=pdfmetrics.stringWidth: d(a,b,c), v)) |
|
328 |
||
2271 | 329 |
def _calc_height(self, availHeight, availWidth, H=None, W=None): |
333 | 330 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
331 |
H = self._argH |
2271 | 332 |
if not W: W = _calc_pc(self._argW,availWidth) #widths array |
6 | 333 |
|
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
334 |
hmax = lim = len(H) |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
335 |
longTable = getattr(self,'_longTableOptimize',None) |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
336 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
337 |
if None in H: |
2289 | 338 |
canv = getattr(self,'canv',None) |
339 |
saved = None |
|
340 |
#get a handy list of any cells which span rows. should be ignored for sizing |
|
341 |
if self._spanCmds: |
|
342 |
rowSpanCells = self._rowSpanCells |
|
343 |
colSpanCells = self._colSpanCells |
|
344 |
spanRanges = self._spanRanges |
|
345 |
colpositions = self._colpositions |
|
346 |
else: |
|
347 |
rowSpanCells = colSpanCells = () |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
348 |
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
|
349 |
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
|
350 |
self._rowHeights = H |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
351 |
while None in H: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
352 |
i = H.index(None) |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
353 |
if longTable: |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
354 |
hmax = i |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
355 |
height = reduce(operator.add, H[:i], 0) |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
356 |
# 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
|
357 |
if height > availHeight: break |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
358 |
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
|
359 |
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
|
360 |
h = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
361 |
j = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
362 |
for v, s, w in map(None, V, S, W): # value, style, width (lengths must match) |
2289 | 363 |
ji = j,i |
364 |
if ji in rowSpanCells: |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
365 |
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
|
366 |
else: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
367 |
t = type(v) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
368 |
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
|
369 |
if not t in _SeqTypes: v = (v,) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
370 |
if w is None: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
371 |
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
|
372 |
if canv: canv._fontname, canv._fontsize, canv._leading = s.fontname, s.fontsize, s.leading or 1.2*s.fontsize |
2289 | 373 |
if ji in colSpanCells: |
374 |
t = spanRanges[ji] |
|
375 |
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
|
376 |
dW,t = self._listCellGeom(v,w,s) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
377 |
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
|
378 |
dW = dW + s.leftPadding + s.rightPadding |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
379 |
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
|
380 |
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
|
381 |
else: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
382 |
if t is not StringType: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
383 |
v = v is None and '' or str(v) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
384 |
v = string.split(v, "\n") |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
385 |
t = s.leading*len(v) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
386 |
t = t+s.bottomPadding+s.topPadding |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
387 |
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
|
388 |
j = j + 1 |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
389 |
H[i] = h |
2200
be0cfccc662a
Fixed up tabs and whitespace in all source files
andy_robinson
parents:
2194
diff
changeset
|
390 |
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
|
391 |
|
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
392 |
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
|
393 |
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
|
394 |
for h in H[:hmax]: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
395 |
height = height - h |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
396 |
self._rowpositions.append(height) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
397 |
assert abs(height)<1e-8, 'Internal height error' |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
398 |
self._hmax = hmax |
1596
753865895a6f
Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents:
1536
diff
changeset
|
399 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
400 |
def _calc(self, availWidth, availHeight): |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
401 |
#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
|
402 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
403 |
#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
|
404 |
#cells. If so, apply a different algorithm |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
405 |
#and assign some withs in a dumb way. |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
406 |
#this CHANGES the widths array. |
2284 | 407 |
if (None in self._colWidths or '*' in self._colWidths) and self._hasVariWidthElements(): |
2271 | 408 |
W = self._calcPreliminaryWidths(availWidth) #widths |
409 |
else: |
|
410 |
W = None |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
411 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
412 |
# 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
|
413 |
# 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
|
414 |
# in sizing |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
415 |
if self._spanCmds: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
416 |
self._calcSpanRanges() |
2289 | 417 |
if None in self._argH: |
418 |
self._calc_width(availWidth,W=W) |
|
2190 | 419 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
420 |
# calculate the full table height |
2271 | 421 |
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
|
422 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
423 |
# calculate the full table width |
2271 | 424 |
self._calc_width(availWidth,W=W) |
2190 | 425 |
|
1883 | 426 |
if self._spanCmds: |
2289 | 427 |
#now work out the actual rect for each spanned cell from the underlying grid |
1883 | 428 |
self._calcSpanRects() |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
429 |
|
1917 | 430 |
def _hasVariWidthElements(self, upToRow=None): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
431 |
"""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
|
432 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
433 |
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
|
434 |
images and graphics.""" |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
435 |
bad = 0 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
436 |
if upToRow is None: upToRow = self._nrows |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
437 |
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
|
438 |
for col in range(self._ncols): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
439 |
value = self._cellvalues[row][col] |
1917 | 440 |
if not self._canGetWidth(value): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
441 |
bad = 1 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
442 |
#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
|
443 |
return bad |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
444 |
|
1917 | 445 |
def _canGetWidth(self, thing): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
446 |
"Can we work out the width quickly?" |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
447 |
if type(thing) in (ListType, TupleType): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
448 |
for elem in thing: |
1917 | 449 |
if not self._canGetWidth(elem): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
450 |
return 0 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
451 |
return 1 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
452 |
elif isinstance(thing, Flowable): |
1917 | 453 |
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
|
454 |
else: #string, number, None etc. |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
455 |
#anything else gets passed to str(...) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
456 |
# so should be sizable |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
457 |
return 1 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
458 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
459 |
def _calcPreliminaryWidths(self, availWidth): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
460 |
"""Fallback algorithm for when main one fails. |
1883 | 461 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
462 |
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
|
463 |
paragraphs might be present, do a preliminary scan |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
464 |
and assign some sensible values - just divide up |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
465 |
all unsizeable columns by the remaining space.""" |
2271 | 466 |
|
467 |
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
|
468 |
verbose = 0 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
469 |
totalDefined = 0.0 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
470 |
numberUndefined = 0 |
2271 | 471 |
for w in W: |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
472 |
if w is None: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
473 |
numberUndefined = numberUndefined + 1 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
474 |
else: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
475 |
totalDefined = totalDefined + w |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
476 |
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
|
477 |
self._ncols, numberUndefined, availWidth - totalDefined) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
478 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
479 |
#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
|
480 |
given = [] |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
481 |
sizeable = [] |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
482 |
unsizeable = [] |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
483 |
for colNo in range(self._ncols): |
2271 | 484 |
if W[colNo] is None: |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
485 |
siz = 1 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
486 |
for rowNo in range(self._nrows): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
487 |
value = self._cellvalues[rowNo][colNo] |
1917 | 488 |
if not self._canGetWidth(value): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
489 |
siz = 0 |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
490 |
break |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
491 |
if siz: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
492 |
sizeable.append(colNo) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
493 |
else: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
494 |
unsizeable.append(colNo) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
495 |
else: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
496 |
given.append(colNo) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
497 |
if len(given) == self._ncols: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
498 |
return |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
499 |
if verbose: print 'predefined width: ',given |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
500 |
if verbose: print 'uncomputable width: ',unsizeable |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
501 |
if verbose: print 'computable width: ',sizeable |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
502 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
503 |
#how much width is left: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
504 |
# on the next iteration we could size the sizeable ones, for now I'll just |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
505 |
# divide up the space |
2271 | 506 |
newColWidths = list(W) |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
507 |
guessColWidth = (availWidth - totalDefined) / (len(unsizeable)+len(sizeable)) |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
508 |
assert guessColWidth >= 0, "table is too wide already, cannot choose a sane width for undefined columns" |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
509 |
if verbose: print 'assigning width %0.2f to all undefined columns' % guessColWidth |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
510 |
for colNo in sizeable: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
511 |
newColWidths[colNo] = guessColWidth |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
512 |
for colNo in unsizeable: |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
513 |
newColWidths[colNo] = guessColWidth |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
514 |
|
2271 | 515 |
if verbose: print 'new widths are:', newColWidths |
516 |
self._argW = self._colWidths = newColWidths |
|
517 |
return newColWidths |
|
2190 | 518 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
519 |
def _calcSpanRanges(self): |
1883 | 520 |
"""Work out rects for tables which do row and column spanning. |
521 |
||
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
522 |
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
|
523 |
if a cell is part of a "spanned" range. |
1883 | 524 |
self._spanRanges shows the 'coords' in integers of each |
525 |
'cell range', or None if it was clobbered: |
|
526 |
(col, row) -> (col0, row0, col1, row1) |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
527 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
528 |
Any cell not in the key is not part of a spanned region |
1883 | 529 |
""" |
2289 | 530 |
self._spanRanges = spanRanges = {} |
531 |
for x in xrange(self._ncols): |
|
532 |
for y in xrange(self._nrows): |
|
533 |
spanRanges[x,y] = (x, y, x, y) |
|
534 |
self._colSpanCells = [] |
|
535 |
self._rowSpanCells = [] |
|
536 |
csa = self._colSpanCells.append |
|
537 |
rsa = self._rowSpanCells.append |
|
1883 | 538 |
for (cmd, start, stop) in self._spanCmds: |
539 |
x0, y0 = start |
|
540 |
x1, y1 = stop |
|
541 |
||
542 |
#normalize |
|
543 |
if x0 < 0: x0 = x0 + self._ncols |
|
544 |
if x1 < 0: x1 = x1 + self._ncols |
|
545 |
if y0 < 0: y0 = y0 + self._nrows |
|
546 |
if y1 < 0: y1 = y1 + self._nrows |
|
2289 | 547 |
if x0 > x1: x0, x1 = x1, x0 |
548 |
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
|
549 |
|
2289 | 550 |
if x0!=x1 or y0!=y1: |
551 |
#column span |
|
552 |
if x0!=x1: |
|
553 |
for y in xrange(y0, y1+1): |
|
554 |
for x in xrange(x0,x1+1): |
|
555 |
csa((x,y)) |
|
556 |
#row span |
|
557 |
if y0!=y1: |
|
558 |
for y in xrange(y0, y1+1): |
|
559 |
for x in xrange(x0,x1+1): |
|
560 |
rsa((x,y)) |
|
2190 | 561 |
|
2289 | 562 |
for y in xrange(y0, y1+1): |
563 |
for x in xrange(x0,x1+1): |
|
564 |
spanRanges[x,y] = None |
|
565 |
# set the main entry |
|
566 |
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
|
567 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
568 |
def _calcSpanRects(self): |
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
569 |
"""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
|
570 |
|
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
571 |
Based on self._spanRanges, which is already known, |
2190 | 572 |
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
|
573 |
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
|
574 |
(col, row) -> (x, y, width, height) |
2190 | 575 |
|
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
576 |
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
|
577 |
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
|
578 |
""" |
2289 | 579 |
if getattr(self,'_spanRects',None): return |
1883 | 580 |
spanRects = {} |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
581 |
for (coord, value) in self._spanRanges.items(): |
1883 | 582 |
if value is None: |
583 |
spanRects[coord] = None |
|
584 |
else: |
|
585 |
col,row = coord |
|
586 |
col0, row0, col1, row1 = value |
|
587 |
x = self._colpositions[col0] |
|
588 |
y = self._rowpositions[row1+1] # should I add 1 for bottom left? |
|
589 |
width = self._colpositions[col1+1] - x |
|
590 |
height = self._rowpositions[row0] - y |
|
591 |
spanRects[coord] = (x, y, width, height) |
|
2190 | 592 |
|
1883 | 593 |
self._spanRects = spanRects |
2190 | 594 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
595 |
def setStyle(self, tblstyle): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
596 |
if type(tblstyle) is not TableStyleType: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
597 |
tblstyle = TableStyle(tblstyle) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
598 |
for cmd in tblstyle.getCommands(): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
599 |
self._addCommand(cmd) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
600 |
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
|
601 |
setattr(self,k,v) |
350 | 602 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
603 |
def _addCommand(self,cmd): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
604 |
if cmd[0] == 'BACKGROUND': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
605 |
self._bkgrndcmds.append(cmd) |
2392 | 606 |
if cmd[0] == 'ROWBACKGROUNDS': |
607 |
self._bkgrndcmds.append(cmd) |
|
608 |
if cmd[0] == 'COLBACKGROUNDS': |
|
609 |
self._bkgrndcmds.append(cmd) |
|
1883 | 610 |
elif cmd[0] == 'SPAN': |
611 |
self._spanCmds.append(cmd) |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
612 |
elif _isLineCommand(cmd): |
1699 | 613 |
# we expect op, start, stop, weight, colour, cap, dashes, join |
614 |
cmd = tuple(cmd) |
|
615 |
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
|
616 |
|
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
617 |
#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
|
618 |
if len(cmd)<6: |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
619 |
cmd = cmd+(1,) |
1699 | 620 |
else: |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
621 |
cap = cmd[5] |
1699 | 622 |
try: |
623 |
if type(cap) is not type(int): |
|
624 |
cap = LINECAPS[cap] |
|
625 |
elif cap<0 or cap>2: |
|
626 |
raise ValueError |
|
627 |
cmd = cmd[:5]+(cap,)+cmd[6:] |
|
628 |
except: |
|
629 |
ValueError('Bad cap value %s in %s'%(cap,str(cmd))) |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
630 |
#dashes at index 6 - this is a dash array: |
1699 | 631 |
if len(cmd)<7: cmd = cmd+(None,) |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
632 |
|
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
633 |
#join mode at index 7 - can be string or numeric, look up as for caps |
1699 | 634 |
if len(cmd)<8: cmd = cmd+(1,) |
635 |
else: |
|
636 |
join = cmd[7] |
|
637 |
try: |
|
638 |
if type(join) is not type(int): |
|
639 |
join = LINEJOINS[cap] |
|
640 |
elif join<0 or join>2: |
|
641 |
raise ValueError |
|
642 |
cmd = cmd[:7]+(join,) |
|
643 |
except: |
|
644 |
ValueError('Bad join value %s in %s'%(join,str(cmd))) |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
645 |
|
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
646 |
#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
|
647 |
if len(cmd)<9: |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
648 |
lineCount = 1 |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
649 |
cmd = cmd + (lineCount,) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
650 |
else: |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
651 |
lineCount = cmd[8] |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
652 |
assert lineCount >= 1 |
2221 | 653 |
#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
|
654 |
#width so you get a visible gap between centres |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
655 |
if len(cmd)<10: cmd = cmd + (cmd[3],) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
656 |
|
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
657 |
assert len(cmd) == 10 |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
658 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
659 |
self._linecmds.append(cmd) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
660 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
661 |
(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
|
662 |
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
|
663 |
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
|
664 |
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
|
665 |
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
|
666 |
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
|
667 |
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
|
668 |
_setCellStyle(self._cellStyles, i, j, op, values) |
326 | 669 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
670 |
def _drawLines(self): |
1699 | 671 |
ccap, cdash, cjoin = None, None, None |
672 |
self.canv.saveState() |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
673 |
for op, (sc,sr), (ec,er), weight, color, cap, dash, join, count, space in self._linecmds: |
2185 | 674 |
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
|
675 |
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
|
676 |
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
|
677 |
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
|
678 |
if er < 0: er = er + self._nrows |
1699 | 679 |
if ccap!=cap: |
680 |
self.canv.setLineCap(cap) |
|
681 |
ccap = cap |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
682 |
getattr(self,_LineOpMap.get(op, '_drawUnknown' ))( (sc, sr), (ec, er), weight, color, count, space) |
1699 | 683 |
self.canv.restoreState() |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
684 |
self._curcolor = None |
248 | 685 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
686 |
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
|
687 |
raise ValueError, "Unknown line command '%s'" % op |
403 | 688 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
689 |
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
|
690 |
self._drawBox( (sc, sr), (ec, er), weight, color, count, space) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
691 |
self._drawInnerGrid( (sc, sr), (ec, er), weight, color, count, space) |
403 | 692 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
693 |
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
|
694 |
self._drawHLines((sc, sr), (ec, sr), weight, color, count, space) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
695 |
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
|
696 |
self._drawVLines((sc, sr), (sc, er), weight, color, count, space) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
697 |
self._drawVLines((ec+1, sr), (ec+1, er), weight, color, count, space) |
350 | 698 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
699 |
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
|
700 |
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
|
701 |
self._drawVLines((sc+1, sr), (ec, er), weight, color, count, space) |
350 | 702 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
703 |
def _prepLine(self, weight, color): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
704 |
if color != self._curcolor: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
705 |
self.canv.setStrokeColor(color) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
706 |
self._curcolor = color |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
707 |
if weight != self._curweight: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
708 |
self.canv.setLineWidth(weight) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
709 |
self._curweight = weight |
350 | 710 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
711 |
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
|
712 |
ecp = self._colpositions[sc:ec+2] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
713 |
rp = self._rowpositions[sr:er+1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
714 |
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
|
715 |
self._prepLine(weight, color) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
716 |
scp = ecp[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
717 |
ecp = ecp[-1] |
2220 | 718 |
if count == 1: |
719 |
for rowpos in rp: |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
720 |
self.canv.line(scp, rowpos, ecp, rowpos) |
2220 | 721 |
else: |
722 |
#multi-lines; position so count==1 and no space gives origin |
|
723 |
ws = weight+space |
|
724 |
offset = 0.5*(count-1)*ws |
|
725 |
for rowpos in rp: |
|
726 |
y = rowpos+offset |
|
727 |
for idx in xrange(count): |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
728 |
self.canv.line(scp, y, ecp, y) |
2220 | 729 |
y -= ws |
403 | 730 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
731 |
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
|
732 |
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
|
733 |
|
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
734 |
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
|
735 |
erp = self._rowpositions[sr:er+2] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
736 |
cp = self._colpositions[sc:ec+1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
737 |
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
|
738 |
self._prepLine(weight, color) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
739 |
srp = erp[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
740 |
erp = erp[-1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
741 |
for colpos in cp: |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
742 |
if count == 1: |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
743 |
self.canv.line(colpos, srp, colpos, erp) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
744 |
else: #double/triple lines |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
745 |
#position so count=1 and no space gives origin |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
746 |
offset = (count-1) * (weight + space) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
747 |
for idx in range(count): |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
748 |
x = colpos + 0.5*offset - (idx * (weight+space)) |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
749 |
self.canv.line(x, srp, x, erp) |
326 | 750 |
|
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
751 |
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
|
752 |
self._drawVLines((sc+1, sr), (ec+1, er), weight, color, count, space) |
403 | 753 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
754 |
def wrap(self, availWidth, availHeight): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
755 |
self._calc(availWidth, availHeight) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
756 |
#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
|
757 |
self.availWidth = availWidth |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
758 |
return (self._width, self._height) |
1495 | 759 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
760 |
def onSplit(self,T,byRow=1): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
761 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
762 |
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
|
763 |
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
|
764 |
''' |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
765 |
pass |
419 | 766 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
767 |
def _cr_0(self,n,cmds): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
768 |
for c in cmds: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
769 |
c = tuple(c) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
770 |
(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
|
771 |
if sr>=n: continue |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
772 |
if er>=n: er = n-1 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
773 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
350 | 774 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
775 |
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
|
776 |
for c in cmds: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
777 |
c = tuple(c) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
778 |
(sc,sr), (ec,er) = c[1:3] |
2185 | 779 |
if sr in ('splitfirst','splitlast'): self._addCommand(c) |
780 |
else: |
|
781 |
if sr>=0 and sr>=repeatRows and sr<n and er>=0 and er<n: continue |
|
782 |
if sr>=repeatRows and sr<n: sr=repeatRows |
|
783 |
elif sr>=repeatRows and sr>=n: sr=sr+repeatRows-n |
|
784 |
if er>=repeatRows and er<n: er=repeatRows |
|
785 |
elif er>=repeatRows and er>=n: er=er+repeatRows-n |
|
786 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
|
350 | 787 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
788 |
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
|
789 |
for c in cmds: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
790 |
c = tuple(c) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
791 |
(sc,sr), (ec,er) = c[1:3] |
2185 | 792 |
if sr in ('splitfirst','splitlast'): self._addCommand(c) |
793 |
else: |
|
794 |
if er>=0 and er<n: continue |
|
795 |
if sr>=0 and sr<n: sr=0 |
|
796 |
if sr>=n: sr = sr-n |
|
797 |
if er>=n: er = er-n |
|
798 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
|
350 | 799 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
800 |
def _splitRows(self,availHeight): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
801 |
h = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
802 |
n = 0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
803 |
lim = len(self._rowHeights) |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
804 |
while n<self._hmax: |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
805 |
hn = h + self._rowHeights[n] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
806 |
if hn>availHeight: break |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
807 |
h = hn |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
808 |
n = n + 1 |
350 | 809 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
810 |
if n<=self.repeatRows: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
811 |
return [] |
350 | 812 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
813 |
if n==lim: return [self] |
350 | 814 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
815 |
repeatRows = self.repeatRows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
816 |
repeatCols = self.repeatCols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
817 |
splitByRow = self.splitByRow |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
818 |
data = self._cellvalues |
350 | 819 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
820 |
#we're going to split into two superRows |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
821 |
#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
|
822 |
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
|
823 |
repeatRows=repeatRows, repeatCols=repeatCols, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
824 |
splitByRow=splitByRow) |
350 | 825 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
826 |
#copy the styles and commands |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
827 |
R0._cellStyles = self._cellStyles[:n] |
419 | 828 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
829 |
A = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
830 |
# hack up the line commands |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
831 |
for op, (sc,sr), (ec,er), weight, color, cap, dash, join, count, space in self._linecmds: |
2185 | 832 |
if type(sr)is type('') and sr.startswith('split'): |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
833 |
A.append((op,(sc,sr), (ec,sr), weight, color, cap, dash, join, count, space)) |
2185 | 834 |
if sr=='splitlast': |
835 |
sr = er = n-1 |
|
836 |
elif sr=='splitfirst': |
|
837 |
sr = n |
|
838 |
er = n |
|
839 |
||
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
840 |
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
|
841 |
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
|
842 |
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
|
843 |
if er < 0: er = er + self._nrows |
419 | 844 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
845 |
if op in ('BOX','OUTLINE','GRID'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
846 |
if sr<n and er>=n: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
847 |
# we have to split the BOX |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
848 |
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
|
849 |
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
|
850 |
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
|
851 |
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
|
852 |
if op=='GRID': |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
853 |
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
|
854 |
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
|
855 |
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
|
856 |
else: |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
857 |
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
|
858 |
elif op in ('INNERGRID','LINEABOVE'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
859 |
if sr<n and er>=n: |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
860 |
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
|
861 |
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
|
862 |
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
|
863 |
elif op == 'LINEBELOW': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
864 |
if sr<n and er>=(n-1): |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
865 |
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
|
866 |
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
|
867 |
elif op == 'LINEABOVE': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
868 |
if sr<=n and er>=n: |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
869 |
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
|
870 |
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
|
871 |
else: |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
872 |
A.append((op,(sc,sr), (ec,er), weight, color, cap, dash, join, count, space)) |
419 | 873 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
874 |
R0._cr_0(n,A) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
875 |
R0._cr_0(n,self._bkgrndcmds) |
350 | 876 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
877 |
if repeatRows: |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
878 |
#R1 = slelf.__class__(data[:repeatRows]+data[n:],self._argW, |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
879 |
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
|
880 |
self._argH[:repeatRows]+self._argH[n:], |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
881 |
repeatRows=repeatRows, repeatCols=repeatCols, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
882 |
splitByRow=splitByRow) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
883 |
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
|
884 |
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
|
885 |
R1._cr_1_1(n,repeatRows,self._bkgrndcmds) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
886 |
else: |
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
887 |
#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
|
888 |
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
|
889 |
repeatRows=repeatRows, repeatCols=repeatCols, |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
890 |
splitByRow=splitByRow) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
891 |
R1._cellStyles = self._cellStyles[n:] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
892 |
R1._cr_1_0(n,A) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
893 |
R1._cr_1_0(n,self._bkgrndcmds) |
350 | 894 |
|
1498
2e7cfa1159cb
Copy alignments accross split (thanks R�diger M�hl <ruediger.maehl@web.de>
rgbecker
parents:
1495
diff
changeset
|
895 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
896 |
R0.hAlign = R1.hAlign = self.hAlign |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
897 |
R0.vAlign = R1.vAlign = self.vAlign |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
898 |
self.onSplit(R0) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
899 |
self.onSplit(R1) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
900 |
return [R0,R1] |
350 | 901 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
902 |
def split(self, availWidth, availHeight): |
1912
c8509682e3e0
Finished off sizing logic to go with row and column
andy_robinson
parents:
1883
diff
changeset
|
903 |
self._calc(availWidth, availHeight) |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
904 |
if self.splitByRow: |
2012 | 905 |
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
|
906 |
return self._splitRows(availHeight) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
907 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
908 |
raise NotImplementedError |
403 | 909 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
910 |
def draw(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
911 |
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
|
912 |
self._drawBkgrnd() |
1883 | 913 |
if self._spanCmds == []: |
914 |
# old fashioned case, no spanning, steam on and do each cell |
|
915 |
for row, rowstyle, rowpos, rowheight in map(None, self._cellvalues, self._cellStyles, self._rowpositions[1:], self._rowHeights): |
|
916 |
for cellval, cellstyle, colpos, colwidth in map(None, row, rowstyle, self._colpositions[:-1], self._colWidths): |
|
917 |
self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)) |
|
918 |
else: |
|
919 |
# we have some row or col spans, need a more complex algorithm |
|
920 |
# to find the rect for each |
|
921 |
for rowNo in range(self._nrows): |
|
922 |
for colNo in range(self._ncols): |
|
923 |
cellRect = self._spanRects[colNo, rowNo] |
|
924 |
if cellRect is not None: |
|
925 |
(x, y, width, height) = cellRect |
|
926 |
cellval = self._cellvalues[rowNo][colNo] |
|
927 |
cellstyle = self._cellStyles[rowNo][colNo] |
|
928 |
self._drawCell(cellval, cellstyle, (x, y), (width, height)) |
|
2374
66f754e27a01
tables.py: implement Eric Stephen's border drawing suggestion
rgbecker
parents:
2332
diff
changeset
|
929 |
self._drawLines() |
2190 | 930 |
|
326 | 931 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
932 |
def _drawBkgrnd(self): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
933 |
nrows = self._nrows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
934 |
ncols = self._ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
935 |
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
|
936 |
if sc < 0: sc = sc + ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
937 |
if ec < 0: ec = ec + ncols |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
938 |
if sr < 0: sr = sr + nrows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
939 |
if er < 0: er = er + nrows |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
940 |
x0 = self._colpositions[sc] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
941 |
y0 = self._rowpositions[sr] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
942 |
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
|
943 |
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
|
944 |
w, h = x1-x0, y1-y0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
945 |
canv = self.canv |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
946 |
if callable(arg): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
947 |
apply(arg,(self,canv, x0, y0, w, h)) |
2392 | 948 |
elif cmd == 'BACKGROUND': |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
949 |
canv.setFillColor(colors.toColor(arg)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
950 |
canv.rect(x0, y0, w, h, stroke=0,fill=1) |
2392 | 951 |
elif cmd == 'ROWBACKGROUNDS': |
952 |
#Need a list of colors to cycle through. The arguments |
|
953 |
#might be already colours, or convertible to colors, or |
|
954 |
# None, or the string 'None'. |
|
955 |
#It's very common to alternate a pale shade with None. |
|
956 |
#print 'rowHeights=', self._rowHeights |
|
957 |
colorCycle = map(colors.toColorOrNone, arg) |
|
958 |
count = len(colorCycle) |
|
959 |
rowCount = er - sr |
|
960 |
for i in range(rowCount): |
|
961 |
color = colorCycle[i%count] |
|
962 |
h = self._rowHeights[sr + i] |
|
963 |
if color: |
|
964 |
canv.setFillColor(color) |
|
965 |
canv.rect(x0, y0, w, -h, stroke=0,fill=1) |
|
966 |
#print ' draw %0.0f, %0.0f, %0.0f, %0.0f' % (x0,y0,w,-h) |
|
967 |
y0 = y0 - h |
|
968 |
||
969 |
elif cmd == 'COLBACKGROUNDS': |
|
970 |
#cycle through colours columnwise |
|
971 |
colorCycle = map(colors.toColorOrNone, arg) |
|
972 |
count = len(colorCycle) |
|
973 |
colCount = ec - sc |
|
974 |
for i in range(colCount): |
|
975 |
color = colorCycle[i%count] |
|
976 |
w = self._colWidths[sc + i] |
|
977 |
if color: |
|
978 |
canv.setFillColor(color) |
|
979 |
canv.rect(x0, y0, w, h, stroke=0,fill=1) |
|
980 |
x0 = x0 +w |
|
981 |
||
326 | 982 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
983 |
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
|
984 |
if self._curcellstyle is not cellstyle: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
985 |
cur = self._curcellstyle |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
986 |
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
|
987 |
self.canv.setFillColor(cellstyle.color) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
988 |
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
|
989 |
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
|
990 |
self._curcellstyle = cellstyle |
419 | 991 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
992 |
just = cellstyle.alignment |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
993 |
valign = cellstyle.valign |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
994 |
n = type(cellval) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
995 |
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
|
996 |
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
|
997 |
# 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
|
998 |
W = [] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
999 |
H = [] |
2189 | 1000 |
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
|
1001 |
if valign=='TOP': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1002 |
y = rowpos + rowheight - cellstyle.topPadding |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1003 |
elif valign=='BOTTOM': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1004 |
y = rowpos+cellstyle.bottomPadding + h |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1005 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1006 |
y = rowpos+(rowheight+cellstyle.bottomPadding-cellstyle.topPadding+h)/2.0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1007 |
y = y+cellval[0].getSpaceBefore() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1008 |
for v, w, h in map(None,cellval,W,H): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1009 |
if just=='LEFT': x = colpos+cellstyle.leftPadding |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1010 |
elif just=='RIGHT': x = colpos+colwidth-cellstyle.rightPadding - w |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1011 |
elif just in ('CENTRE', 'CENTER'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1012 |
x = colpos+(colwidth+cellstyle.leftPadding-cellstyle.rightPadding-w)/2.0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1013 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1014 |
raise ValueError, 'Invalid justification %s' % just |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1015 |
y = y - v.getSpaceBefore() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1016 |
y = y - h |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1017 |
v.drawOn(self.canv,x,y) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1018 |
y = y - v.getSpaceAfter() |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1019 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1020 |
if just == 'LEFT': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1021 |
draw = self.canv.drawString |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1022 |
x = colpos + cellstyle.leftPadding |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1023 |
elif just in ('CENTRE', 'CENTER'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1024 |
draw = self.canv.drawCentredString |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1025 |
x = colpos + colwidth * 0.5 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1026 |
elif just == 'RIGHT': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1027 |
draw = self.canv.drawRightString |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1028 |
x = colpos + colwidth - cellstyle.rightPadding |
2219
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1029 |
elif just == 'DECIMAL': |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1030 |
draw = self.canv.drawAlignedString |
5c1727997169
Multiple lines and decimal alignments now supported
andy_robinson
parents:
2200
diff
changeset
|
1031 |
x = colpos + colwidth - cellstyle.rightPadding |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1032 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1033 |
raise ValueError, 'Invalid justification %s' % just |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1034 |
if n is StringType: val = cellval |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1035 |
else: val = str(cellval) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1036 |
vals = string.split(val, "\n") |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1037 |
n = len(vals) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1038 |
leading = cellstyle.leading |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1039 |
fontsize = cellstyle.fontsize |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1040 |
if valign=='BOTTOM': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1041 |
y = rowpos + cellstyle.bottomPadding+n*leading-fontsize |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1042 |
elif valign=='TOP': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1043 |
y = rowpos + rowheight - cellstyle.topPadding - fontsize |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1044 |
elif valign=='MIDDLE': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1045 |
y = rowpos + (cellstyle.bottomPadding + rowheight-cellstyle.topPadding+(n-1)*leading)/2.0 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1046 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1047 |
raise ValueError, "Bad valign: '%s'" % str(valign) |
329 | 1048 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1049 |
for v in vals: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1050 |
draw(x, y, v) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1051 |
y = y-leading |
1683 | 1052 |
|
6 | 1053 |
# for text, |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1054 |
# drawCentredString(self, x, y, text) where x is center |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1055 |
# drawRightString(self, x, y, text) where x is right |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1056 |
# drawString(self, x, y, text) where x is left |
6 | 1057 |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1058 |
_LineOpMap = { 'GRID':'_drawGrid', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1059 |
'BOX':'_drawBox', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1060 |
'OUTLINE':'_drawBox', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1061 |
'INNERGRID':'_drawInnerGrid', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1062 |
'LINEBELOW':'_drawHLinesB', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1063 |
'LINEABOVE':'_drawHLines', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1064 |
'LINEBEFORE':'_drawVLines', |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1065 |
'LINEAFTER':'_drawVLinesA', } |
6 | 1066 |
|
2194
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
1067 |
class LongTable(Table): |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
1068 |
'''Henning von Bargen's changes will be active''' |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
1069 |
_longTableOptimize = 1 |
6bc71309eb18
Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents:
2190
diff
changeset
|
1070 |
|
403 | 1071 |
LINECOMMANDS = _LineOpMap.keys() |
6 | 1072 |
|
1073 |
def _isLineCommand(cmd): |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1074 |
return cmd[0] in LINECOMMANDS |
6 | 1075 |
|
350 | 1076 |
def _setCellStyle(cellStyles, i, j, op, values): |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1077 |
#new = CellStyle('<%d, %d>' % (i,j), cellStyles[i][j]) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1078 |
#cellStyles[i][j] = new |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1079 |
## modify in place!!! |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1080 |
new = cellStyles[i][j] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1081 |
if op == 'FONT': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1082 |
n = len(values) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1083 |
new.fontname = values[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1084 |
if n>1: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1085 |
new.fontsize = values[1] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1086 |
if n>2: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1087 |
new.leading = values[2] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1088 |
else: |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1089 |
new.leading = new.fontsize*1.2 |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1090 |
elif op in ('FONTNAME', 'FACE'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1091 |
new.fontname = values[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1092 |
elif op in ('SIZE', 'FONTSIZE'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1093 |
new.fontsize = values[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1094 |
elif op == 'LEADING': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1095 |
new.leading = values[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1096 |
elif op == 'TEXTCOLOR': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1097 |
new.color = colors.toColor(values[0], colors.Color(0,0,0)) |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1098 |
elif op in ('ALIGN', 'ALIGNMENT'): |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1099 |
new.alignment = values[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1100 |
elif op == 'VALIGN': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1101 |
new.valign = values[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1102 |
elif op == 'LEFTPADDING': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1103 |
new.leftPadding = values[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1104 |
elif op == 'RIGHTPADDING': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1105 |
new.rightPadding = values[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1106 |
elif op == 'TOPPADDING': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1107 |
new.topPadding = values[0] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1108 |
elif op == 'BOTTOMPADDING': |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1109 |
new.bottomPadding = values[0] |
6 | 1110 |
|
1111 |
GRID_STYLE = TableStyle( |
|
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1112 |
[('GRID', (0,0), (-1,-1), 0.25, colors.black), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1113 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1114 |
) |
6 | 1115 |
BOX_STYLE = TableStyle( |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1116 |
[('BOX', (0,0), (-1,-1), 0.50, colors.black), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1117 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1118 |
) |
6 | 1119 |
LABELED_GRID_STYLE = TableStyle( |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1120 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1121 |
('BOX', (0,0), (-1,-1), 2, colors.black), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1122 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1123 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1124 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1125 |
) |
6 | 1126 |
COLORED_GRID_STYLE = TableStyle( |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1127 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1128 |
('BOX', (0,0), (-1,-1), 2, colors.red), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1129 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1130 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1131 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1132 |
) |
6 | 1133 |
LIST_STYLE = TableStyle( |
1677
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1134 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1135 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1136 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1137 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
1450177dd19e
Exterminated all tab characters and added a test to make sure
andy_robinson
parents:
1658
diff
changeset
|
1138 |
) |
6 | 1139 |
|
2392 | 1140 |
|
1141 |
# experimental iterator which can apply a sequence |
|
1142 |
# of colors e.g. Blue, None, Blue, None as you move |
|
1143 |
# down. |
|
1144 |
||
1145 |
||
6 | 1146 |
if __name__ == '__main__': |
2384 | 1147 |
from reportlab.test.test_platypus_tables import old_tables_test |
1148 |
old_tables_test() |