reportlab/platypus/tables.py
author andy_robinson
Fri, 12 Mar 2004 23:54:11 +0000
changeset 2219 5c1727997169
parent 2200 be0cfccc662a
child 2220 7a77486a7ea7
permissions -rwxr-xr-x
Multiple lines and decimal alignments now supported
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 421
diff changeset
     1
#copyright ReportLab Inc. 2000
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 421
diff changeset
     2
#see license.txt for license details
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 421
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/tables.py?cvsroot=reportlab
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/platypus/tables.py,v 1.73 2004/03/12 23:54:11 andy_robinson Exp $
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
     5
__version__=''' $Id: tables.py,v 1.73 2004/03/12 23:54:11 andy_robinson Exp $ '''
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
     6
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
     7
__doc__="""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     8
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
     9
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
    10
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
    11
327
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    12
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
    13
or columns should be automatically sized.
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    14
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    15
All the cell values should be convertible to strings; embedded newline '\\n' characters
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    16
cause the value to wrap (ie are like a traditional linefeed).
0944cd1f3f80 Adjusted doc string
rgbecker
parents: 326
diff changeset
    17
268
8414113fa500 more documentation changes
aaron_watters
parents: 253
diff changeset
    18
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
    19
tables and table styles.
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    20
"""
906
04370e5a8cfa hacky fix to nudge bug
aaron_watters
parents: 904
diff changeset
    21
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
    22
from reportlab.platypus.flowables import Flowable, Image, Macro, PageBreak, Preformatted, Spacer, XBox, \
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
    23
                         CondPageBreak, KeepTogether, TraceInfo, FailOnWrap, FailOnDraw
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
    24
from reportlab.platypus.paragraph import Paragraph, cleanBlockQuotedText, ParaLines
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
    25
from reportlab.platypus.paraparser import ParaFrag
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
    26
from reportlab.platypus.frames import Frame
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
    27
from reportlab.platypus.doctemplate import BaseDocTemplate, NextPageTemplate, PageTemplate, ActionFlowable, \
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
    28
                         SimpleDocTemplate, FrameBreak, PageBegin
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
    29
from reportlab.platypus.xpreformatted import XPreformatted
1207
49a514d10cb0 make tables one point off not bomb mysteriously in paid production installations.
aaron_watters
parents: 1169
diff changeset
    30
from reportlab import rl_config
1533
e25c4dda4840 Added emptyTableAction
rgbecker
parents: 1499
diff changeset
    31
from reportlab.lib.styles import PropertySet, getSampleStyleSheet, ParagraphStyle
168
02bac1346c69 Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents: 129
diff changeset
    32
from reportlab.lib import colors
1103
857af510186d Added identity method to Flowables
rgbecker
parents: 906
diff changeset
    33
from reportlab.lib.utils import fp_str
403
05b3be301e19 Started debugging Table split
rgbecker
parents: 361
diff changeset
    34
from reportlab.pdfbase import pdfmetrics
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
    35
312
b39b1a2ed9d5 support explicit \n line splitting in cells
aaron_watters
parents: 268
diff changeset
    36
import operator, string
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    37
421
6dd1e6e707b7 Flowable cell fixes/changes
rgbecker
parents: 420
diff changeset
    38
from types import TupleType, ListType, StringType
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    39
128
a0676f013314 Splitting layout.py
rgbecker
parents: 122
diff changeset
    40
class CellStyle(PropertySet):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    41
    defaults = {
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    42
        'fontname':'Times-Roman',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    43
        'fontsize':10,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    44
        'leading':12,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    45
        'leftPadding':6,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    46
        'rightPadding':6,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    47
        'topPadding':3,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    48
        'bottomPadding':3,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    49
        'firstLineIndent':0,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    50
        'color':colors.black,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    51
        'alignment': 'LEFT',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    52
        'background': (1,1,1),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    53
        'valign': 'BOTTOM',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    54
        }
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    55
1699
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
    56
LINECAPS={'butt':0,'round':1,'projecting':2,'squared':2}
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
    57
LINEJOINS={'miter':0,'round':1,'bevel':2}
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
    58
730
48a169b27fe4 changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents: 684
diff changeset
    59
# experimental replacement
48a169b27fe4 changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents: 684
diff changeset
    60
