reportlab/platypus/tables.py
author rgbecker
Sat, 08 Jul 2000 15:30:04 +0000
changeset 338 f5eb86d4224f
parent 333 a9e94b0832a4
child 342 a6982189331d
permissions -rwxr-xr-x
Cosmetics and error testing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     1
###############################################################################
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     2
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     3
#	ReportLab Public License Version 1.0
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     4
#
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
     5
#	Except for the change of names the spirit and intention of this
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
     6
#	license is the same as that of Python
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     7
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     8
#	(C) Copyright ReportLab Inc. 1998-2000.
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     9
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    10
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    11
# All Rights Reserved
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    12
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    13
# Permission to use, copy, modify, and distribute this software and its
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    14
# documentation for any purpose and without fee is hereby granted, provided
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    15
# that the above copyright notice appear in all copies and that both that
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    16
# copyright notice and this permission notice appear in supporting
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    17
# documentation, and that the name of ReportLab not be used
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    18
# in advertising or publicity pertaining to distribution of the software
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    19
# without specific, written prior permission. 
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    20
# 
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    21
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    22
# Disclaimer
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    23
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    24
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    25
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    26
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    27
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    28
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    29
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    30
# PERFORMANCE OF THIS SOFTWARE. 
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    31
#
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    32
###############################################################################
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    33
#	$Log: tables.py,v $
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
    34
#	Revision 1.18  2000/07/08 15:30:04  rgbecker
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
    35
#	Cosmetics and error testing
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
    36
#
333
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
    37
#	Revision 1.17  2000/07/07 16:22:10  rgbecker
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
    38
#	Fix auto hieght stuff
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
    39
#	
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
    40
#	Revision 1.16  2000/07/07 10:23:36  rgbecker
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
    41
#	First attempt at VALIGN
333
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
    42
#	
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
    43
#	Revision 1.15  2000/07/06 14:05:55	rgbecker
327
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    44
#	Adjusted doc string
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
    45
#	
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
    46
#	Revision 1.14  2000/07/06 12:41:47	rgbecker
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
    47
#	First try at auto sizing
327
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    48
#	
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
    49
#	Revision 1.13  2000/06/29 17:55:19	aaron_watters
312
b39b1a2ed9d5 support explicit \n line splitting in cells
aaron_watters
parents: 268
diff changeset
    50
#	support explicit \n line splitting in cells
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
    51
#	
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
    52
#	Revision 1.12  2000/06/13 13:03:31	aaron_watters
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    53
#	more documentation changes
312
b39b1a2ed9d5 support explicit \n line splitting in cells
aaron_watters
parents: 268
diff changeset
    54
#	
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
    55
#	Revision 1.11  2000/06/01 15:23:06	rgbecker
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 248
diff changeset
    56
#	Platypus re-organisation
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    57
#	
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
    58
#	Revision 1.10  2000/05/26 09:49:23	rgbecker
248
c103b7a55e79 Color fixes; thanks to J Alet
rgbecker
parents: 221
diff changeset
    59
#	Color fixes; thanks to J Alet
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 248
diff changeset
    60
#	
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 168
diff changeset
    61
#	Revision 1.9  2000/05/16 16:15:16  rgbecker
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 168
diff changeset
    62
#	Changes related to removal of SimpleFlowDocument
248
c103b7a55e79 Color fixes; thanks to J Alet
rgbecker
parents: 221
diff changeset
    63
#	
168
02bac1346c69 Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents: 129
diff changeset
    64
#	Revision 1.8  2000/04/26 11:07:15  andy_robinson
02bac1346c69 Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents: 129
diff changeset
    65
#	Tables changed to use reportlab.lib.colors instead of
02bac1346c69 Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents: 129
diff changeset
    66
#	the six hard-coded color strings there previously.
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 168
diff changeset
    67
#	
129
623baa767856 Splitting layout.py
rgbecker
parents: 128
diff changeset
    68
#	Revision 1.7  2000/04/14 12:17:05  rgbecker
623baa767856 Splitting layout.py
rgbecker
parents: 128
diff changeset
    69
#	Splitting layout.py
168
02bac1346c69 Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents: 129
diff changeset
    70
