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