reportlab/platypus/flowables.py
author rgbecker
Mon, 20 Sep 2004 16:31:22 +0000
changeset 2387 75caa41a818f
parent 2384 6d44252e1042
child 2388 5fa400f5d46b
permissions -rw-r--r--
platypus/test: working pto (& inner pto)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2321
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2004
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 445
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2321
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/platypus/flowables.py
2321
3454f5b41760 Unicode and UTF8 support changes
andy
parents: 2284
diff changeset
     4
__version__=''' $Id$ '''
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     5
__doc__="""
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
     6
A flowable is a "floating element" in a document whose exact position is determined by the
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
     7
other elements that precede it, such as a paragraph, a diagram interspersed between paragraphs,
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
     8
a section header, etcetera.  Examples of non-flowables include page numbering annotations,
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
     9
headers, footers, fixed diagrams or logos, among others.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    10
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    11
Flowables are defined here as objects which know how to determine their size and which
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    12
can draw themselves onto a page with respect to a relative "origin" position determined
426
36a228f3e085 Changing to packer led positioning
rgbecker
parents: 368
diff changeset
    13
at a higher level. The object's draw() method should assume that (0,0) corresponds to the
36a228f3e085 Changing to packer led positioning
rgbecker
parents: 368
diff changeset
    14
bottom left corner of the enclosing rectangle that will contain the object. The attributes
36a228f3e085 Changing to packer led positioning
rgbecker
parents: 368
diff changeset
    15
vAlign and hAlign may be used by 'packers' as hints as to how the object should be placed.
36a228f3e085 Changing to packer led positioning
rgbecker
parents: 368
diff changeset
    16
36a228f3e085 Changing to packer led positioning
rgbecker
parents: 368
diff changeset
    17
Some Flowables also know how to "split themselves".  For example a
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    18
long paragraph might split itself between one page and the next.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    19
1490
d6d958390ff0 Added api instructions
rgbecker
parents: 1486
diff changeset
    20
Packers should set the canv attribute during wrap, split & draw operations to allow
d6d958390ff0 Added api instructions
rgbecker
parents: 1486
diff changeset
    21
the flowable to work out sizes etc in the proper context.
d6d958390ff0 Added api instructions
rgbecker
parents: 1486
diff changeset
    22
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    23
The "text" of a document usually consists mainly of a sequence of flowables which
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    24
flow into a document from top to bottom (with column and page breaks controlled by
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    25
higher level components).
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    26
"""
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    27
import os
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    28
import string
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    29
from copy import deepcopy
557
0ed50942f243 Allow for non string file names in Image.__init__
rgbecker
parents: 541
diff changeset
    30
from types import ListType, TupleType, StringType
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    31
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    32
from reportlab.pdfgen import canvas
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    33
from reportlab.lib.units import inch
2276
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
    34
from reportlab.lib.colors import red, gray, lightgrey
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    35
from reportlab.pdfbase import pdfutils
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    36
2384
6d44252e1042 platypus: added ptocontainer, moved tables test
rgbecker
parents: 2376
diff changeset
    37
from reportlab.rl_config import defaultPageSize, _FUZZ
684
2a43c747527a Changed things to use config.py for defaults
rgbecker
parents: 558
diff changeset
    38
PAGE_HEIGHT = defaultPageSize[1]
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    39
2113
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    40
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    41
class TraceInfo:
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    42
    "Holder for info about where an object originated"
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    43
    def __init__(self):
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    44
        self.srcFile = '(unknown)'
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    45
        self.startLineNo = -1
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    46
        self.startLinePos = -1
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    47
        self.endLineNo = -1
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    48
        self.endLinePos = -1
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2192
diff changeset
    49
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    50
#############################################################
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    51
#   Flowable Objects - a base class and a few examples.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    52
#   One is just a box to get some metrics.  We also have
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    53
#   a paragraph, an image and a special 'page break'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    54
#   object which fills the space.
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    55
#############################################################
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    56
class Flowable:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    57
    """Abstract base class for things to be drawn.  Key concepts:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    58
    1. It knows its size
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    59
    2. It draws in its own coordinate system (this requires the
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    60
        base API to provide a translate() function.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    61
    """
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1882
diff changeset
    62
    _fixedWidth = 0         #assume wrap results depend on arguments?
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1882
diff changeset
    63
    _fixedHeight = 0
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1882
diff changeset
    64
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    65
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    66
        self.width = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    67
        self.height = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    68
        self.wrapped = 0
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    69
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    70
        #these are hints to packers/frames as to how the floable should be positioned
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    71
        self.hAlign = 'LEFT'    #CENTER/CENTRE or RIGHT
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    72
        self.vAlign = 'BOTTOM'  #MIDDLE or TOP