class CellStyle1(PropertySet):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    61
    fontname = "Times-Roman"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    62
    fontsize = 10
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    63
    leading = 12
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    64
    leftPadding = 6
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    65
    rightPadding = 6
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    66
    topPadding = 3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    67
    bottomPadding = 3
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    68
    firstLineIndent = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    69
    color = colors.black
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    70
    alignment = 'LEFT'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    71
    background = (1,1,1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    72
    valign = "BOTTOM"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    73
    def __init__(self, name, parent=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    74
        self.name = name
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    75
        if parent is not None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    76
            parent.copy(self)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    77
    def copy(self, result=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    78
        if result is None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    79
            result = CellStyle1()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    80
        for name in dir(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    81
            setattr(result, name, gettattr(self, name))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    82
        return result
730
48a169b27fe4 changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents: 684
diff changeset
    83
48a169b27fe4 changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents: 684
diff changeset
    84
CellStyle = CellStyle1
48a169b27fe4 changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents: 684
diff changeset
    85
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    86
class TableStyle:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    87
    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
    88
        #handle inheritance from parent first.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    89
        commands = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    90
        if parent:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    91
            # 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
    92
            commands = commands + parent.getCommands()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    93
            self._opts = parent._opts
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    94
        if cmds:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    95
            commands = commands + list(cmds)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    96
        self._cmds = commands
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    97
        self._opts={}
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
    98
        self._opts.update(kw)
1435
4d0f57749e18 Added _opts to TableStyle for possible keepWithNext
rgbecker
parents: 1253
diff changeset
    99
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   100
    def add(self, *cmd):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   101
        self._cmds.append(cmd)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   102
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   103
        L = map(repr, self._cmds)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   104
        import string
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   105
        L = string.join(L, "  \n")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   106
        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
   107
    def getCommands(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   108
        return self._cmds
338
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   109
f5eb86d4224f Cosmetics and error testing
rgbecker
parents: 333
diff changeset
   110
TableStyleType = type(TableStyle())
419
469fe11a010b Additions/Improvements to LINE CMD Splitting
rgbecker
parents: 403
diff changeset
   111
_SeqTypes = (TupleType, ListType)
356
377367fe28cb Table argument order changed
rgbecker
parents: 354
diff changeset
   112
377367fe28cb Table argument order changed
rgbecker
parents: 354
diff changeset
   113
def _rowLen(x):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   114
    return type(x) not in _SeqTypes and 1 or len(x)
419
469fe11a010b Additions/Improvements to LINE CMD Splitting
rgbecker
parents: 403
diff changeset
   115
356
377367fe28cb Table argument order changed
rgbecker
parents: 354
diff changeset
   116
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 168
diff changeset
   117
class Table(Flowable):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   118
    def __init__(self, data, colWidths=None, rowHeights=None, style=None,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   119
                repeatRows=0, repeatCols=0, splitByRow=1, emptyTableAction=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   120
        #print "colWidths", colWidths
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   121
        self.hAlign = 'CENTER'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   122
        self.vAlign = 'MIDDLE'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   123
        if type(data) not in _SeqTypes:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   124
            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
   125
        self._nrows = nrows = len(data)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   126
        if nrows: self._ncols = ncols = max(map(_rowLen,data))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   127
        elif colWidths: ncols = len(colWidths)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   128
        else: ncols = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   129
        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
   130
        if not (nrows and ncols):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   131
            if emptyTableAction=='error':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   132
                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
   133
            elif emptyTableAction=='indicate':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   134
                self.__class__ = Preformatted
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   135
                global _emptyTableStyle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   136
                if '_emptyTableStyle' not in globals().keys():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   137
                    _emptyTableStyle = ParagraphStyle('_emptyTableStyle')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   138
                    _emptyTableStyle.textColor = colors.red
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   139
                    _emptyTableStyle.backColor = colors.yellow
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   140
                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
   141
            elif emptyTableAction=='ignore':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   142
                self.__class__ = Spacer
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   143
                Spacer.__init__(self,0,0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   144
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   145
                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
   146
            return
1533
e25c4dda4840 Added emptyTableAction
rgbecker
parents: 1499
diff changeset
   147
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   148
        if colWidths is None: colWidths = ncols*[None]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   149
        elif len(colWidths) != ncols:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   150
            raise ValueError, "%s data error - %d columns in data but %d in grid" % (self.identity(),ncols, len(colWidths))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   151
        if rowHeights is None: rowHeights = nrows*[None]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   152
        elif len(rowHeights) != nrows:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   153
            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
   154
        for i in range(nrows):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   155
            if len(data[i]) != ncols:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   156
                raise ValueError, "%s not enough data points in row %d!" % (self.identity(),i)
1989
a91ec8282d6e Attempt to fix up identity for early errors
rgbecker
parents: 1917
diff changeset
   157
        self._cellvalues = data
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   158
        self._rowHeights = self._argH = rowHeights
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   159
        self._colWidths = self._argW = colWidths
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   160
        cellrows = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   161
        for i in range(nrows):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   162
            cellcols = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   163
            for j in range(ncols):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   164
                cellcols.append(CellStyle(`(i,j)`))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   165
            cellrows.append(cellcols)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   166
        self._cellStyles = cellrows
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   167
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   168
        self._bkgrndcmds = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   169
        self._linecmds = []
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   170
        self._spanCmds = []
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   171
        self.repeatRows = repeatRows
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   172
        self.repeatCols = repeatCols
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   173
        self.splitByRow = splitByRow
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   174
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   175
        if style:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   176
            self.setStyle(style)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   177
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   178
        "incomplete, but better than nothing"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   179
        r = self._rowHeights
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   180
        c = self._colWidths
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   181
        cv = self._cellvalues
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   182
        import pprint, string
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   183
        cv = pprint.pformat(cv)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   184
        cv = string.replace(cv, "\n", "\n  ")
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   185
        return "%s(\n rowHeights=%s,\n colWidths=%s,\n%s\n) # end table" % (self.__class__.__name__,r,c,cv)
342
a6982189331d Added tables to PythonPoint
andy_robinson
parents: 338
diff changeset
   186
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   187
    def identity(self, maxLen=30):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   188
        '''Identify our selves as well as possible'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   189
        vx = None
1989
a91ec8282d6e Attempt to fix up identity for early errors
rgbecker
parents: 1917
diff changeset
   190
        nr = getattr(self,'_nrows','unknown')
a91ec8282d6e Attempt to fix up identity for early errors
rgbecker
parents: 1917
diff changeset
   191
        nc = getattr(self,'_ncols','unknown')
a91ec8282d6e Attempt to fix up identity for early errors
rgbecker
parents: 1917
diff changeset
   192
        cv = self._cellvalues
a91ec8282d6e Attempt to fix up identity for early errors
rgbecker
parents: 1917
diff changeset
   193
        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
   194
            b = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   195
            for i in xrange(nr):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   196
                for j in xrange(nc):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   197
                    v = cv[i][j]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   198
                    t = type(v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   199
                    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
   200
                        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
   201
                        r = ''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   202
                        for vij in v:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   203
                            r = vij.identity(maxLen)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   204
                            if r and r[-4:]!='>...':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   205
                                break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   206
                        if r and r[-4:]!='>...':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   207
                            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
   208
                    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   209
                        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
   210
                        ix, jx, vx = i, j, v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   211
                        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
   212
                        if maxLen: vx = vx[:maxLen]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   213
                    if b: break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   214
                if b: break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   215
        if vx:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   216
            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
   217
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   218
            vx = '...'
1103
857af510186d Added identity method to Flowables
rgbecker
parents: 906
diff changeset
   219
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   220
        return "<%s at %d %d rows x %s cols>%s" % (self.__class__.__name__, id(self), nr, nc, vx)
1103
857af510186d Added identity method to Flowables
rgbecker
parents: 906
diff changeset
   221
2189
47cb423f5f1a Make _listCellGeom more specific
rgbecker
parents: 2185
diff changeset
   222
    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
   223
        aW = w-s.leftPadding-s.rightPadding
2189
47cb423f5f1a Make _listCellGeom more specific
rgbecker
parents: 2185
diff changeset
   224
        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
   225
        t = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   226
        w = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   227
        canv = getattr(self,'canv',None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   228
        for v in V:
2189
47cb423f5f1a Make _listCellGeom more specific
rgbecker
parents: 2185
diff changeset
   229
            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
   230
            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
   231
            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
   232
            w = max(w,vw)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   233
            t = t + vh + v.getSpaceBefore()+v.getSpaceAfter()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   234
        return w, t - V[0].getSpaceBefore()-V[-1].getSpaceAfter()
1492
612646d068ca Ensure canv is set on sub flowables
rgbecker
parents: 1488
diff changeset
   235
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   236
    def _calc_width(self):
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   237
        #comments added by Andy to Robin's slightly
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   238
        #terse variable names
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   239
        W = self._argW  #widths array
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   240
        #print 'widths array = %s' % str(self._colWidths)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   241
        canv = getattr(self,'canv',None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   242
        saved = None
1596
753865895a6f Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents: 1536
diff changeset
   243
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   244
        if None in W:  #some column widths are not given
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   245
            if self._spanCmds:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   246
                colspans = self._colSpannedCells
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   247
            else:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   248
                colspans = {}
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   249
##            k = colspans.keys()
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   250
##            k.sort()
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   251
##            print 'the following cells are part of spanned ranges: %s' % k
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   252
            W = W[:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   253
            self._colWidths = W
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   254
            while None in W:
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   255
                j = W.index(None) #find first unspecified column
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   256
                #print 'sizing column %d' % j
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   257
                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
   258
                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
   259
                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
   260
                w = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   261
                i = 0
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   262
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   263
                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
   264
                    #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
   265
                    #assume a zero size.
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   266
                    if colspans.has_key((j, i)):
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   267
                        #print 'sizing a spanned cell (%d, %d) with content "%s"' % (j, i, str(v))
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   268
                        t = 0.0
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   269
                    else:#work out size
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   270
                        t = self._elementWidth(v,s)
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   271
                        if t is None:
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   272
                            raise ValueError, "Flowable %s in cell(%d,%d) can't have auto width\n%s" % (v.identity(30),i,j,self.identity(30))
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   273
                        t = t + s.leftPadding+s.rightPadding
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   274
                    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
   275
                    i = i + 1
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   276
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   277
                #print 'max width for column %d is %0.2f' % (j, w)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   278
                W[j] = w
1596
753865895a6f Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents: 1536
diff changeset
   279
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   280
        width = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   281
        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
   282
        for w in W:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   283
            #print w, width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   284
            width = width + w
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   285
            self._colpositions.append(width)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   286
        #print "final width", width
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   287
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   288
        self._width = width
1596
753865895a6f Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents: 1536
diff changeset
   289
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   290
    def _elementWidth(self,v,s):
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   291
        t = type(v)
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   292
        if t in _SeqTypes:
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   293
            w = 0
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   294
            for e in v:
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   295
                ew = self._elementWidth(self,v)
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   296
                if ew is None: return None
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   297
                w = max(w,ew)
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   298
            return w
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   299
        elif isinstance(v,Flowable) and v._fixedWidth:
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   300
            return v.width
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   301
        else:
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   302
            if t is not StringType: v = v is None and '' or str(v)
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   303
            v = string.split(v, "\n")
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   304
            return max(map(lambda a, b=s.fontname, c=s.fontsize,d=pdfmetrics.stringWidth: d(a,b,c), v))
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   305
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   306
    def _calc_height(self, availHeight):
333
a9e94b0832a4 Fix auto hieght stuff
rgbecker
parents: 329
diff changeset
   307
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   308
        H = self._argH
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   309
        W = self._argW
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   310
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   311
        canv = getattr(self,'canv',None)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   312
        saved = None
1596
753865895a6f Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents: 1536
diff changeset
   313
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   314
        #get a handy list of any cells which span rows.
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   315
        #these should be ignored for sizing
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   316
        if self._spanCmds:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   317
            spans = self._rowSpannedCells
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   318
        else:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   319
            spans = {}
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   320
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   321
        hmax = lim = len(H)
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   322
        longTable = getattr(self,'_longTableOptimize',None)
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   323
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   324
        if None in H:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   325
            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
   326
            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
   327
            self._rowHeights = H
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   328
            while None in H:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   329
                i = H.index(None)
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   330
                if longTable:
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   331
                    hmax = i
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   332
                    height = reduce(operator.add, H[:i], 0)
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   333
                    # 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
   334
                    if height > availHeight: break
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   335
                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
   336
                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
   337
                h = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   338
                j = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   339
                for v, s, w in map(None, V, S, W): # value, style, width (lengths must match)
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   340
                    if spans.has_key((j, i)):
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   341
                        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
   342
                    else:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   343
                        t = type(v)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   344
                        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
   345
                            if not t in _SeqTypes: v = (v,)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   346
                            if w is None:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   347
                                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
   348
                            if canv: canv._fontname, canv._fontsize, canv._leading = s.fontname, s.fontsize, s.leading or 1.2*s.fontsize
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   349
                            dW,t = self._listCellGeom(v,w,s)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   350
                            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
   351
                            #print "leftpadding, rightpadding", s.leftPadding, s.rightPadding
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   352
                            dW = dW + s.leftPadding + s.rightPadding
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   353
                            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
   354
                                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
   355
                        else:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   356
                            if t is not StringType:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   357
                                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
   358
                            v = string.split(v, "\n")
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   359
                            t = s.leading*len(v)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   360
                        t = t+s.bottomPadding+s.topPadding
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   361
                    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
   362
                    j = j + 1
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   363
                H[i] = h
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2194
diff changeset
   364
            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
   365
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   366
        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
   367
        #print "height, H", height, H
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   368
        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
   369
        for h in H[:hmax]:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   370
            height = height - h
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   371
            self._rowpositions.append(height)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   372
        assert abs(height)<1e-8, 'Internal height error'
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   373
        self._hmax = hmax
1596
753865895a6f Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents: 1536
diff changeset
   374
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   375
    def _calc(self, availWidth, availHeight):
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   376
        #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
   377
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   378
        #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
   379
        #cells.  If so, apply a different algorithm
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   380
        #and assign some withs in a dumb way.
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   381
        #this CHANGES the widths array.
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   382
        if None in self._colWidths:
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   383
            if self._hasVariWidthElements():
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   384
                self._calcPreliminaryWidths(availWidth)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   385
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   386
        # 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
   387
        # 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
   388
        # in sizing
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   389
        if self._spanCmds:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   390
            self._calcSpanRanges()
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   391
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   392
        # calculate the full table height
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   393
        #print 'during calc, self._colWidths=', self._colWidths
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   394
        self._calc_height(availHeight)
1596
753865895a6f Jim Kraai's patch to preserve auto col width on subsequent pages
andy_robinson
parents: 1536
diff changeset
   395
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   396
        # if the width has already been calculated, don't calculate again
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   397
        # there's surely a better, more pythonic way to short circuit this FIXME FIXME
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   398
        if hasattr(self,'_width_calculated_once'): return
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   399
        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
   400
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   401
        # calculate the full table width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   402
        self._calc_width()
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
   403
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   404
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   405
        if self._spanCmds:
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   406
            #now work out the actual rect for each spanned cell
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   407
            #from the underlying grid
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   408
            self._calcSpanRects()
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   409
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   410
    def _hasVariWidthElements(self, upToRow=None):
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   411
        """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
   412
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   413
        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
   414
        images and graphics."""
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   415
        bad = 0
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   416
        if upToRow is None: upToRow = self._nrows
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   417
        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
   418
            for col in range(self._ncols):
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   419
                value = self._cellvalues[row][col]
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   420
                if not self._canGetWidth(value):
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   421
                    bad = 1
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   422
                    #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
   423
        return bad
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   424
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   425
    def _canGetWidth(self, thing):
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   426
        "Can we work out the width quickly?"
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   427
        if type(thing) in (ListType, TupleType):
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   428
            for elem in thing:
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   429
                if not self._canGetWidth(elem):
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   430
                    return 0
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   431
            return 1
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   432
        elif isinstance(thing, Flowable):
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   433
            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
   434
        else: #string, number, None etc.
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   435
            #anything else gets passed to str(...)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   436
            # so should be sizable
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   437
            return 1
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   438
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   439
    def _calcPreliminaryWidths(self, availWidth):
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   440
        """Fallback algorithm for when main one fails.
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   441
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   442
        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
   443
        paragraphs might be present, do a preliminary scan
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   444
        and assign some sensible values - just divide up
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   445
        all unsizeable columns by the remaining space."""
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   446
        verbose = 0
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   447
        totalDefined = 0.0
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   448
        numberUndefined = 0
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   449
        for w in self._colWidths:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   450
            if w is None:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   451
                numberUndefined = numberUndefined + 1
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   452
            else:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   453
                totalDefined = totalDefined + w
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   454
        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
   455
            self._ncols, numberUndefined, availWidth - totalDefined)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   456
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   457
        #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
   458
        given = []
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   459
        sizeable = []
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   460
        unsizeable = []
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   461
        for colNo in range(self._ncols):
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   462
            if self._colWidths[colNo] is None:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   463
                siz = 1
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   464
                for rowNo in range(self._nrows):
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   465
                    value = self._cellvalues[rowNo][colNo]
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1912
diff changeset
   466
                    if not self._canGetWidth(value):
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   467
                        siz = 0
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   468
                        break
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   469
                if siz:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   470
                    sizeable.append(colNo)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   471
                else:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   472
                    unsizeable.append(colNo)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   473
            else:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   474
                given.append(colNo)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   475
        if len(given) == self._ncols:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   476
            return
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   477
        if verbose: print 'predefined width:   ',given
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   478
        if verbose: print 'uncomputable width: ',unsizeable
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   479
        if verbose: print 'computable width:    ',sizeable
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   480
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   481
        #how much width is left:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   482
        # on the next iteration we could size the sizeable ones, for now I'll just
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   483
        # divide up the space
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   484
        newColWidths = list(self._colWidths)
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   485
        guessColWidth = (availWidth - totalDefined) / (len(unsizeable)+len(sizeable))
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   486
        assert guessColWidth >= 0, "table is too wide already, cannot choose a sane width for undefined columns"
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   487
        if verbose: print 'assigning width %0.2f to all undefined columns' % guessColWidth
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   488
        for colNo in sizeable:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   489
            newColWidths[colNo] = guessColWidth
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   490
        for colNo in unsizeable:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   491
            newColWidths[colNo] = guessColWidth
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   492
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   493
        self._colWidths = newColWidths
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   494
        self._argW = newColWidths
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   495
        if verbose: print 'new widths are:', self._colWidths
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   496
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   497
    def _calcSpanRanges(self):
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   498
        """Work out rects for tables which do row and column spanning.
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   499
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   500
        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
   501
        if a cell is part of a "spanned" range.
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   502
        self._spanRanges shows the 'coords' in integers of each
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   503
        'cell range', or None if it was clobbered:
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   504
          (col, row) -> (col0, row0, col1, row1)
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   505
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   506
        Any cell not in the key is not part of a spanned region
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   507
        """
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   508
        spanRanges = {}
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   509
        for row in range(self._nrows):
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   510
            for col in range(self._ncols):
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   511
                spanRanges[(col, row)] = (col, row, col, row)
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   512
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   513
        for (cmd, start, stop) in self._spanCmds:
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   514
            x0, y0 = start
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   515
            x1, y1 = stop
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   516
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   517
            #normalize
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   518
            if x0 < 0: x0 = x0 + self._ncols
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   519
            if x1 < 0: x1 = x1 + self._ncols
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   520
            if y0 < 0: y0 = y0 + self._nrows
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   521
            if y1 < 0: y1 = y1 + self._nrows
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   522
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   523
            if x0 > x1:
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   524
                x0, x1 = x1, x0
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   525
            if y0 > y1:
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   526
                y0, y1 = y1, y0
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   527
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   528
            # unset/clobber all the ones it
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   529
            # overwrites
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   530
            for y in range(y0, y1+1):
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   531
                for x in range(x0, x1+1):
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   532
                    spanRanges[x, y] = None
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   533
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   534
            # set the main entry
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   535
            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
   536
##            from pprint import pprint as pp
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   537
##            pp(spanRanges)
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   538
        self._spanRanges = spanRanges
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   539
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   540
        #now produce a "listing" of all cells which
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   541
        #are part of a spanned region, so the normal
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   542
        #sizing algorithm can not bother sizing such cells
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   543
        colSpannedCells = {}
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   544
        for (key, value) in spanRanges.items():
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   545
            if value is None:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   546
                colSpannedCells[key] = 1
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   547
            elif len(value) == 4:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   548
                if value[0] == value[2]:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   549
                    #not colspanned
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   550
                    pass
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   551
                else:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   552
                    colSpannedCells[key] = 1
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   553
        self._colSpannedCells = colSpannedCells
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   554
        #ditto for row-spanned ones.
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   555
        rowSpannedCells = {}
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   556
        for (key, value) in spanRanges.items():
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   557
            if value is None:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   558
                rowSpannedCells[key] = 1
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   559
            elif len(value) == 4:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   560
                if value[1] == value[3]:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   561
                    #not rowspanned
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   562
                    pass
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   563
                else:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   564
                    rowSpannedCells[key] = 1
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   565
        self._rowSpannedCells = rowSpannedCells
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   566
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   567
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   568
    def _calcSpanRects(self):
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   569
        """Work out rects for tables which do row and column spanning.
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   570
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   571
        Based on self._spanRanges, which is already known,
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   572
        and the widths which were given or previously calculated,
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   573
        self._spanRects shows the real coords for drawing:
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   574
          (col, row) -> (x, y, width, height)
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   575
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   576
        for each cell.  Any cell which 'does not exist' as another
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   577
        has spanned over it will get a None entry on the right
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   578
        """
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   579
        spanRects = {}
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   580
        for (coord, value) in self._spanRanges.items():
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   581
            if value is None:
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   582
                spanRects[coord] = None
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   583
            else:
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   584
                col,row = coord
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   585
                col0, row0, col1, row1 = value
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   586
                x = self._colpositions[col0]
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   587
                y = self._rowpositions[row1+1]  # should I add 1 for bottom left?
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   588
                width = self._colpositions[col1+1] - x
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   589
                height = self._rowpositions[row0] - y
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   590
                spanRects[coord] = (x, y, width, height)
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   591
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   592
        self._spanRects = spanRects
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   593
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   594
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   595
    def setStyle(self, tblstyle):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   596
        if type(tblstyle) is not TableStyleType:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   597
            tblstyle = TableStyle(tblstyle)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   598
        for cmd in tblstyle.getCommands():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   599
            self._addCommand(cmd)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   600
        for k,v in tblstyle._opts.items():
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   601
            setattr(self,k,v)
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   602
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   603
    def _addCommand(self,cmd):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   604
        if cmd[0] == 'BACKGROUND':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   605
            self._bkgrndcmds.append(cmd)
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   606
        elif cmd[0] == 'SPAN':
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   607
            self._spanCmds.append(cmd)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   608
        elif _isLineCommand(cmd):
1699
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   609
            # we expect op, start, stop, weight, colour, cap, dashes, join
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   610
            cmd = tuple(cmd)
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   611
            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
   612
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   613
            #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
   614
            if len(cmd)<6:
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   615
                cmd = cmd+(1,)  
1699
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   616
            else:
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   617
                cap = cmd[5]  
1699
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   618
                try:
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   619
                    if type(cap) is not type(int):
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   620
                        cap = LINECAPS[cap]
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   621
                    elif cap<0 or cap>2:
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   622
                        raise ValueError
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   623
                    cmd = cmd[:5]+(cap,)+cmd[6:]
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   624
                except:
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   625
                    ValueError('Bad cap value %s in %s'%(cap,str(cmd)))
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   626
            #dashes at index 6 - this is a dash array:
1699
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   627
            if len(cmd)<7: cmd = cmd+(None,)
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   628
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   629
            #join mode at index 7 - can be string or numeric, look up as for caps
1699
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   630
            if len(cmd)<8: cmd = cmd+(1,)
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   631
            else:
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   632
                join = cmd[7]
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   633
                try:
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   634
                    if type(join) is not type(int):
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   635
                        join = LINEJOINS[cap]
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   636
                    elif join<0 or join>2:
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   637
                        raise ValueError
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   638
                    cmd = cmd[:7]+(join,)
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   639
                except:
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   640
                    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
   641
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   642
            #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
   643
            if len(cmd)<9:
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   644
                lineCount = 1
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   645
                cmd = cmd + (lineCount,)
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   646
            else:
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   647
                lineCount = cmd[8]
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   648
            assert lineCount >= 1
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   649
            #linespacing at index 9. Not applicable unless 2+ lines, defaults to 2*line
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   650
            #width so you get a visible gap between centres
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   651
            if len(cmd)<10: cmd = cmd + (cmd[3],)
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   652
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   653
            assert len(cmd) == 10
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   654
            
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   655
            self._linecmds.append(cmd)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   656
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   657
            (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
   658
            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
   659
            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
   660
            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
   661
            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
   662
            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
   663
                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
   664
                    _setCellStyle(self._cellStyles, i, j, op, values)
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   665
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   666
    def _drawLines(self):
1699
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   667
        ccap, cdash, cjoin = None, None, None
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   668
        self.canv.saveState()
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   669
        for op, (sc,sr), (ec,er), weight, color, cap, dash, join, count, space in self._linecmds:
2185
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   670
            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
   671
            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
   672
            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
   673
            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
   674
            if er < 0: er = er + self._nrows
1699
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   675
            if ccap!=cap:
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   676
                self.canv.setLineCap(cap)
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   677
                ccap = cap
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   678
            getattr(self,_LineOpMap.get(op, '_drawUnknown' ))( (sc, sr), (ec, er), weight, color, count, space)
1699
37fa8717925f Improved line styles, allow for cap etc
rgbecker
parents: 1683
diff changeset
   679
        self.canv.restoreState()
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   680
        self._curcolor = None
248
c103b7a55e79 Color fixes; thanks to J Alet
rgbecker
parents: 221
diff changeset
   681
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   682
    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
   683
        raise ValueError, "Unknown line command '%s'" % op
403
05b3be301e19 Started debugging Table split
rgbecker
parents: 361
diff changeset
   684
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   685
    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
   686
        self._drawBox( (sc, sr), (ec, er), weight, color, count, space)
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   687
        self._drawInnerGrid( (sc, sr), (ec, er), weight, color, count, space)
403
05b3be301e19 Started debugging Table split
rgbecker
parents: 361
diff changeset
   688
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   689
    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
   690
        self._drawHLines((sc, sr), (ec, sr), weight, color, count, space)
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   691
        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
   692
        self._drawVLines((sc, sr), (sc, er), weight, color, count, space)
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   693
        self._drawVLines((ec+1, sr), (ec+1, er), weight, color, count, space)
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   694
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   695
    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
   696
        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
   697
        self._drawVLines((sc+1, sr), (ec, er), weight, color, count, space)
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   698
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   699
    def _prepLine(self, weight, color):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   700
        if color != self._curcolor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   701
            self.canv.setStrokeColor(color)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   702
            self._curcolor = color
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   703
        if weight != self._curweight:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   704
            self.canv.setLineWidth(weight)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   705
            self._curweight = weight
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   706
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   707
    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
   708
        ecp = self._colpositions[sc:ec+2]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   709
        rp = self._rowpositions[sr:er+1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   710
        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
   711
        self._prepLine(weight, color)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   712
        scp = ecp[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   713
        ecp = ecp[-1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   714
        for rowpos in rp:
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   715
            if count == 1:
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   716
                self.canv.line(scp, rowpos, ecp, rowpos)
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   717
            else: #double/triple lines
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   718
                #position so count=1 and no space gives origin
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   719
                offset = (count-1) * (weight + space)
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   720
                for idx in range(count):
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   721
                    y = rowpos + 0.5*offset - (idx * (weight+space))
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   722
                    self.canv.line(scp, y, ecp, y)
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   723
403
05b3be301e19 Started debugging Table split
rgbecker
parents: 361
diff changeset
   724
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   725
    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
   726
        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
   727
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   728
    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
   729
        erp = self._rowpositions[sr:er+2]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   730
        cp  = self._colpositions[sc:ec+1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   731
        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
   732
        self._prepLine(weight, color)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   733
        srp = erp[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   734
        erp = erp[-1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   735
        for colpos in cp:
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   736
            if count == 1:
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   737
                self.canv.line(colpos, srp, colpos, erp)
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   738
            else: #double/triple lines
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   739
                #position so count=1 and no space gives origin
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   740
                offset = (count-1) * (weight + space)
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   741
                for idx in range(count):
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   742
                    x = colpos + 0.5*offset - (idx * (weight+space))
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   743
                    self.canv.line(x, srp, x, erp)
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   744
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   745
    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
   746
        self._drawVLines((sc+1, sr), (ec+1, er), weight, color, count, space)
403
05b3be301e19 Started debugging Table split
rgbecker
parents: 361
diff changeset
   747
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   748
    def wrap(self, availWidth, availHeight):
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   749
        self._calc(availWidth, availHeight)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   750
        #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
   751
        self.availWidth = availWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   752
        return (self._width, self._height)
1495
fd32c1794998 Propagate font properties to Flowable cells
rgbecker
parents: 1492
diff changeset
   753
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   754
    def onSplit(self,T,byRow=1):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   755
        '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   756
        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
   757
        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
   758
        '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   759
        pass
419
469fe11a010b Additions/Improvements to LINE CMD Splitting
rgbecker
parents: 403
diff changeset
   760
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   761
    def _cr_0(self,n,cmds):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   762
        for c in cmds:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   763
            c = tuple(c)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   764
            (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
   765
            if sr>=n: continue
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   766
            if er>=n: er = n-1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   767
            self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:])
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   768
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   769
    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
   770
        for c in cmds:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   771
            c = tuple(c)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   772
            (sc,sr), (ec,er) = c[1:3]
2185
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   773
            if sr in ('splitfirst','splitlast'): self._addCommand(c)
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   774
            else:
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   775
                if sr>=0 and sr>=repeatRows and sr<n and er>=0 and er<n: continue
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   776
                if sr>=repeatRows and sr<n: sr=repeatRows
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   777
                elif sr>=repeatRows and sr>=n: sr=sr+repeatRows-n
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   778
                if er>=repeatRows and er<n: er=repeatRows
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   779
                elif er>=repeatRows and er>=n: er=er+repeatRows-n
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   780
                self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:])
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   781
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   782
    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
   783
        for c in cmds:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   784
            c = tuple(c)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   785
            (sc,sr), (ec,er) = c[1:3]
2185
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   786
            if sr in ('splitfirst','splitlast'): self._addCommand(c)
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   787
            else:
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   788
                if er>=0 and er<n: continue
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   789
                if sr>=0 and sr<n: sr=0
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   790
                if sr>=n: sr = sr-n
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   791
                if er>=n: er = er-n
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   792
                self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:])
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   793
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   794
    def _splitRows(self,availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   795
        h = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   796
        n = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   797
        lim = len(self._rowHeights)
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   798
        while n<self._hmax:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   799
            hn = h + self._rowHeights[n]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   800
            if hn>availHeight: break
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   801
            h = hn
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   802
            n = n + 1
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   803
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   804
        if n<=self.repeatRows:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   805
            return []
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   806
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   807
        if n==lim: return [self]
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   808
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   809
        repeatRows = self.repeatRows
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   810
        repeatCols = self.repeatCols
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   811
        splitByRow = self.splitByRow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   812
        data = self._cellvalues
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   813
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   814
        #we're going to split into two superRows
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   815
        #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
   816
        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
   817
                repeatRows=repeatRows, repeatCols=repeatCols,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   818
                splitByRow=splitByRow)
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   819
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   820
        #copy the styles and commands
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   821
        R0._cellStyles = self._cellStyles[:n]
419
469fe11a010b Additions/Improvements to LINE CMD Splitting
rgbecker
parents: 403
diff changeset
   822
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   823
        A = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   824
        # hack up the line commands
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   825
        for op, (sc,sr), (ec,er), weight, color, cap, dash, join, count, space in self._linecmds:
2185
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   826
            if type(sr)is type('') and sr.startswith('split'):
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   827
                A.append((op,(sc,sr), (ec,sr), weight, color, cap, dash, join, count, space))
2185
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   828
                if sr=='splitlast':
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   829
                    sr = er = n-1
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   830
                elif sr=='splitfirst':
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   831
                    sr = n
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   832
                    er = n
a45d3b2ebe37 Added splitfirst/last handling
rgbecker
parents: 2012
diff changeset
   833
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   834
            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
   835
            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
   836
            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
   837
            if er < 0: er = er + self._nrows
419
469fe11a010b Additions/Improvements to LINE CMD Splitting
rgbecker
parents: 403
diff changeset
   838
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   839
            if op in ('BOX','OUTLINE','GRID'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   840
                if sr<n and er>=n:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   841
                    # we have to split the BOX
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   842
                    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
   843
                    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
   844
                    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
   845
                    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
   846
                    if op=='GRID':
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   847
                        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
   848
                        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
   849
                        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
   850
                else:
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   851
                    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
   852
            elif op in ('INNERGRID','LINEABOVE'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   853
                if sr<n and er>=n:
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   854
                    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
   855
                    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
   856
                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
   857
            elif op == 'LINEBELOW':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   858
                if sr<n and er>=(n-1):
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   859
                    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
   860
                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
   861
            elif op == 'LINEABOVE':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   862
                if sr<=n and er>=n:
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   863
                    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
   864
                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
   865
            else:
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   866
                A.append((op,(sc,sr), (ec,er), weight, color, cap, dash, join, count, space))
419
469fe11a010b Additions/Improvements to LINE CMD Splitting
rgbecker
parents: 403
diff changeset
   867
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   868
        R0._cr_0(n,A)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   869
        R0._cr_0(n,self._bkgrndcmds)
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   870
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   871
        if repeatRows:
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   872
            #R1 = slelf.__class__(data[:repeatRows]+data[n:],self._argW,
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   873
            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
   874
                    self._argH[:repeatRows]+self._argH[n:],
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   875
                    repeatRows=repeatRows, repeatCols=repeatCols,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   876
                    splitByRow=splitByRow)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   877
            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
   878
            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
   879
            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
   880
        else:
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
   881
            #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
   882
            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
   883
                    repeatRows=repeatRows, repeatCols=repeatCols,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   884
                    splitByRow=splitByRow)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   885
            R1._cellStyles = self._cellStyles[n:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   886
            R1._cr_1_0(n,A)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   887
            R1._cr_1_0(n,self._bkgrndcmds)
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   888
1498
2e7cfa1159cb Copy alignments accross split (thanks R�diger M�hl <ruediger.maehl@web.de>
rgbecker
parents: 1495
diff changeset
   889
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   890
        R0.hAlign = R1.hAlign = self.hAlign
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   891
        R0.vAlign = R1.vAlign = self.vAlign
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   892
        self.onSplit(R0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   893
        self.onSplit(R1)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   894
        return [R0,R1]
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
   895
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   896
    def split(self, availWidth, availHeight):
1912
c8509682e3e0 Finished off sizing logic to go with row and column
andy_robinson
parents: 1883
diff changeset
   897
        self._calc(availWidth, availHeight)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   898
        if self.splitByRow:
2012
d962f1d86a1b Allow for special case width overflows
rgbecker
parents: 1989
diff changeset
   899
            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
   900
            return self._splitRows(availHeight)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   901
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   902
            raise NotImplementedError
403
05b3be301e19 Started debugging Table split
rgbecker
parents: 361
diff changeset
   903
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   904
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   905
        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
   906
        self._drawBkgrnd()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   907
        self._drawLines()
1883
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   908
        if self._spanCmds == []:
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   909
            # old fashioned case, no spanning, steam on and do each cell
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   910
            for row, rowstyle, rowpos, rowheight in map(None, self._cellvalues, self._cellStyles, self._rowpositions[1:], self._rowHeights):
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   911
                for cellval, cellstyle, colpos, colwidth in map(None, row, rowstyle, self._colpositions[:-1], self._colWidths):
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   912
                    self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight))
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   913
        else:
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   914
            # we have some row or col spans, need a more complex algorithm
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   915
            # to find the rect for each
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   916
            for rowNo in range(self._nrows):
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   917
                for colNo in range(self._ncols):
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   918
                    cellRect = self._spanRects[colNo, rowNo]
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   919
                    if cellRect is not None:
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   920
                        (x, y, width, height) = cellRect
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   921
                        cellval = self._cellvalues[rowNo][colNo]
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   922
                        cellstyle = self._cellStyles[rowNo][colNo]
eb6f902a0613 Initial try at column spanning
andy_robinson
parents: 1699
diff changeset
   923
                        self._drawCell(cellval, cellstyle, (x, y), (width, height))
2190
e925cd300e9e merging Jython-branch
dragan1
parents: 2189
diff changeset
   924
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   925
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   926
    def _drawBkgrnd(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   927
        nrows = self._nrows
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   928
        ncols = self._ncols
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   929
        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
   930
            if sc < 0: sc = sc + ncols
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   931
            if ec < 0: ec = ec + ncols
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   932
            if sr < 0: sr = sr + nrows
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   933
            if er < 0: er = er + nrows
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   934
            x0 = self._colpositions[sc]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   935
            y0 = self._rowpositions[sr]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   936
            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
   937
            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
   938
            w, h = x1-x0, y1-y0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   939
            canv = self.canv
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   940
            if callable(arg):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   941
                apply(arg,(self,canv, x0, y0, w, h))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   942
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   943
                canv.setFillColor(colors.toColor(arg))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   944
                canv.rect(x0, y0, w, h, stroke=0,fill=1)
326
159576a5816e First try at auto sizing
rgbecker
parents: 312
diff changeset
   945
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   946
    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
   947
        if self._curcellstyle is not cellstyle:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   948
            cur = self._curcellstyle
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   949
            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
   950
                self.canv.setFillColor(cellstyle.color)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   951
            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
   952
                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
   953
            self._curcellstyle = cellstyle
419
469fe11a010b Additions/Improvements to LINE CMD Splitting
rgbecker
parents: 403
diff changeset
   954
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   955
        just = cellstyle.alignment
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   956
        valign = cellstyle.valign
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   957
        n = type(cellval)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   958
        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
   959
            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
   960
            # 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
   961
            W = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   962
            H = []
2189
47cb423f5f1a Make _listCellGeom more specific
rgbecker
parents: 2185
diff changeset
   963
            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
   964
            if valign=='TOP':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   965
                y = rowpos + rowheight - cellstyle.topPadding
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   966
            elif valign=='BOTTOM':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   967
                y = rowpos+cellstyle.bottomPadding + h
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   968
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   969
                y = rowpos+(rowheight+cellstyle.bottomPadding-cellstyle.topPadding+h)/2.0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   970
            y = y+cellval[0].getSpaceBefore()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   971
            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
   972
                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
   973
                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
   974
                elif just in ('CENTRE', 'CENTER'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   975
                    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
   976
                else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   977
                    raise ValueError, 'Invalid justification %s' % just
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   978
                y = y - v.getSpaceBefore()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   979
                y = y - h
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   980
                v.drawOn(self.canv,x,y)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   981
                y = y - v.getSpaceAfter()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   982
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   983
            if just == 'LEFT':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   984
                draw = self.canv.drawString
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   985
                x = colpos + cellstyle.leftPadding
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   986
            elif just in ('CENTRE', 'CENTER'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   987
                draw = self.canv.drawCentredString
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   988
                x = colpos + colwidth * 0.5
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   989
            elif just == 'RIGHT':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   990
                draw = self.canv.drawRightString
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   991
                x = colpos + colwidth - cellstyle.rightPadding
2219
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   992
            elif just == 'DECIMAL':
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   993
                draw = self.canv.drawAlignedString
5c1727997169 Multiple lines and decimal alignments now supported
andy_robinson
parents: 2200
diff changeset
   994
                x = colpos + colwidth - cellstyle.rightPadding
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   995
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   996
                raise ValueError, 'Invalid justification %s' % just
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   997
            if n is StringType: val = cellval
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   998
            else: val = str(cellval)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
   999
            vals = string.split(val, "\n")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1000
            n = len(vals)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1001
            leading = cellstyle.leading
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1002
            fontsize = cellstyle.fontsize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1003
            if valign=='BOTTOM':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1004
                y = rowpos + cellstyle.bottomPadding+n*leading-fontsize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1005
            elif valign=='TOP':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1006
                y = rowpos + rowheight - cellstyle.topPadding - fontsize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1007
            elif valign=='MIDDLE':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1008
                y = rowpos + (cellstyle.bottomPadding + rowheight-cellstyle.topPadding+(n-1)*leading)/2.0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1009
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1010
                raise ValueError, "Bad valign: '%s'" % str(valign)
329
7358814b9b59 First attempt at VALIGN
rgbecker
parents: 327
diff changeset
  1011
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1012
            for v in vals:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1013
                draw(x, y, v)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1014
                y = y-leading
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
  1015
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1016
# for text,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1017
#   drawCentredString(self, x, y, text) where x is center
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1018
#   drawRightString(self, x, y, text) where x is right
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1019
#   drawString(self, x, y, text) where x is left
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1020
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1021
_LineOpMap = {  'GRID':'_drawGrid',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1022
                'BOX':'_drawBox',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1023
                'OUTLINE':'_drawBox',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1024
                'INNERGRID':'_drawInnerGrid',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1025
                'LINEBELOW':'_drawHLinesB',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1026
                'LINEABOVE':'_drawHLines',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1027
                'LINEBEFORE':'_drawVLines',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1028
                'LINEAFTER':'_drawVLinesA', }
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1029
2194
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
  1030
class LongTable(Table):
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
  1031
    '''Henning von Bargen's changes will be active'''
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
  1032
    _longTableOptimize = 1
6bc71309eb18 Modified version of Henning von Bargen's LongTables optimisation
rgbecker
parents: 2190
diff changeset
  1033
403
05b3be301e19 Started debugging Table split
rgbecker
parents: 361
diff changeset
  1034
LINECOMMANDS = _LineOpMap.keys()
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1035
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1036
def _isLineCommand(cmd):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1037
    return cmd[0] in LINECOMMANDS
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1038
350
0916ae478b25 Table splitting start
rgbecker
parents: 342
diff changeset
  1039
def _setCellStyle(cellStyles, i, j, op, values):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1040
    #new = CellStyle('<%d, %d>' % (i,j), cellStyles[i][j])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1041
    #cellStyles[i][j] = new
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1042
    ## modify in place!!!
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1043
    new = cellStyles[i][j]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1044
    if op == 'FONT':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1045
        n = len(values)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1046
        new.fontname = values[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1047
        if n>1:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1048
            new.fontsize = values[1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1049
            if n>2:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1050
                new.leading = values[2]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1051
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1052
                new.leading = new.fontsize*1.2
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1053
    elif op in ('FONTNAME', 'FACE'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1054
        new.fontname = values[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1055
    elif op in ('SIZE', 'FONTSIZE'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1056
        new.fontsize = values[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1057
    elif op == 'LEADING':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1058
        new.leading = values[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1059
    elif op == 'TEXTCOLOR':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1060
        new.color = colors.toColor(values[0], colors.Color(0,0,0))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1061
    elif op in ('ALIGN', 'ALIGNMENT'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1062
        new.alignment = values[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1063
    elif op == 'VALIGN':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1064
        new.valign = values[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1065
    elif op == 'LEFTPADDING':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1066
        new.leftPadding = values[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1067
    elif op == 'RIGHTPADDING':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1068
        new.rightPadding = values[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1069
    elif op == 'TOPPADDING':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1070
        new.topPadding = values[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1071
    elif op == 'BOTTOMPADDING':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1072
        new.bottomPadding = values[0]
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1073
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1074
GRID_STYLE = TableStyle(
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1075
    [('GRID', (0,0), (-1,-1), 0.25, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1076
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1077
    )
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1078
BOX_STYLE = TableStyle(
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1079
    [('BOX', (0,0), (-1,-1), 0.50, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1080
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1081
    )
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1082
LABELED_GRID_STYLE = TableStyle(
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1083
    [('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1084
     ('BOX', (0,0), (-1,-1), 2, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1085
     ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1086
     ('LINEAFTER', (0,0), (0,-1), 2, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1087
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1088
    )
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1089
COLORED_GRID_STYLE = TableStyle(
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1090
    [('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1091
     ('BOX', (0,0), (-1,-1), 2, colors.red),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1092
     ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1093
     ('LINEAFTER', (0,0), (0,-1), 2, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1094
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1095
    )
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1096
LIST_STYLE = TableStyle(
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1097
    [('LINEABOVE', (0,0), (-1,0), 2, colors.green),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1098
     ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1099
     ('LINEBELOW', (0,-1), (-1,-1), 2, colors.green),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1100
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1101
    )
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1102
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1103
def test():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1104
    from reportlab.lib.units import inch
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1105
    rowheights = (24, 16, 16, 16, 16)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1106
    rowheights2 = (24, 16, 16, 16, 30)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1107
    colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1108
    data = (
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1109
        ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1110
        ('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1111
        ('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1112
        ('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1113
        ('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1114
        )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1115
    data2 = (
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1116
        ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1117
        ('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1118
        ('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1119
        ('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1120
        ('Hats\nLarge', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1121
        )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1122
    styleSheet = getSampleStyleSheet()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1123
    lst = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1124
    lst.append(Paragraph("Tables", styleSheet['Heading1']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1125
    lst.append(Paragraph(__doc__, styleSheet['BodyText']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1126
    lst.append(Paragraph("The Tables (shown in different styles below) were created using the following code:", styleSheet['BodyText']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1127
    lst.append(Preformatted("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1128
    colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1129
    rowheights = (24, 16, 16, 16, 16)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1130
    data = (
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1131
        ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun',
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1132
           'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1133
        ('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1134
        ('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1135
        ('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1136
        ('Hats', 893, 912, '1,212', 643, 789, 159,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1137
             888, '1,298', 832, 453, '1,344','2,843')
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
  1138
        )
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1139
    t = Table(data, colwidths, rowheights)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1140
    """, styleSheet['Code'], dedent=4))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1141
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1142
    You can then give the Table a TableStyle object to control its format. The first TableStyle used was
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1143
    created as follows:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1144
    """, styleSheet['BodyText']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1145
    lst.append(Preformatted("""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
  1146
GRID_STYLE = TableStyle(
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1147
    [('GRID', (0,0), (-1,-1), 0.25, colors.black),
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1148
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1149
    )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1150
    """, styleSheet['Code']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1151
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1152
    TableStyles are created by passing in a list of commands. There are two types of commands - line commands
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1153
    and cell formatting commands. In all cases, the first three elements of a command are the command name,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1154
    the starting cell and the ending cell.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1155
    """, styleSheet['BodyText']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1156
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1157
    Line commands always follow this with the weight and color of the desired lines. Colors can be names,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1158
    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
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1159
    command names are: GRID, BOX, OUTLINE, INNERGRID, LINEBELOW, LINEABOVE, LINEBEFORE
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1160
    and LINEAFTER. BOX and OUTLINE are equivalent, and GRID is the equivalent of applying both BOX and
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1161
    INNERGRID.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1162
    """, styleSheet['BodyText']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1163
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1164
    Cell formatting commands are:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1165
    """, styleSheet['BodyText']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1166
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1167
    FONT - takes fontname, fontsize and (optional) leading.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1168
    """, styleSheet['Definition']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1169
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1170
    TEXTCOLOR - takes a color name or (R,G,B) tuple.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1171
    """, styleSheet['Definition']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1172
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1173
    ALIGNMENT (or ALIGN) - takes one of LEFT, RIGHT and CENTRE (or CENTER).
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1174
    """, styleSheet['Definition']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1175
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1176
    LEFTPADDING - defaults to 6.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1177
    """, styleSheet['Definition']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1178
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1179
    RIGHTPADDING - defaults to 6.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1180
    """, styleSheet['Definition']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1181
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1182
    BOTTOMPADDING - defaults to 3.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1183
    """, styleSheet['Definition']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1184
    lst.append(Paragraph("""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1185
    A tablestyle is applied to a table by calling Table.setStyle(tablestyle).
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1186
    """, styleSheet['BodyText']))
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1658
diff changeset
  1187
    t = Table(data, colwidths, rowheights)