src/reportlab/platypus/frames.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4370 823a8c33ce43
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 470
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2213
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/platypus/frames.py
565
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
     4
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4172
diff changeset
     5
__version__='3.3.0'
565
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
     6
3032
22224b1b4d24 New docstrings mainly for module titles
damian
parents: 3031
diff changeset
     7
__doc__="""A frame is a container for content on a page.
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
     8
"""
565
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
     9
2500
ea0e8cec358c added a platypus logger
andy
parents: 2379
diff changeset
    10
import logging
ea0e8cec358c added a platypus logger
andy
parents: 2379
diff changeset
    11
logger = logging.getLogger('reportlab.platypus')
ea0e8cec358c added a platypus logger
andy
parents: 2379
diff changeset
    12
565
179927300074 Minor changes.
dinu_gherman
parents: 541
diff changeset
    13
_geomAttr=('x1', 'y1', 'width', 'height', 'leftPadding', 'bottomPadding', 'rightPadding', 'topPadding')
4090
054784834fed frames.py: fix bitbucket issue 29 contributed by Chadwick Rolfs
robin
parents: 4067
diff changeset
    14
from reportlab import rl_config, isPy3
2379
6226a940d978 move _FUZZ to rl_config
rgbecker
parents: 2375
diff changeset
    15
_FUZZ=rl_config._FUZZ
1231
05dc2104f72d disable LayoutError error for production use (option to enable for debug)
aaron_watters
parents: 1035
diff changeset
    16
2735
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
    17
class ShowBoundaryValue:
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
    18
    def __init__(self,color=(0,0,0),width=0.1):
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
    19
        self.color = color
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
    20
        self.width = width
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
    21
4090
054784834fed frames.py: fix bitbucket issue 29 contributed by Chadwick Rolfs
robin
parents: 4067
diff changeset
    22
    if isPy3:
054784834fed frames.py: fix bitbucket issue 29 contributed by Chadwick Rolfs
robin
parents: 4067
diff changeset
    23
        def __bool__(self):
054784834fed frames.py: fix bitbucket issue 29 contributed by Chadwick Rolfs
robin
parents: 4067
diff changeset
    24
            return self.color is not None and self.width>=0
054784834fed frames.py: fix bitbucket issue 29 contributed by Chadwick Rolfs
robin
parents: 4067
diff changeset
    25
    else:
054784834fed frames.py: fix bitbucket issue 29 contributed by Chadwick Rolfs
robin
parents: 4067
diff changeset
    26
        def __nonzero__(self):
054784834fed frames.py: fix bitbucket issue 29 contributed by Chadwick Rolfs
robin
parents: 4067
diff changeset
    27
            return self.color is not None and self.width>=0
054784834fed frames.py: fix bitbucket issue 29 contributed by Chadwick Rolfs
robin
parents: 4067
diff changeset
    28
2735
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
    29
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    30
class Frame:
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    31
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    32
    A Frame is a piece of space in a document that is filled by the
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    33
    "flowables" in the story.  For example in a book like document most
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    34
    pages have the text paragraphs in one or two frames.  For generality
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    35
    a page might have several frames (for example for 3 column text or
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    36
    for text that wraps around a graphic).
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
    37
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    38
    After creation a Frame is not usually manipulated directly by the
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    39
    applications program -- it is used internally by the platypus modules.
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
    40
3031
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3010
diff changeset
    41
    Here is a diagramatid abstraction for the definitional part of a Frame::
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    42
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    43
                width                    x2,y2
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    44
        +---------------------------------+
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    45
        | l  top padding                r | h
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    46
        | e +-------------------------+ i | e
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    47
        | f |                         | g | i
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    48
        | t |                         | h | g
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    49
        |   |                         | t | h
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    50
        | p |                         |   | t
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    51
        | a |                         | p |
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    52
        | d |                         | a |
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    53
        |   |                         | d |
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    54
        |   +-------------------------+   |
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    55
        |    bottom padding               |
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    56
        +---------------------------------+
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    57
        (x1,y1) <-- lower left corner
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
    58