#	
128
a0676f013314 Splitting layout.py
rgbecker
parents: 122
diff changeset
    71
#	Revision 1.6  2000/04/14 11:54:57  rgbecker
a0676f013314 Splitting layout.py
rgbecker
parents: 122
diff changeset
    72
#	Splitting layout.py
129
623baa767856 Splitting layout.py
rgbecker
parents: 128
diff changeset
    73
#	
122
fd68411f49db Drawable ==> Flowable
rgbecker
parents: 16
diff changeset
    74
#	Revision 1.5  2000/04/14 08:56:20  rgbecker
fd68411f49db Drawable ==> Flowable
rgbecker
parents: 16
diff changeset
    75
#	Drawable ==> Flowable
128
a0676f013314 Splitting layout.py
rgbecker
parents: 122
diff changeset
    76
#	
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    77
#	Revision 1.4  2000/02/17 02:09:05  rgbecker
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    78
#	Docstring & other fixes
122
fd68411f49db Drawable ==> Flowable
rgbecker
parents: 16
diff changeset
    79
#	
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    80
#	Revision 1.3  2000/02/15 17:55:59  rgbecker
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    81
#	License text fixes
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    82
#	
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    83
#	Revision 1.2  2000/02/15 15:47:09  rgbecker
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    84
#	Added license, __version__ and Logi comment
7
ca9eb4f68eab License text fixes
rgbecker
parents: 6
diff changeset
    85
#	
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
    86
__version__=''' $Id: tables.py,v 1.18 2000/07/08 15:30:04 rgbecker Exp $ '''
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
    87
__doc__="""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    88
Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    89
row order. Drawing of the table can be controlled by using a TableStyle instance. This allows control of the
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    90
color and weight of the lines (if any), and the font, alignment and padding of the text.
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    91
327
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    92
None values in the sequence of row heights or column widths, mean that the corresponding rows
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    93
or columns should be automatically sized.
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    94
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    95
All the cell values should be convertible to strings; embedded newline '\\n' characters
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    96
cause the value to wrap (ie are like a traditional linefeed).
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    97
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    98
See the test output from running this module as a script for a discussion of the method for constructing
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    99
tables and table styles.
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   100
"""
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 248
diff changeset
   101
from reportlab.platypus import *
129
623baa767856 Splitting layout.py
rgbecker
parents: 128
diff changeset
   102
from reportlab.lib.styles import PropertySet, getSampleStyleSheet
168
02bac1346c69 Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents: 129
diff changeset
   103
from reportlab.lib import colors
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 248
diff changeset
   104
from reportlab.lib.pagesizes import DEFAULT_PAGE_SIZE
312
b39b1a2ed9d5 support explicit \n line splitting in cells
aaron_watters
parents: 268
diff changeset
   105
import operator, string
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   106
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   107
_stringtype = type('')
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   108
128
a0676f013314 Splitting layout.py
rgbecker
parents: 122
diff changeset
   109
class CellStyle(PropertySet):
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   110
	defaults = {
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   111
		'fontname':'Times-Roman',
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   112
		'fontsize':10,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   113
		'leading':12,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   114
		'leftPadding':6,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   115
		'rightPadding':6,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   116
		'topPadding':3,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   117
		'bottomPadding':3,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   118
		'firstLineIndent':0,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   119
		'color':colors.black,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   120
		'alignment': 'LEFT',
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   121
		'background': (1,1,1),
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   122
		'valign': 'BOTTOM',
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   123
		}
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   124
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   125
class TableStyle:
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   126
	def __init__(self, cmds=None):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   127
		self._cmds = cmds
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   128
		if cmds is None:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   129
			self._cmds = []
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   130
	def add(self, *cmd):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   131
		self._cmds.append(cmd)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   132
	def getCommands(self):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   133
		return self._cmds
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   134
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   135
TableStyleType = type(TableStyle())
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   136
		
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 168
diff changeset
   137