426
36a228f3e085 Changing to packer led positioning
rgbecker
parents: 368
diff changeset
    73
2113
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    74
        #optional holder for trace info
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
    75
        self._traceInfo = None
2158
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2113
diff changeset
    76
        self._showBoundary = None
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2192
diff changeset
    77
1494
b0707e45102e Added _drawOn method
rgbecker
parents: 1491
diff changeset
    78
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    79
    def _drawOn(self,canv):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    80
        '''ensure canv is set on and then draw'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    81
        self.canv = canv
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    82
        self.draw()#this is the bit you overload
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    83
        del self.canv
1494
b0707e45102e Added _drawOn method
rgbecker
parents: 1491
diff changeset
    84
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    85
    def drawOn(self, canvas, x, y, _sW=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    86
        "Tell it to draw itself on the canvas.  Do not override"
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    87
        if _sW and hasattr(self,'hAlign'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    88
            a = self.hAlign
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    89
            if a in ['CENTER','CENTRE']:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    90
                x = x + 0.5*_sW
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    91
            elif a == 'RIGHT':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    92
                x = x + _sW
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    93
            elif a != 'LEFT':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    94
                raise ValueError, "Bad hAlign value "+str(a)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    95
        canvas.saveState()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    96
        canvas.translate(x, y)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
    97
        self._drawOn(canvas)
2158
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2113
diff changeset
    98
        if hasattr(self, '_showBoundary') and self._showBoundary:
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2113
diff changeset
    99
            #diagnostic tool support
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2113
diff changeset
   100
            canvas.setStrokeColor(gray)
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2113
diff changeset
   101
            canvas.rect(0,0,self.width, self.height)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   102
        canvas.restoreState()
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   103
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   104
    def wrapOn(self, canv, aW, aH):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   105
        '''intended for use by packers allows setting the canvas on
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   106
        during the actual wrap'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   107
        self.canv = canv
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   108
        w, h = self.wrap(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   109
        del self.canv
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   110
        return w, h
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   111
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   112
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   113
        """This will be called by the enclosing frame before objects
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   114
        are asked their size, drawn or whatever.  It returns the
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   115
        size actually used."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   116
        return (self.width, self.height)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   117
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   118
    def minWidth(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   119
        """This should return the minimum required width"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   120
        return getattr(self,'_minWidth',self.width)
954
bf9f434cb08a added minWidth method to Flowable, Paragraph
rgbecker
parents: 760
diff changeset
   121
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   122
    def splitOn(self, canv, aW, aH):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   123
        '''intended for use by packers allows setting the canvas on
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   124
        during the actual split'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   125
        self.canv = canv
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   126
        S = self.split(aW,aH)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   127
        del self.canv
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   128
        return S
1491
8cb77f6ac40d Introduce wrapOn & splitOn
rgbecker
parents: 1490
diff changeset
   129
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   130
    def split(self, availWidth, availheight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   131
        """This will be called by more sophisticated frames when
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   132
        wrap fails. Stupid flowables should return []. Clever flowables
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   133
        should split themselves and return a list of flowables"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   134
        return []
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   135
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   136
    def getKeepWithNext(self):
1879
c1dc90e2f470 Fix trivial spelling burble
rgbecker
parents: 1768
diff changeset
   137
        """returns boolean determining whether the next flowable should stay with this one"""
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   138
        if hasattr(self,'keepWithNext'): return self.keepWithNext
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   139
        elif hasattr(self,'style') and hasattr(self.style,'keepWithNext'): return self.style.keepWithNext
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   140
        else: return 0
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   141
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   142
    def getSpaceAfter(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   143
        """returns how much space should follow this item if another item follows on the same page."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   144
        if hasattr(self,'spaceAfter'): return self.spaceAfter
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   145
        elif hasattr(self,'style') and hasattr(self.style,'spaceAfter'): return self.style.spaceAfter
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   146
        else: return 0
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   147
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   148
    def getSpaceBefore(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   149
        """returns how much space should precede this item if another item precedess on the same page."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   150
        if hasattr(self,'spaceBefore'): return self.spaceBefore
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   151
        elif hasattr(self,'style') and hasattr(self.style,'spaceBefore'): return self.style.spaceBefore
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   152
        else: return 0
1426
377351bbe539 Add getKeepWithNext to Flowable
rgbecker
parents: 1360
diff changeset
   153
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   154
    def isIndexing(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   155
        """Hook for IndexingFlowables - things which have cross references"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   156
        return 0
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 496
diff changeset
   157
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   158
    def identity(self, maxLen=None):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   159
        '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   160
        This method should attempt to return a string that can be used to identify
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   161
        a particular flowable uniquely. The result can then be used for debugging
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   162
        and or error printouts
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   163
        '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   164
        if hasattr(self, 'getPlainText'):
2172
2740f571bd24 Fixes to cope with rml2pdf weirds
rgbecker
parents: 2158
diff changeset
   165
            r = self.getPlainText(identify=1)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   166
        elif hasattr(self, 'text'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   167
            r = self.text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   168
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   169
            r = '...'
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   170
        if r and maxLen:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   171
            r = r[:maxLen]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   172
        return "<%s at %d>%s" % (self.__class__.__name__, id(self), r)
1103
857af510186d Added identity method to Flowables
rgbecker
parents: 1036
diff changeset
   173
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   174
class XBox(Flowable):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   175
    """Example flowable - a box with an x through it and a caption.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   176
    This has a known size, so does not need to respond to wrap()."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   177
    def __init__(self, width, height, text = 'A Box'):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   178
        Flowable.__init__(self)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   179
        self.width = width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   180
        self.height = height
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   181
        self.text = text
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   182
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   183
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   184
        return "XBox(w=%s, h=%s, t=%s)" % (self.width, self.height, self.text)
541
33de80b3655c added __repr__s and enhanced exception messages for debugging
aaron_watters
parents: 512
diff changeset
   185
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   186
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   187
        self.canv.rect(0, 0, self.width, self.height)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   188
        self.canv.line(0, 0, self.width, self.height)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   189
        self.canv.line(0, self.height, self.width, 0)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   190
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   191
        #centre the text
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   192
        self.canv.setFont('Times-Roman',12)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   193
        self.canv.drawCentredString(0.5*self.width, 0.5*self.height, self.text)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   194
445
8b64b9812ca6 Fixes to _dedenter
rgbecker
parents: 442
diff changeset
   195
def _trimEmptyLines(lines):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   196
    #don't want the first or last to be empty
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   197
    while len(lines) and string.strip(lines[0]) == '':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   198
        lines = lines[1:]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   199
    while len(lines) and string.strip(lines[-1]) == '':
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   200
        lines = lines[:-1]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   201
    return lines
445
8b64b9812ca6 Fixes to _dedenter
rgbecker
parents: 442
diff changeset
   202
442
e3eac15cddbd XPreformatted first fixes; now runs
rgbecker
parents: 426
diff changeset
   203
def _dedenter(text,dedent=0):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   204
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   205
    tidy up text - carefully, it is probably code.  If people want to
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   206
    indent code within a source script, you can supply an arg to dedent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   207
    and it will chop off that many character, otherwise it leaves
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   208
    left edge intact.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   209
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   210
    lines = string.split(text, '\n')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   211
    if dedent>0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   212
        templines = _trimEmptyLines(lines)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   213
        lines = []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   214
        for line in templines:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   215
            line = string.rstrip(line[dedent:])
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   216
            lines.append(line)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   217
    else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   218
        lines = _trimEmptyLines(lines)
445
8b64b9812ca6 Fixes to _dedenter
rgbecker
parents: 442
diff changeset
   219
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   220
    return lines
442
e3eac15cddbd XPreformatted first fixes; now runs
rgbecker
parents: 426
diff changeset
   221
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   222
class Preformatted(Flowable):
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   223
    """This is like the HTML <PRE> tag.
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   224
    It attempts to display text exactly as you typed it in a fixed width "typewriter" font.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   225
    The line breaks are exactly where you put
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   226
    them, and it will not be wrapped."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   227
    def __init__(self, text, style, bulletText = None, dedent=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   228
        """text is the text to display. If dedent is set then common leading space
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   229
        will be chopped off the front (for example if the entire text is indented
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   230
        6 spaces or more then each line will have 6 spaces removed from the front).
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   231
        """
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   232
        self.style = style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   233
        self.bulletText = bulletText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   234
        self.lines = _dedenter(text,dedent)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   235
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   236
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   237
        bT = self.bulletText
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   238
        H = "Preformatted("
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   239
        if bT is not None:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   240
            H = "Preformatted(bulletText=%s," % repr(bT)
2023
2d5def514733 Fix typo found by Christoph Devenoges
rgbecker
parents: 1994
diff changeset
   241
        return "%s'''\\ \n%s''')" % (H, string.join(self.lines,'\n'))
541
33de80b3655c added __repr__s and enhanced exception messages for debugging
aaron_watters
parents: 512
diff changeset
   242
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   243
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   244
        self.width = availWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   245
        self.height = self.style.leading*len(self.lines)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   246
        return (self.width, self.height)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   247
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   248
    def split(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   249
        #returns two Preformatted objects
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   250
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   251
        #not sure why they can be called with a negative height
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   252
        if availHeight < self.style.leading:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   253
            return []
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   254
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   255
        linesThatFit = int(availHeight * 1.0 / self.style.leading)
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   256
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   257
        text1 = string.join(self.lines[0:linesThatFit], '\n')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   258
        text2 = string.join(self.lines[linesThatFit:], '\n')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   259
        style = self.style
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   260
        if style.firstLineIndent != 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   261
            style = deepcopy(style)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   262
            style.firstLineIndent = 0
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   263
        return [Preformatted(text1, self.style), Preformatted(text2, style)]
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   264
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   265
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   266
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   267
        #call another method for historical reasons.  Besides, I
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   268
        #suspect I will be playing with alternate drawing routines
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   269
        #so not doing it here makes it easier to switch.
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   270
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   271
        cur_x = self.style.leftIndent
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   272
        cur_y = self.height - self.style.fontSize
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   273
        self.canv.addLiteral('%PreformattedPara')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   274
        if self.style.textColor:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   275
            self.canv.setFillColor(self.style.textColor)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   276
        tx = self.canv.beginText(cur_x, cur_y)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   277
        #set up the font etc.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   278
        tx.setFont( self.style.fontName,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   279
                    self.style.fontSize,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   280
                    self.style.leading)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   281
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   282
        for text in self.lines:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   283
            tx.textLine(text)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   284
        self.canv.drawText(tx)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   285
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   286
class Image(Flowable):
2045
cc042609c62e hack to ImageReader
rgbecker
parents: 2023
diff changeset
   287
    """an image (digital picture).  Formats supported by PIL/Java 1.4 (the Python/Java Imaging Library
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   288
       are supported.  At the present time images as flowables are always centered horozontally
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   289
       in the frame. We allow for two kinds of lazyness to allow for many images in a document
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   290
       which could lead to file handle starvation.
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   291
       lazy=1 don't open image until required.
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   292
       lazy=2 open image when required then shut it.
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   293
    """
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1882
diff changeset
   294
    _fixedWidth = 1
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1882
diff changeset
   295
    _fixedHeight = 1
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   296
    def __init__(self, filename, width=None, height=None, kind='direct', mask="auto", lazy=1):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   297
        """If size to draw at not specified, get it from the image."""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   298
        self.hAlign = 'CENTER'
1880
5444027dd195 Allow for image transparancy mask
rgbecker
parents: 1879
diff changeset
   299
        self._mask = mask
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   300
        # if it is a JPEG, will be inlined within the file -
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   301
        # but we still need to know its size now
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   302
        fp = hasattr(filename,'read')
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   303
        if fp:
2237
005d070deb87 Fix up more Image cases
rgbecker
parents: 2200
diff changeset
   304
            self._file = filename
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   305
            self.filename = `filename`
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   306
        else:
2237
005d070deb87 Fix up more Image cases
rgbecker
parents: 2200
diff changeset
   307
            self._file = self.filename = filename
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   308
        if not fp and os.path.splitext(filename)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']:
2241
5bf5d0f03603 Make Image to use open_for_read
rgbecker
parents: 2237
diff changeset
   309
            from reportlab.lib.utils import open_for_read
5bf5d0f03603 Make Image to use open_for_read
rgbecker
parents: 2237
diff changeset
   310
            f = open_for_read(filename, 'b')
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   311
            info = pdfutils.readJPEGInfo(f)
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   312
            f.close()
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   313
            self.imageWidth = info[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   314
            self.imageHeight = info[1]
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   315
            self._setup(width,height,kind,0)
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   316
            self._img = None
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   317
        elif fp:
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   318
            self._setup(width,height,kind,0)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   319
        else:
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   320
            self._setup(width,height,kind,lazy)
1477
dc5c2ba1005a Allow for various Image kinds
rgbecker
parents: 1426
diff changeset
   321
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   322
    def _setup(self,width,height,kind,lazy):
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   323
        self._lazy = lazy
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   324
        self._width = width
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   325
        self._height = height
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   326
        self._kind = kind
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   327
        if lazy<=0: self._setup_inner()
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   328
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   329
    def _setup_inner(self):
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   330
        width = self._width
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   331
        height = self._height
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   332
        kind = self._kind
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   333
        (self.imageWidth, self.imageHeight) = self._img.getSize()
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   334
        if self._lazy>=2: del self._img
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   335
        if kind in ['direct','absolute']:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   336
            self.drawWidth = width or self.imageWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   337
            self.drawHeight = height or self.imageHeight
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   338
        elif kind in ['percentage','%']:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   339
            self.drawWidth = self.imageWidth*width*0.01
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   340
            self.drawHeight = self.imageHeight*height*0.01
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   341
        elif kind in ['bound','proportional']:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   342
            factor = min(float(width)/self.imageWidth,float(height)/self.imageHeight)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   343
            self.drawWidth = self.imageWidth*factor
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   344
            self.drawHeight = self.imageHeight*factor
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   345
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   346
    def __getattr__(self,a):
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   347
        if a=='_img':
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   348
            from reportlab.lib.utils import ImageReader  #this may raise an error
2237
005d070deb87 Fix up more Image cases
rgbecker
parents: 2200
diff changeset
   349
            self._img = ImageReader(self._file)
005d070deb87 Fix up more Image cases
rgbecker
parents: 2200
diff changeset
   350
            del self._file
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   351
            return self._img
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   352
        elif a in ('drawWidth','drawHeight','imageWidth','imageHeight'):
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   353
            self._setup_inner()
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   354
            return self.__dict__[a]
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   355
        raise AttributeError(a)
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   356
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   357
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   358
        #the caller may decide it does not fit.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   359
        return (self.drawWidth, self.drawHeight)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   360
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   361
    def draw(self):
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   362
        lazy = self._lazy
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   363
        if lazy>=2: self._lazy = 1
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   364
        self.canv.drawImage(    self._img or self.filename,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   365
                                0,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   366
                                0,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   367
                                self.drawWidth,
1880
5444027dd195 Allow for image transparancy mask
rgbecker
parents: 1879
diff changeset
   368
                                self.drawHeight,
1882
ce334775a5b2 Fixed mask bug (lexical/dynamic scoping?)
andy_robinson
parents: 1880
diff changeset
   369
                                mask=self._mask,
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   370
                                )
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   371
        if lazy>=2:
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   372
            self._img = None
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   373
            self._lazy = lazy
1103
857af510186d Added identity method to Flowables
rgbecker
parents: 1036
diff changeset
   374
2192
955d4bf3b9d2 Candidate fix for infinite looping
andy_robinson
parents: 2172
diff changeset
   375
    def identity(self,maxLen=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   376
        r = Flowable.identity(self,maxLen)
2080
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   377
        if r[-4:]=='>...' and type(self.filename) is StringType:
39156f56ab6f Added in lazy Image stuff
rgbecker
parents: 2045
diff changeset
   378
            r = "%s filename=%s>" % (r[:-4],self.filename)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   379
        return r
1103
857af510186d Added identity method to Flowables
rgbecker
parents: 1036
diff changeset
   380
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   381
class Spacer(Flowable):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   382
    """A spacer just takes up space and doesn't draw anything - it guarantees
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   383
       a gap between objects."""
1917
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1882
diff changeset
   384
    _fixedWidth = 1
25b37daf5ab7 Prepare for a more general idea of cell size
rgbecker
parents: 1882
diff changeset
   385
    _fixedHeight = 1
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   386
    def __init__(self, width, height):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   387
        self.width = width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   388
        self.height = height
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   389
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   390
    def __repr__(self):
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   391
        return "%s(%s, %s)" % (self.__class__.__name__,self.width, self.height)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   392
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   393
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   394
        pass
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   395
307
c439e402b404 Added CondPageBreak
rgbecker
parents: 268
diff changeset
   396
class PageBreak(Spacer):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   397
    """Move on to the next page in the document.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   398
       This works by consuming all remaining space in the frame!"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   399
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   400
        pass
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   401
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   402
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   403
        return "PageBreak()"
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   404
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   405
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   406
        self.width = availWidth
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   407
        self.height = availHeight
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   408
        return (availWidth,availHeight)  #step back a point
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   409
307
c439e402b404 Added CondPageBreak
rgbecker
parents: 268
diff changeset
   410
class CondPageBreak(Spacer):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   411
    """Throw a page if not enough vertical space"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   412
    def __init__(self, height):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   413
        self.height = height
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   414
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   415
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   416
        return "CondPageBreak(%s)" %(self.height,)
307
c439e402b404 Added CondPageBreak
rgbecker
parents: 268
diff changeset
   417
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   418
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   419
        if availHeight<self.height:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   420
            return (availWidth, availHeight)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   421
        return (0, 0)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   422
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   423
def _listWrapOn(F,availWidth,canv,mergeSpace=1):
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   424
    '''return max width, required height for a list of flowables F'''
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   425
    W = 0
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   426
    H = 0
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   427
    pS = 0
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   428
    n = len(F)
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   429
    nm1 = n - 1
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   430
    for i in xrange(n):
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   431
        f = F[i]
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   432
        w,h = f.wrapOn(canv,availWidth,0xfffffff)
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   433
        W = max(W,w)
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   434
        H = H+h
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   435
        if i:
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   436
            h = f.getSpaceBefore()
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   437
            if mergeSpace: H += max(h-pS,0) 
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   438
            else: H += h
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   439
        if i!=nm1:
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   440
            pS = f.getSpaceAfter()
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   441
            H += pS
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   442
    return W, H
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   443
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   444
def _makeIndexable(V):
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   445
    if type(V) not in (ListType, TupleType): V = V is not None and [V] or []
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   446
    return V
558
f9013e25af9d Added a random text module
andy_robinson
parents: 557
diff changeset
   447
367
ca1adb0294f0 Added KeepTogether
rgbecker
parents: 307
diff changeset
   448
class KeepTogether(Flowable):
1994
fdb2334c338a Attempt to allow for overlong KeepTogethers
rgbecker
parents: 1917
diff changeset
   449
    def __init__(self,flowables,maxHeight=None):
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   450
        self._flowables = _makeIndexable(flowables)
1994
fdb2334c338a Attempt to allow for overlong KeepTogethers
rgbecker
parents: 1917
diff changeset
   451
        self._maxHeight = maxHeight
367
ca1adb0294f0 Added KeepTogether
rgbecker
parents: 307
diff changeset
   452
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   453
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   454
        f = self._flowables
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   455
        L = map(repr,f)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   456
        import string
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   457
        L = "\n"+string.join(L, "\n")
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   458
        L = string.replace(L, "\n", "\n  ")
1994
fdb2334c338a Attempt to allow for overlong KeepTogethers
rgbecker
parents: 1917
diff changeset
   459
        return "KeepTogether(%s,maxHeight=%s) # end KeepTogether" % (L,self._maxHeight)
541
33de80b3655c added __repr__s and enhanced exception messages for debugging
aaron_watters
parents: 512
diff changeset
   460
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   461
    def wrap(self, aW, aH):
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   462
        W,H = _listWrapOn(self._flowables,aW,self.canv)
1994
fdb2334c338a Attempt to allow for overlong KeepTogethers
rgbecker
parents: 1917
diff changeset
   463
        self._CPage = (H>aH) and (not self._maxHeight or H<=self._maxHeight)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   464
        return W, 0xffffff  # force a split
367
ca1adb0294f0 Added KeepTogether
rgbecker
parents: 307
diff changeset
   465
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   466
    def split(self, aW, aH):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   467
        S = getattr(self,'_CPage',1) and [CondPageBreak(aH+1)] or []
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   468
        for f in self._flowables: S.append(f)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   469
        return S
367
ca1adb0294f0 Added KeepTogether
rgbecker
parents: 307
diff changeset
   470
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   471
class Macro(Flowable):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   472
    """This is not actually drawn (i.e. it has zero height)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   473
    but is executed when it would fit in the frame.  Allows direct
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   474
    access to the canvas through the object 'canvas'"""
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   475
    def __init__(self, command):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   476
        self.command = command
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   477
    def __repr__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   478
        return "Macro(%s)" % repr(self.command)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   479
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   480
        return (0,0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1554
diff changeset
   481
    def draw(self):
1768
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   482
        exec self.command in globals(), {'canvas':self.canv}
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   483
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   484
class ParagraphAndImage(Flowable):
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   485
    '''combine a Paragraph and an Image'''
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   486
    def __init__(self,P,I,xpad=3,ypad=3):
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   487
        self.P, self.style, self.I, self.xpad, self.ypad = P,P.style,I,xpad,ypad
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   488
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   489
    def wrap(self,availWidth,availHeight):
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   490
        wI, hI = self.I.wrap(availWidth,availHeight)
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   491
        self.wI, self.hI = wI, hI
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   492
        # work out widths array for breaking
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   493
        self.width = availWidth
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   494
        P, style, xpad, ypad = self.P, self.style, self.xpad, self.ypad
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   495
        leading = style.leading
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   496
        leftIndent = style.leftIndent
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   497
        later_widths = availWidth - leftIndent - style.rightIndent
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   498
        intermediate_widths = later_widths - xpad - wI
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   499
        first_line_width = intermediate_widths - style.firstLineIndent
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   500
        P.width = 0
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   501
        P.blPara = P.breakLines([first_line_width] + int((hI+ypad)/leading)*[intermediate_widths]+[later_widths])
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   502
        P.height = len(P.blPara.lines)*leading
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   503
        self.height = max(hI,P.height)
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   504
        return (self.width, self.height)
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   505
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   506
    def split(self,availWidth, availHeight):
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   507
        P, wI, hI, ypad = self.P, self.wI, self.hI, self.ypad
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   508
        if hI+ypad>availHeight or len(P.frags)<=0: return []
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   509
        S = P.split(availWidth,availHeight)
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   510
        if not S: return S
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   511
        P = self.P = S[0]
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   512
        del S[0]
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   513
        style = self.style = P.style
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   514
        P.height = len(self.P.blPara.lines)*style.leading
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   515
        self.height = max(hI,P.height)
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   516
        return [self]+S
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   517
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   518
    def draw(self):
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   519
        canv = self.canv
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   520
        self.I.drawOn(canv,self.width-self.wI-self.xpad,self.height-self.hI)
ea95b92748cc Added ParagraphAndImage
rgbecker
parents: 1683
diff changeset
   521
        self.P.drawOn(canv,0,0)
2113
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   522
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   523
class FailOnWrap(Flowable):
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   524
    def wrap(self, availWidth, availHeight):
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   525
        raise ValueError("FailOnWrap flowable wrapped and failing as ordered!")
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2192
diff changeset
   526
2113
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   527
    def draw(self):
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   528
        pass
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   529
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   530
class FailOnDraw(Flowable):
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   531
    def wrap(self, availWidth, availHeight):
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   532
        return (0,0)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2192
diff changeset
   533
2113
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   534
    def draw(self):
e82d8b3880d8 Preliminary support for tracing through doc builds
andy_robinson
parents: 2080
diff changeset
   535
        raise ValueError("FailOnDraw flowable drawn, and failing as ordered!")
2276
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   536
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   537
class HRFlowable(Flowable):
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   538
    '''Like the hr tag'''
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   539
    def __init__(self,
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   540
            width="80%",
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   541
            thickness=1,
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   542
            lineCap='round',
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   543
            color=lightgrey,
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   544
            spaceBefore=1, spaceAfter=1,
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   545
            hAlign='CENTER', vAlign='BOTTOM'):
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   546
        Flowable.__init__(self)
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   547
        self.width = width
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   548
        self.lineWidth = thickness
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   549
        self.lineCap=lineCap
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   550
        self.spaceBefore = spaceBefore
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   551
        self.spaceAfter = spaceAfter
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   552
        self.color = color
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   553
        self.hAlign = hAlign
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   554
        self.vAlign = vAlign
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   555
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   556
    def __repr__(self):
2284
b181b45c6792 Minor changes
rgbecker
parents: 2276
diff changeset
   557
        return "HRFlowable(width=%s, height=%s)" % (self.width, self.height)
2276
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   558
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   559
    def wrap(self, availWidth, availHeight):
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   560
        w = self.width
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   561
        if type(w) is type(''):
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   562
            w = w.strip()
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   563
            if w.endswith('%'): w = availWidth*float(w[:-1])*0.01
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   564
            else: w = float(w)
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   565
        w = min(w,availWidth)
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   566
        self._width = w
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   567
        return w, self.lineWidth
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   568
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   569
    def draw(self):
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   570
        canv = self.canv
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   571
        canv.saveState()
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   572
        canv.setLineWidth(self.lineWidth)
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   573
        canv.setLineCap({'butt':0,'round':1, 'square': 2}[self.lineCap.lower()])
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   574
        canv.setStrokeColor(self.color)
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   575
        canv.line(0, 0, self._width, self.height)
10fc2ce1114f Added HRFlowable
rgbecker
parents: 2241
diff changeset
   576
        canv.restoreState()
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   577
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   578
class _PTOInfo:
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   579
    def __init__(self,trailer,header):
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   580
        self.trailer = _makeIndexable(trailer)
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   581
        self.header = _makeIndexable(header)
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   582
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   583
class PTOContainer(Flowable):
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   584
    '''PTOContainer(contentList,trailerList,headerList)
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   585
    
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   586
    A container for flowables decorated with trailer & header lists.
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   587
    If the split operation would be called then the trailer and header
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   588
    lists are injected before and after the split. This allows specialist
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   589
    "please turn over" and "continued from previous" like behaviours.''' 
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   590
    def __init__(self,content,trailer=None,header=None):
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   591
        I = _PTOInfo(trailer,header)
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   592
        self._content = C = []
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   593
        for _ in _makeIndexable(content):
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   594
            if isinstance(_,PTOContainer):
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   595
                C.extend(_._content)
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   596
            else:
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   597
                C.append(_)
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   598
                if not hasattr(_,'_ptoinfo'): _._ptoinfo = I
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   599
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   600
    def wrap(self,availWidth,availHeight):
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   601
        self.width, self.height = _listWrapOn(self._content,availWidth,self.canv)
2384
6d44252e1042 platypus: added ptocontainer, moved tables test
rgbecker
parents: 2376
diff changeset
   602
        return self.width,self.height
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   603
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   604
    def getSpaceBefore(self):
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   605
        return self._content[0].getSpaceBefore()
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   606
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   607
    def getSpaceAfter(self):
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   608
        return self._content[-1].getSpaceAfter()
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   609
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   610
    def split(self, availWidth, availHeight):
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   611
        canv = self.canv
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   612
        C = self._content
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   613
        x = i = H = pS = 0
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   614
        n = len(C)
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   615
        I2W = {}
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   616
        for x in xrange(n):
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   617
            c = C[x]
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   618
            I = c._ptoinfo
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   619
            if I not in I2W:
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   620
                T = I.trailer
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   621
                Hdr = I.header
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   622
                tW, tH = _listWrapOn(T, availWidth, self.canv)
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   623
                tSB = T[0].getSpaceBefore()
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   624
                I2W[I] = T,tW,tH,tSB
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   625
            else:
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   626
                T,tW,tH,tSB = I2W[I]
2384
6d44252e1042 platypus: added ptocontainer, moved tables test
rgbecker
parents: 2376
diff changeset
   627
            _, h = c.wrapOn(canv,availWidth,0xfffffff)
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   628
            if x: h += max(c.getSpaceBefore()-pS,0)
2384
6d44252e1042 platypus: added ptocontainer, moved tables test
rgbecker
parents: 2376
diff changeset
   629
            pS = c.getSpaceAfter()
6d44252e1042 platypus: added ptocontainer, moved tables test
rgbecker
parents: 2376
diff changeset
   630
            H += h+pS
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   631
            if H+tH+max(tSB,pS)>=availHeight-_FUZZ: break
2384
6d44252e1042 platypus: added ptocontainer, moved tables test
rgbecker
parents: 2376
diff changeset
   632
            i += 1
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   633
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   634
        #first retract last thing we tried
2384
6d44252e1042 platypus: added ptocontainer, moved tables test
rgbecker
parents: 2376
diff changeset
   635
        H -= (h+pS)
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   636
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   637
        #attempt a sub split on the last one we have
2384
6d44252e1042 platypus: added ptocontainer, moved tables test
rgbecker
parents: 2376
diff changeset
   638
        aH = (availHeight - H - max(pS,tSB) - tH)*0.99
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   639
        if aH>=0.05*availHeight:
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   640
            SS = c.splitOn(canv,availWidth,aH)
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   641
        else:
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   642
            SS = []
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   643
        if SS:
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   644
            from doctemplate import  FrameBreak
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   645
            F = [FrameBreak()]
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2375
diff changeset
   646
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   647
        if SS:
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   648
            R1 = C[:i] + SS[:1] + T + F
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2375
diff changeset
   649
            R2 = Hdr + SS[1:]+C[i+1:]
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   650
        elif not i:
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   651
            return []
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   652
        else:
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   653
            R1 = C[:i-1]+T
2376
7e70411a7236 flowables.py: minor change to PTOContainer
rgbecker
parents: 2375
diff changeset
   654
            R2 = Hdr + C[i:]
2387
75caa41a818f platypus/test: working pto (& inner pto)
rgbecker
parents: 2384
diff changeset
   655
        return R1 + [PTOContainer(R2,deepcopy(I.trailer),deepcopy(I.header))]
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   656
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   657
    def drawOn(self, canv, x, y, _sW=0):
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   658
        '''we simulate being added to a frame'''
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   659
        pS = 0
2384
6d44252e1042 platypus: added ptocontainer, moved tables test
rgbecker
parents: 2376
diff changeset
   660
        aW = self.width+_sW
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   661
        C = self._content
2384
6d44252e1042 platypus: added ptocontainer, moved tables test
rgbecker
parents: 2376
diff changeset
   662
        y += self.height
2375
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   663
        for c in C:
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   664
            w, h = c.wrapOn(canv,aW,0xfffffff)
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   665
            if c is not C[0]: h += max(c.getSpaceBefore()-pS,0)
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   666
            y -= h
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   667
            c.drawOn(canv,x,y,_sW=aW-w)
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   668
            if c is not C[-1]:
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   669
                pS = c.getSpaceAfter()
1e175a4474d0 platypus: almost finished PTOContainer
rgbecker
parents: 2341
diff changeset
   670
                y -= pS