3031
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3010
diff changeset
    59
    NOTE!! Frames are stateful objects.  No single frame should be used in
6f90e7668adb docstrings cleaned up for epydoc
tim
parents: 3010
diff changeset
    60
    two documents at the same time (especially in the presence of multithreading.
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    61
    '''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    62
    def __init__(self, x1, y1, width,height, leftPadding=6, bottomPadding=6,
2213
edf7d844279c Added overlapAttachedSpace
rgbecker
parents: 2200
diff changeset
    63
            rightPadding=6, topPadding=6, id=None, showBoundary=0,
2531
40b6f4b41f7c platypus: changes to make identity more useful
rgbecker
parents: 2525
diff changeset
    64
            overlapAttachedSpace=None,_debug=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    65
        self.id = id
2531
40b6f4b41f7c platypus: changes to make identity more useful
rgbecker
parents: 2525
diff changeset
    66
        self._debug = _debug
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    67
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    68
        #these say where it goes on the page
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    69
        self.__dict__['_x1'] = x1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    70
        self.__dict__['_y1'] = y1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    71
        self.__dict__['_width'] = width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    72
        self.__dict__['_height'] = height
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    73
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    74
        #these create some padding.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    75
        self.__dict__['_leftPadding'] = leftPadding
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    76
        self.__dict__['_bottomPadding'] = bottomPadding
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    77
        self.__dict__['_rightPadding'] = rightPadding
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    78
        self.__dict__['_topPadding'] = topPadding
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    79
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    80
        # if we want a boundary to be shown
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    81
        self.showBoundary = showBoundary
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    82
2213
edf7d844279c Added overlapAttachedSpace
rgbecker
parents: 2200
diff changeset
    83
        if overlapAttachedSpace is None: overlapAttachedSpace = rl_config.overlapAttachedSpace
edf7d844279c Added overlapAttachedSpace
rgbecker
parents: 2200
diff changeset
    84
        self._oASpace = overlapAttachedSpace
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    85
        self._geom()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    86
        self._reset()
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
    87
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    88
    def __getattr__(self,a):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    89
        if a in _geomAttr: return self.__dict__['_'+a]
2915
f2c1f8ef3b51 reportlab: obsoletisms removed
rgbecker
parents: 2735
diff changeset
    90
        raise AttributeError(a)
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    91
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    92
    def __setattr__(self,a,v):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    93
        if a in _geomAttr:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    94
            self.__dict__['_'+a] = v
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    95
            self._geom()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    96
        else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
    97
            self.__dict__[a] = v
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
    98
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
    99
    def _saveGeom(self, **kwds):
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   100
        if not self.__dict__.setdefault('_savedGeom',{}):
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   101
            for ga in _geomAttr:
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   102
                ga = '_'+ga
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   103
                self.__dict__['_savedGeom'][ga] = self.__dict__[ga]
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   104
        for k,v in kwds.items():
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   105
            setattr(self,k,v)
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   106
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   107
    def _restoreGeom(self):
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   108
        if self.__dict__.get('_savedGeom',None):
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   109
            for ga in _geomAttr:
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   110
                ga = '_'+ga
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   111
                self.__dict__[ga] = self.__dict__[ga]['_savedGeom']
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   112
                del self.__dict__['_savedGeom']
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   113
            self._geom()
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   114
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   115
    def _geom(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   116
        self._x2 = self._x1 + self._width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   117
        self._y2 = self._y1 + self._height
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   118
        #efficiency
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   119
        self._y1p = self._y1 + self._bottomPadding
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   120
        #work out the available space
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   121
        self._aW = self._x2 - self._x1 - self._leftPadding - self._rightPadding
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   122
        self._aH = self._y2 - self._y1p - self._topPadding
410
b5c0992c9b99 geometry changing attributes now work
rgbecker
parents: 332
diff changeset
   123
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   124
    def _reset(self):
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   125
        self._restoreGeom()
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   126
        #drawing starts at top left
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   127
        self._x = self._x1 + self._leftPadding
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   128
        self._y = self._y2 - self._topPadding
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   129
        self._atTop = 1
2213
edf7d844279c Added overlapAttachedSpace
rgbecker
parents: 2200
diff changeset
   130
        self._prevASpace = 0
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   131
2525
634ec1f48514 platypus: initial FrameFlowable implementation
rgbecker
parents: 2500
diff changeset
   132
        # these two should NOT be set on a frame.
634ec1f48514 platypus: initial FrameFlowable implementation
rgbecker
parents: 2500
diff changeset
   133
        # they are used when Indenter flowables want
634ec1f48514 platypus: initial FrameFlowable implementation
rgbecker
parents: 2500
diff changeset
   134
        # to adjust edges e.g. to do nested lists
634ec1f48514 platypus: initial FrameFlowable implementation
rgbecker
parents: 2500
diff changeset
   135
        self._leftExtraIndent = 0.0
634ec1f48514 platypus: initial FrameFlowable implementation
rgbecker
parents: 2500
diff changeset
   136
        self._rightExtraIndent = 0.0
634ec1f48514 platypus: initial FrameFlowable implementation
rgbecker
parents: 2500
diff changeset
   137
1923
e692491f0967 Added relative indentation
andy_robinson
parents: 1838
diff changeset
   138
    def _getAvailableWidth(self):
e692491f0967 Added relative indentation
andy_robinson
parents: 1838
diff changeset
   139
        return self._aW - self._leftExtraIndent - self._rightExtraIndent
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2013
diff changeset
   140
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   141
    def _add(self, flowable, canv, trySplit=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   142
        """ Draws the flowable at the current position.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   143
        Returns 1 if successful, 0 if it would not fit.
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   144
        Raises a LayoutError if the object is too wide,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   145
        or if it is too high for a totally empty frame,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   146
        to avoid infinite loops"""
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   147
        flowable._frame = self
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   148
        flowable.canv = canv #so they can use stringWidth etc
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   149
        try:
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   150
            if getattr(flowable,'frameAction',None):
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   151
                flowable.frameAction(self)
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   152
                return 1
2525
634ec1f48514 platypus: initial FrameFlowable implementation
rgbecker
parents: 2500
diff changeset
   153
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   154
            y = self._y
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   155
            p = self._y1p
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   156
            s = 0
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   157
            aW = self._getAvailableWidth()
4172
842c7f90976a make Frame._add notice _ZEROSIZE
robin
parents: 4090
diff changeset
   158
            zeroSize = getattr(flowable,'_ZEROSIZE',False)
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   159
            if not self._atTop:
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   160
                s =flowable.getSpaceBefore()
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   161
                if self._oASpace:
4172
842c7f90976a make Frame._add notice _ZEROSIZE
robin
parents: 4090
diff changeset
   162
                    if getattr(flowable,'_SPACETRANSFER',False) or zeroSize:
3750
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   163
                        s = self._prevASpace
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   164
                    s = max(s-self._prevASpace,0)
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   165
            h = y - p - s
4172
842c7f90976a make Frame._add notice _ZEROSIZE
robin
parents: 4090
diff changeset
   166
            if h>0 or zeroSize:
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   167
                w, h = flowable.wrap(aW, h)
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   168
            else:
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   169
                return 0
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   170
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   171
            h += s
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   172
            y -= h
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   173
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   174
            if y < p-_FUZZ:
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   175
                if not rl_config.allowTableBoundsErrors and ((h>self._aH or w>aW) and not trySplit):
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   176
                    from reportlab.platypus.doctemplate import LayoutError
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   177
                    raise LayoutError("Flowable %s (%sx%s points) too large for frame (%sx%s points)." % (
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   178
                        flowable.__class__, w,h, aW,self._aH))
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   179
                return 0
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   180
            else:
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   181
                #now we can draw it, and update the current point.
3750
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   182
                s = flowable.getSpaceAfter()
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   183
                fbg = getattr(self,'_frameBGs',None)
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   184
                if fbg:
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   185
                    fbgl, fbgr, fbgc = fbg[-1]
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   186
                    fbw = self._width-fbgl-fbgr
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   187
                    fbh = y + h + s
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   188
                    fby = max(p,y-s)
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   189
                    fbh = max(0,fbh-fby)
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   190
                    if abs(fbw)>_FUZZ and abs(fbh)>_FUZZ:
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   191
                        canv.saveState()
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   192
                        canv.setFillColor(fbgc)
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   193
                        canv.rect(self._x1+fbgl,fby,fbw,fbh,stroke=0,fill=1)
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   194
                        canv.restoreState()
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   195
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   196
                flowable.drawOn(canv, self._x + self._leftExtraIndent, y, _sW=aW-w)
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   197
                flowable.canv=canv
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   198
                if self._debug: logger.debug('drew %s' % flowable.identity())
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   199
                y -= s
3750
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   200
                if self._oASpace:
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   201
                    if getattr(flowable,'_SPACETRANSFER',False):
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   202
                        s = self._prevASpace
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   203
                    self._prevASpace = s
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   204
                if y!=self._y: self._atTop = 0
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   205
                self._y = y
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   206
                return 1
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   207
        finally:
3010
1a6fc2f5876a frames.py: fix occasional bad traceback bug
rgbecker
parents: 2970
diff changeset
   208
            #sometimes canv/_frame aren't still on the flowable
1a6fc2f5876a frames.py: fix occasional bad traceback bug
rgbecker
parents: 2970
diff changeset
   209
            for a in ('canv', '_frame'):
1a6fc2f5876a frames.py: fix occasional bad traceback bug
rgbecker
parents: 2970
diff changeset
   210
                if hasattr(flowable,a):
1a6fc2f5876a frames.py: fix occasional bad traceback bug
rgbecker
parents: 2970
diff changeset
   211
                    delattr(flowable,a)
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   212
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   213
    add = _add
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   214
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   215
    def split(self,flowable,canv):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   216
        '''Ask the flowable to split using up the available space.'''
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   217
        y = self._y
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   218
        p = self._y1p
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   219
        s = 0
2970
17c7629174cf frames.py: bug & fix in Frame.split contributed by Matt Folwell
rgbecker
parents: 2964
diff changeset
   220
        if not self._atTop:
17c7629174cf frames.py: bug & fix in Frame.split contributed by Matt Folwell
rgbecker
parents: 2964
diff changeset
   221
            s = flowable.getSpaceBefore()
17c7629174cf frames.py: bug & fix in Frame.split contributed by Matt Folwell
rgbecker
parents: 2964
diff changeset
   222
            if self._oASpace:
17c7629174cf frames.py: bug & fix in Frame.split contributed by Matt Folwell
rgbecker
parents: 2964
diff changeset
   223
                s = max(s-self._prevASpace,0)
3536
770d419eef48 frames.py: put _frame on flowables in Frame.split
rgbecker
parents: 3131
diff changeset
   224
        flowable._frame = self                  #some flowables might need these
770d419eef48 frames.py: put _frame on flowables in Frame.split
rgbecker
parents: 3131
diff changeset
   225
        flowable.canv = canv        
770d419eef48 frames.py: put _frame on flowables in Frame.split
rgbecker
parents: 3131
diff changeset
   226
        try:
770d419eef48 frames.py: put _frame on flowables in Frame.split
rgbecker
parents: 3131
diff changeset
   227
            r = flowable.split(self._aW, y-p-s)
770d419eef48 frames.py: put _frame on flowables in Frame.split
rgbecker
parents: 3131
diff changeset
   228
        finally:
770d419eef48 frames.py: put _frame on flowables in Frame.split
rgbecker
parents: 3131
diff changeset
   229
            #sometimes canv/_frame aren't still on the flowable
770d419eef48 frames.py: put _frame on flowables in Frame.split
rgbecker
parents: 3131
diff changeset
   230
            for a in ('canv', '_frame'):
770d419eef48 frames.py: put _frame on flowables in Frame.split
rgbecker
parents: 3131
diff changeset
   231
                if hasattr(flowable,a):
770d419eef48 frames.py: put _frame on flowables in Frame.split
rgbecker
parents: 3131
diff changeset
   232
                    delattr(flowable,a)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   233
        return r
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   234
3750
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   235
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   236
    def drawBoundary(self,canv):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   237
        "draw the frame boundary as a rectangle (primarily for debugging)."
3750
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   238
        from reportlab.lib.colors import Color, toColor
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   239
        sb = self.showBoundary
3750
63d5d0c8665e flowables.py & frames.py: attempt to improve handling of various Zero size flowables
robin
parents: 3721
diff changeset
   240
        ss = isinstance(sb,(str,tuple,list)) or isinstance(sb,Color)
2735
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   241
        w = -1
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   242
        if ss:
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   243
            c = toColor(sb,self)
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   244
            ss = c is not self
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   245
        elif isinstance(sb,ShowBoundaryValue) and sb:
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   246
            c = toColor(sb.color,self)
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   247
            w = sb.width
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   248
            ss = c is not self
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   249
        if ss:
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   250
            canv.saveState()
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   251
            canv.setStrokeColor(c)
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   252
            if w>=0:
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   253
                canv.setLineWidth(w)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   254
        canv.rect(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   255
                self._x1,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   256
                self._y1,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   257
                self._x2 - self._x1,
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   258
                self._y2 - self._y1
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   259
                )
2735
70cf084e5811 reportlab: fix justification space counting bug and improve tests
rgbecker
parents: 2593
diff changeset
   260
        if ss: canv.restoreState()
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   261
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   262
    def addFromList(self, drawlist, canv):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   263
        """Consumes objects from the front of the list until the
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   264
        frame is full.  If it cannot fit one object, raises
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   265
        an exception."""
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents:
diff changeset
   266
2532
130a873a98f6 frames.py: logger only when _debug
rgbecker
parents: 2531
diff changeset
   267
        if self._debug: logger.debug("enter Frame.addFromlist() for frame %s" % self.id)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   268
        if self.showBoundary:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   269
            self.drawBoundary(canv)
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: 1652
diff changeset
   271
        while len(drawlist) > 0:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   272
            head = drawlist[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   273
            if self.add(head,canv,trySplit=0):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   274
                del drawlist[0]
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   275
            else:
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1652
diff changeset
   276
                #leave it in the list for later
2013
6899a4177d08 I think we actually need a width check
rgbecker
parents: 1923
diff changeset
   277
                break
2950
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   278
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   279
    def add_generated_content(self,*C):
6c243412b06c platypus: add support for FramSplitter flowable
rgbecker
parents: 2915
diff changeset
   280
        self.__dict__.setdefault('_generated_content',[]).extend(C)
3131
0f15fabe9d8d reportlab: improve information in doctemplate overflow error
rgbecker
parents: 3032
diff changeset
   281
0f15fabe9d8d reportlab: improve information in doctemplate overflow error
rgbecker
parents: 3032
diff changeset
   282
    def _aSpaceString(self):
0f15fabe9d8d reportlab: improve information in doctemplate overflow error
rgbecker
parents: 3032
diff changeset
   283
        return '(%s x %s%s)' % (self._getAvailableWidth(),self._aH,self._atTop and '*' or '')