class Table(Flowable):
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   138
	def __init__(self, colWidths, rowHeights, data):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   139
		if not colWidths:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   140
			raise ValueError, "Table must have at least 1 column"
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   141
		if not rowHeights:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   142
			raise ValueError, "Table must have at least 1 row"
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   143
		nrows = self._nrows = len(rowHeights)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   144
		if len(data) != nrows:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   145
			raise ValueError, "Data error - %d rows in data but %d in grid" % (len(data), nrows)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   146
		ncols = self._ncols = len(colWidths)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   147
		for i in range(nrows):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   148
			if len(data[i]) != ncols:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   149
				raise ValueError, "Not enough data points in row %d!" % i
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   150
		self._rowHeights = rowHeights
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   151
		self._colWidths = colWidths
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   152
		self._cellvalues = data
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   153
		dflt = CellStyle('<default>')
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   154
		self._cellstyles = [None]*nrows
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   155
		for i in range(nrows):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   156
			self._cellstyles[i] = [dflt]*ncols
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   157
		self._bkgrndcmds = []
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   158
		self._linecmds = []
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   159
		self._curweight = self._curcolor = self._curcellstyle = None
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   160
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   161
	def _calc(self):
333
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   162
		if hasattr(self,'_argH'):
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   163
			self._rowHeights = self._argH
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   164
			del self._argH
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   165
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   166
		if hasattr(self,'_argW'):
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   167
			self._colWidths = self._argW
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   168
			del self._argW
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   169
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   170
		H = self._rowHeights
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   171
		W = self._colWidths
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   172
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   173
		if None in H:
333
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   174
			self._argH = H
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   175
			H = H[:]	#make a copy as we'll change it
333
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   176
			self._rowHeights = H
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   177
			while None in H:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   178
				i = H.index(None)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   179
				V = self._cellvalues[i]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   180
				S = self._cellstyles[i]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   181
				h = 0
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   182
				for v, s in map(None, V, S):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   183
					if type(v) is not _stringtype: v = str(v)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   184
					v = string.split(v, "\n")
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   185
					t = s.leading*len(v)+s.bottomPadding+s.topPadding
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   186
					if t>h: h = t	#record a new maximum
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   187
				H[i] = h
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   188
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   189
		if None in W:
333
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   190
			self._argW = H
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   191
			W = W[:]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   192
			self._colWidths = W
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   193
			while None in W:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   194
				i = W.index(None)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   195
				f = lambda x,i=i: operator.getitem(x,i)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   196
				V = map(f,self._cellvalues)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   197
				S = map(f,self._cellstyles)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   198
				w = 0
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   199
				for v, s in map(None, V, S):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   200
					if type(v) is not _stringtype: v = str(v)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   201
					v = string.split(v, "\n")
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   202
					t = s.leftPadding+s.rightPadding + max(map(lambda a, b=s.fontname,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   203
								c=s.fontsize,d=self.canv: d.stringWidth(a,b,c), v))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   204
					if t>w: w = t	#record a new maximum
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   205
				W[i] = w
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   206
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   207
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   208
		height = self._height = reduce(operator.add, H, 0)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   209
		self._rowpositions = [height]	 # index 0 is actually topline; we skip when processing cells
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   210
		for h in H:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   211
			height = height - h
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   212
			self._rowpositions.append(height)
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   213
		assert abs(height)<1e-8, 'Internal height error'
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   214
		width = 0
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   215
		self._colpositions = [0]		#index -1 is right side boundary; we skip when processing cells
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   216
		for w in W:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   217
			width = width + w
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   218
			self._colpositions.append(width)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   219
		self._width = width
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   220
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   221
	def setStyle(self, tblstyle):
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   222
		if type(tblstyle) is not TableStyleType:
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   223
			tblstyle = TableStyle(tblstyle)
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   224
		for cmd in tblstyle.getCommands():
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   225
			if cmd[0] == 'BACKGROUND':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   226
				self._bkgrndcmds.append(cmd)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   227
			elif _isLineCommand(cmd):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   228
				self._linecmds.append(cmd)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   229
			else:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   230
				(op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   231
				if sc < 0: sc = sc + self._ncols
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   232
				if ec < 0: ec = ec + self._ncols
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   233
				if sr < 0: sr = sr + self._nrows
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   234
				if er < 0: er = er + self._nrows
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   235
				for i in range(sr, er+1):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   236
					for j in range(sc, ec+1):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   237
						_setCellStyle(self._cellstyles, i, j, op, values)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   238
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   239
	def _drawLines(self):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   240
		for op, (sc, sr), (ec, er), weight, color in self._linecmds:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   241
			if sc < 0: sc = sc + self._ncols
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   242
			if ec < 0: ec = ec + self._ncols
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   243
			if sr < 0: sr = sr + self._nrows
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   244
			if er < 0: er = er + self._nrows
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   245
			if op == 'GRID':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   246
				self._drawBox( (sc, sr), (ec, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   247
				self._drawInnerGrid( (sc, sr), (ec, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   248
			elif op in ('BOX',	'OUTLINE',):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   249
				self._drawBox( (sc, sr), (ec, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   250
			elif op == 'INNERGRID':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   251
				self._drawInnerGrid( (sc, sr), (ec, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   252
			elif op == 'LINEBELOW':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   253
				self._drawHLines((sc, sr+1), (ec, er+1), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   254
			elif op == 'LINEABOVE':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   255
				self._drawHLines((sc, sr), (ec, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   256
			elif op == 'LINEBEFORE':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   257
				self._drawVLines((sc, sr), (ec, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   258
			elif op == 'LINEAFTER':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   259
				self._drawVLines((sc+1, sr), (ec+1, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   260
			else:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   261
				raise ValueError, "Unknown line style %s" % op
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   262
		self._curcolor = None
248
c103b7a55e79 Color fixes; thanks to J Alet
rgbecker
parents: 221
diff changeset
   263
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   264
	def _drawBox(self,	(sc, sr), (ec, er), weight, color):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   265
		self._drawHLines((sc, sr), (ec, sr), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   266
		self._drawHLines((sc, er+1), (ec, er+1), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   267
		self._drawVLines((sc, sr), (sc, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   268
		self._drawVLines((ec+1, sr), (ec+1, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   269
	def _drawInnerGrid(self, (sc, sr), (ec, er), weight, color):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   270
		self._drawHLines((sc, sr+1), (ec, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   271
		self._drawVLines((sc+1, sr), (ec, er), weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   272
	def _prepLine(self, weight, color):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   273
		if color != self._curcolor:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   274
			self.canv.setStrokeColor(color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   275
			self._curcolor = color
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   276
		if weight != self._curweight:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   277
			self.canv.setLineWidth(weight)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   278
			self._curweight = weight
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   279
	def _drawHLines(self, (sc, sr), (ec, er), weight, color):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   280
		self._prepLine(weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   281
		scp = self._colpositions[sc]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   282
		ecp = self._colpositions[ec+1]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   283
		for rowpos in self._rowpositions[sr:er+1]:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   284
			self.canv.line(scp, rowpos, ecp, rowpos)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   285
	def _drawVLines(self, (sc, sr), (ec, er), weight, color):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   286
		self._prepLine(weight, color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   287
		srp = self._rowpositions[sr]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   288
		erp = self._rowpositions[er+1]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   289
		for colpos in self._colpositions[sc:ec+1]:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   290
			self.canv.line(colpos, srp, colpos, erp)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   291
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   292
	def wrap(self, availWidth, availHeight):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   293
		self._calc()
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   294
		#nice and easy, since they are predetermined size
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   295
		self.availWidth = availWidth
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   296
		return (self._width, self._height)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   297
				
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   298
	def draw(self):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   299
		nudge = 0.5 * (self.availWidth - self._width)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   300
		self.canv.translate(nudge, 0)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   301
		self._drawBkgrnd()
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   302
		self._drawLines()
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   303
		for row, rowstyle, rowpos, rowheight in map(None, self._cellvalues, self._cellstyles, self._rowpositions[1:], self._rowHeights):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   304
			for cellval, cellstyle, colpos, colwidth in map(None, row, rowstyle, self._colpositions[:-1], self._colWidths):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   305
				self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   306
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   307
	def _drawBkgrnd(self):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   308
		for cmd, (sc, sr), (ec, er), color in self._bkgrndcmds:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   309
			if sc < 0: sc = sc + self._ncols
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   310
			if ec < 0: ec = ec + self._ncols
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   311
			if sr < 0: sr = sr + self._nrows
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   312
			if er < 0: er = er + self._nrows
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   313
			color = colors.toColor(color, colors.Color(1,1,1))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   314
			x0 = self._colpositions[sc]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   315
			y0 = self._rowpositions[sr]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   316
			x1 = self._colpositions[ec+1]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   317
			y1 = self._rowpositions[er+1]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   318
			self.canv.setFillColor(color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   319
			self.canv.rect(x0, y0, x1-x0, y1-y0,stroke=0,fill=1)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   320
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   321
	def _drawCell(self, cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   322
		#print "cellstyle is ", repr(cellstyle), id(cellstyle)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   323
		if self._curcellstyle is not cellstyle:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   324
			cur = self._curcellstyle
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   325
			if cur is None or cellstyle.color != cur.color:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   326
				#print "setting cell color to %s" % `cellstyle.color`
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   327
				self.canv.setFillColor(cellstyle.color)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   328
			if cur is None or cellstyle.leading != cur.leading or cellstyle.fontname != cur.fontname or cellstyle.fontsize != cur.fontsize:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   329
				#print "setting font: %s, %s, %s" % (cellstyle.fontname, cellstyle.fontsize, cellstyle.leading)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   330
				self.canv.setFont(cellstyle.fontname, cellstyle.fontsize, cellstyle.leading)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   331
			self._curcellstyle = cellstyle
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   332
		#print "leading is ", cellstyle.leading, "size is", cellstyle.fontsize
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   333
		just = cellstyle.alignment
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   334
		#print "alignment is ", just
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   335
		if just == 'LEFT':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   336
			draw = self.canv.drawString
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   337
			x = colpos + cellstyle.leftPadding
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   338
		elif just in ('CENTRE', 'CENTER'):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   339
			draw = self.canv.drawCentredString
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   340
			x = colpos + colwidth * 0.5
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   341
		elif just == 'RIGHT':
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   342
			draw = self.canv.drawRightString
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   343
			x = colpos + colwidth - cellstyle.rightPadding
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   344
		else:
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   345
			raise ValueError, 'Invalid justification %s' % just
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   346
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   347
		if type(cellval) is _stringtype:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   348
			val = cellval
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   349
		else:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   350
			val = str(cellval)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   351
		vals = string.split(val, "\n")
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   352
		n = len(vals)-1
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   353
		leading = cellstyle.leading
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   354
		valign = cellstyle.valign
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   355
		if valign=='BOTTOM':
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   356
			y = rowpos + cellstyle.bottomPadding+n*leading
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   357
		elif valign=='TOP':
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   358
			y = rowpos + rowheight - cellstyle.topPadding - cellstyle.fontsize
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   359
		elif valign=='MIDDLE':
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   360
			y = rowpos + (cellstyle.bottomPadding + rowheight - cellstyle.topPadding+n*leading)/2.0
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   361
		else:
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   362
			raise ValueError, "Bad valign: '%s'" % str(valign)
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   363
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   364
		for v in vals:
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   365
			draw(x, y, v)
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   366
			y = y-leading
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   367
		
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   368
# for text,
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   369
#	drawCentredString(self, x, y, text) where x is center
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   370
#	drawRightString(self, x, y, text) where x is right
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   371
#	drawString(self, x, y, text) where x is left
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   372
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   373
LINECOMMANDS = (
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   374
	'GRID', 'BOX', 'OUTLINE', 'INNERGRID', 'LINEBELOW', 'LINEABOVE', 'LINEBEFORE', 'LINEAFTER', )
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   375
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   376
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   377
def _isLineCommand(cmd):
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   378
	return cmd[0] in LINECOMMANDS
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   379
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   380
def _setCellStyle(cellstyles, i, j, op, values):
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   381
	new = CellStyle('<%d, %d>' % (i,j), cellstyles[i][j])
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   382
	cellstyles[i][j] = new
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   383
	if op == 'FONT':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   384
		new.fontname = values[0]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   385
		new.fontsize = values[1]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   386
	elif op == 'TEXTCOLOR':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   387
		new.color = colors.toColor(values[0], colors.Color(0,0,0))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   388
	elif op in ('ALIGN', 'ALIGNMENT'):
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   389
		new.alignment = values[0]
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   390
	elif op == 'VALIGN':
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   391
		new.valign = values[0]
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   392
	elif op == 'LEFTPADDING':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   393
		new.leftPadding = values[0]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   394
	elif op == 'RIGHTPADDING':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   395
		new.rightPadding = values[0]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   396
	elif op == 'TOPPADDING':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   397
		new.topPadding = values[0]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   398
	elif op == 'BOTTOMPADDING':
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   399
		new.bottomPadding = values[0]
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   400
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   401
GRID_STYLE = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   402
	[('GRID', (0,0), (-1,-1), 0.25, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   403
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   404
	)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   405
BOX_STYLE = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   406
	[('BOX', (0,0), (-1,-1), 0.50, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   407
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   408
	)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   409
LABELED_GRID_STYLE = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   410
	[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   411
	 ('BOX', (0,0), (-1,-1), 2, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   412
	 ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   413
	 ('LINEAFTER', (0,0), (0,-1), 2, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   414
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   415
	)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   416
COLORED_GRID_STYLE = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   417
	[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   418
	 ('BOX', (0,0), (-1,-1), 2, colors.red),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   419
	 ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   420
	 ('LINEAFTER', (0,0), (0,-1), 2, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   421
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   422
	)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   423
LIST_STYLE = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   424
	[('LINEABOVE', (0,0), (-1,0), 2, colors.green),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   425
	 ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   426
	 ('LINEBELOW', (0,-1), (-1,-1), 2, colors.green),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   427
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   428
	)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   429
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   430
def test():
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   431
	from reportlab.lib.units import inch
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   432
	rowheights = (24, 16, 16, 16, 16)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   433
	rowheights2 = (24, 16, 16, 16, 30)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   434
	colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   435
	data = (
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   436
		('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   437
		('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   438
		('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   439
		('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   440
		('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843')
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   441
		)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   442
	data2 = (
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   443
		('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   444
		('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   445
		('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   446
		('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   447
		('Hats\nLarge', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843')
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   448
		)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   449
	styleSheet = getSampleStyleSheet()
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   450
	lst = []
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   451
	lst.append(Paragraph("Tables", styleSheet['Heading1']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   452
	lst.append(Paragraph(__doc__, styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   453
	lst.append(Paragraph("The Tables (shown in different styles below) were created using the following code:", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   454
	lst.append(Preformatted("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   455
	colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   456
	rowheights = (24, 16, 16, 16, 16)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   457
	data = (
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   458
		('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun',
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   459
		   'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   460
		('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   461
		('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   462
		('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   463
		('Hats', 893, 912, '1,212', 643, 789, 159,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   464
			 888, '1,298', 832, 453, '1,344','2,843')
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   465
		)	
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   466
	t = Table(colwidths, rowheights,  data)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   467
	""", styleSheet['Code'], dedent=4))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   468
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   469
	You can then give the Table a TableStyle object to control its format. The first TableStyle used was
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   470
	created as follows:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   471
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   472
	lst.append(Preformatted("""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   473
GRID_STYLE = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   474
	[('GRID', (0,0), (-1,-1), 0.25, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   475
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   476
	)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   477
	""", styleSheet['Code']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   478
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   479
	TableStyles are created by passing in a list of commands. There are two types of commands - line commands
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   480
	and cell formatting commands. In all cases, the first three elements of a command are the command name,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   481
	the starting cell and the ending cell.
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   482
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   483
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   484
	Line commands always follow this with the weight and color of the desired lines. Colors can be names,
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   485
	or they can be specified as a (R,G,B) tuple, where R, G and B are floats and (0,0,0) is black. The line
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   486
	command names are: GRID, BOX, OUTLINE, INNERGRID, LINEBELOW, LINEABOVE, LINEBEFORE
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   487
	and LINEAFTER. BOX and OUTLINE are equivalent, and GRID is the equivalent of applying both BOX and
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   488
	INNERGRID.
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   489
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   490
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   491
	Cell formatting commands are:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   492
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   493
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   494
	FONT - takes fontname, fontsize and (optional) leading.
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   495
	""", styleSheet['Definition']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   496
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   497
	TEXTCOLOR - takes a color name or (R,G,B) tuple.
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   498
	""", styleSheet['Definition']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   499
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   500
	ALIGNMENT (or ALIGN) - takes one of LEFT, RIGHT and CENTRE (or CENTER).
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   501
	""", styleSheet['Definition']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   502
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   503
	LEFTPADDING - defaults to 6.
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   504
	""", styleSheet['Definition']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   505
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   506
	RIGHTPADDING - defaults to 6.
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   507
	""", styleSheet['Definition']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   508
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   509
	BOTTOMPADDING - defaults to 3.
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   510
	""", styleSheet['Definition']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   511
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   512
	A tablestyle is applied to a table by calling Table.setStyle(tablestyle).
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   513
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   514
	t = Table(colwidths, rowheights,  data)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   515
	t.setStyle(GRID_STYLE)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   516
	lst.append(PageBreak())
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   517
	lst.append(Paragraph("This is GRID_STYLE\n", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   518
	lst.append(t)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   519
	
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   520
	t = Table(colwidths, rowheights,  data)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   521
	t.setStyle(BOX_STYLE)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   522
	lst.append(Paragraph("This is BOX_STYLE\n", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   523
	lst.append(t)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   524
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   525
	It was created as follows:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   526
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   527
	lst.append(Preformatted("""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   528
BOX_STYLE = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   529
	[('BOX', (0,0), (-1,-1), 0.50, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   530
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   531
	)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   532
	""", styleSheet['Code']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   533
	
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   534
	t = Table(colwidths, rowheights,  data)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   535
	t.setStyle(LABELED_GRID_STYLE)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   536
	lst.append(Paragraph("This is LABELED_GRID_STYLE\n", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   537
	lst.append(t)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   538
	t = Table(colwidths, rowheights2,  data2)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   539
	t.setStyle(LABELED_GRID_STYLE)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   540
	lst.append(Paragraph("This is LABELED_GRID_STYLE ILLUSTRATES EXPLICIT LINE SPLITTING WITH NEWLINE (different heights and data)\n", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   541
	lst.append(t)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   542
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   543
	It was created as follows:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   544
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   545
	lst.append(Preformatted("""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   546
LABELED_GRID_STYLE = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   547
	[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   548
	 ('BOX', (0,0), (-1,-1), 2, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   549
	 ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   550
	 ('LINEAFTER', (0,0), (0,-1), 2, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   551
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   552
	)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   553
	""", styleSheet['Code']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   554
	lst.append(PageBreak())
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   555
	
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   556
	t = Table(colwidths, rowheights,  data)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   557
	t.setStyle(COLORED_GRID_STYLE)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   558
	lst.append(Paragraph("This is COLORED_GRID_STYLE\n", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   559
	lst.append(t)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   560
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   561
	It was created as follows:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   562
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   563
	lst.append(Preformatted("""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   564
COLORED_GRID_STYLE = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   565
	[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   566
	 ('BOX', (0,0), (-1,-1), 2, colors.red),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   567
	 ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   568
	 ('LINEAFTER', (0,0), (0,-1), 2, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   569
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   570
	)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   571
	""", styleSheet['Code']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   572
	
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   573
	t = Table(colwidths, rowheights,  data)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   574
	t.setStyle(LIST_STYLE)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   575
	lst.append(Paragraph("This is LIST_STYLE\n", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   576
	lst.append(t)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   577
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   578
	It was created as follows:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   579
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   580
	lst.append(Preformatted("""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   581
LIST_STYLE = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   582
	[('LINEABOVE', (0,0), (-1,0), 2, colors.green),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   583
	 ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   584
	 ('LINEBELOW', (0,-1), (-1,-1), 2, colors.green),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   585
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   586
	)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   587
	""", styleSheet['Code']))
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   588
   
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   589
	t = Table(colwidths, rowheights,  data)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   590
	ts = TableStyle(
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   591
	[('LINEABOVE', (0,0), (-1,0), 2, colors.green),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   592
	 ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   593
	 ('LINEBELOW', (0,-1), (-1,-1), 2, colors.green),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   594
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   595
	 ('TEXTCOLOR', (0,1), (0,-1), colors.red),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   596
	 ('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   597
	)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   598
	t.setStyle(ts)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   599
	lst.append(Paragraph("This is a custom style\n", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   600
	lst.append(t)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   601
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   602
	It was created as follows:
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   603
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   604
	lst.append(Preformatted("""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   605
   ts = TableStyle(
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   606
	[('LINEABOVE', (0,0), (-1,0), 2, colors.green),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   607
	 ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   608
	 ('LINEBELOW', (0,-1), (-1,-1), 2, colors.green),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   609
	 ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   610
	 ('TEXTCOLOR', (0,1), (0,-1), colors.red),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   611
	 ('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))]
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   612
	)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   613
	""", styleSheet['Code']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   614
	data = (
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   615
		('', 'Jan\nCold', 'Feb\n', 'Mar\n','Apr\n','May\n', 'Jun\nHot', 'Jul\n', 'Aug\nThunder', 'Sep\n', 'Oct\n', 'Nov\n', 'Dec\n'),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   616
		('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   617
		('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   618
		('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   619
		('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843')
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   620
		)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   621
	c = list(colwidths)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   622
	c[0] = None
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   623
	c[8] = None
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   624
	t = Table(c, [None]+list(rowheights[1:]),  data)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   625
	t.setStyle(LIST_STYLE)
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   626
	lst.append(Paragraph("""
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   627
		This is a LIST_STYLE table with the first rowheight set to None ie automatic.
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   628
		The top row cells are split at a newline '\\n' character. The first and August
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   629
		column widths were also set to None.
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   630
	""", styleSheet['BodyText']))
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   631
	lst.append(t)
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   632
	lst.append(Paragraph("""
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   633
		The red numbers should be aligned LEFT &amp; BOTTOM, the blue RIGHT &amp; TOP
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   634
		and the green CENTER &amp; MIDDLE.
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   635
	""", styleSheet['BodyText']))
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   636
	data=	[['X00y', 'X01y', 'X02y', 'X03y', 'X04y'],
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   637
			['X10y', 'X11y', 'X12y', 'X13y', 'X14y'],
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   638
			['X20y', 'X21y', 'X22y', 'X23y', 'X24y'],
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   639
			['X30y', 'X31y', 'X32y', 'X33y', 'X34y']]
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   640
	t=Table(5*[0.4*inch], 4*[0.4*inch], data)
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   641
	t.setStyle([('ALIGN',(1,1),(-2,-2),'LEFT'),
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   642
				('TEXTCOLOR',(1,1),(-2,-2),colors.red),
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   643
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   644
				('VALIGN',(0,0),(1,-1),'TOP'),
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   645
				('ALIGN',(0,0),(1,-1),'RIGHT'),
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   646
				('TEXTCOLOR',(0,0),(1,-1),colors.blue),
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   647
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   648
				('ALIGN',(0,-1),(-1,-1),'CENTER'),
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   649
				('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   650
				('TEXTCOLOR',(0,-1),(-1,-1),colors.green),
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   651
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   652
				('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   653
				('BOX', (0,0), (-1,-1), 0.25, colors.black),
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   654
				])
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
   655
	lst.append(t)
333
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   656
	data = [('alignment', 'align\012alignment'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   657
			('bulletColor', 'bulletcolor\012bcolor'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   658
			('bulletFontName', 'bfont\012bulletfontname'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   659
			('bulletFontSize', 'bfontsize\012bulletfontsize'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   660
			('bulletIndent', 'bindent\012bulletindent'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   661
			('firstLineIndent', 'findent\012firstlineindent'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   662
			('fontName', 'face\012fontname\012font'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   663
			('fontSize', 'size\012fontsize'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   664
			('leading', 'leading'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   665
			('leftIndent', 'leftindent\012lindent'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   666
			('rightIndent', 'rightindent\012rindent'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   667
			('spaceAfter', 'spaceafter\012spacea'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   668
			('spaceBefore', 'spacebefore\012spaceb'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   669
			('textColor', 'fg\012textcolor\012color')]
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   670
	t = Table(2*[None], len(data)*[None], data)
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   671
	t.setStyle([
333
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   672
            ('VALIGN',(0,0),(-1,-1),'TOP'),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   673
            ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   674
            ('BOX', (0,0), (-1,-1), 0.25, colors.black),
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   675
            ])
333
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   676
	lst.append(t)
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   677
	SimpleDocTemplate('testtables.pdf', showBoundary=1).build(lst)
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   678
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   679
if __name__ == '__main__':
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   680
	test()