reportlab/platypus/doctemplate.py
author andy_robinson
Sun, 03 Feb 2002 21:25:57 +0000
changeset 1502 125b52eb0a8e
parent 1440 243d35446390
child 1505 e45994cb76bb
permissions -rw-r--r--
Added callbacks throughout
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 405
diff changeset
     1
#copyright ReportLab Inc. 2000
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 405
diff changeset
     2
#see license.txt for license details
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 405
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/doctemplate.py?cvsroot=reportlab
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/platypus/doctemplate.py,v 1.50 2002/02/03 21:25:57 andy_robinson Exp $
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
     5
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
     6
__version__=''' $Id: doctemplate.py,v 1.50 2002/02/03 21:25:57 andy_robinson Exp $ '''
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
     7
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
     8
__doc__="""
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
     9
This module contains the core structure of platypus.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    10
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    11
Platypus constructs documents.  Document styles are determined by DocumentTemplates.
268
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    12
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    13
Each DocumentTemplate contains one or more PageTemplates which defines the look of the
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    14
pages of the document.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    15
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    16
Each PageTemplate has a procedure for drawing the "non-flowing" part of the page
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    17
(for example the header, footer, page number, fixed logo graphic, watermark, etcetera) and
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    18
a set of Frames which enclose the flowing part of the page (for example the paragraphs,
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    19
tables, or non-fixed diagrams of the text).
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    20
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    21
A document is built when a DocumentTemplate is fed a sequence of Flowables.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    22
The action of the build consumes the flowables in order and places them onto
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    23
frames on pages as space allows.  When a frame runs out of space the next frame
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    24
of the page is used.  If no frame remains a new page is created.  A new page
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    25
can also be created if a page break is forced.
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    26
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    27
The special invisible flowable NextPageTemplate can be used to specify
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    28
the page template for the next page (which by default is the one being used
8414113fa500 more documentation changes
aaron_watters
parents: 255
diff changeset
    29
for the current frame).
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    30
"""
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
    31
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    32
from reportlab.platypus.flowables import *
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    33
from reportlab.platypus.paragraph import Paragraph
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    34
from reportlab.platypus.frames import Frame
1131
069fea8037dc Added _verbose control
rgbecker
parents: 1103
diff changeset
    35
from reportlab.rl_config import defaultPageSize, _verbose
279
e7d8b3631d5c Global sequencer put in the 'story builder'.
andy_robinson
parents: 272
diff changeset
    36
import reportlab.lib.sequencer
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
    37
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    38
from types import *
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    39
import sys
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    40
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
    41
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    42
def _doNothing(canvas, doc):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    43
    "Dummy callback for onPage"
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    44
    pass
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
    45
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
    46
1440
243d35446390 Removed 0 from multiBuild stuff prior to further changes;
andy_robinson
parents: 1428
diff changeset
    47
class IndexingFlowable(Flowable):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    48
    """Abstract interface definition for flowables which might
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    49
    hold references to other pages or themselves be targets
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    50
    of cross-references.  XRefStart, XRefDest, Table of Contents,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    51
    Indexes etc."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    52
    def isIndexing(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    53
        return 1
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
    54
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    55
    def isSatisfied(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    56
        return 1
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
    57
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    58
    def notify(self, kind, stuff):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    59
        """This will be called by the framework wherever 'stuff' happens.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    60
        'kind' will be a value that can be used to decide whether to
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    61
        pay attention or not."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    62
        pass
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
    63
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    64
    def beforeBuild(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    65
        """Called by multiBuild before it starts; use this to clear
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    66
        old contents"""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    67
        pass
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
    68
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    69
    def afterBuild(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    70
        """Called after build ends but before isSatisfied"""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    71
        pass
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
    72
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
    73
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    74
class ActionFlowable(Flowable):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    75
    '''This Flowable is never drawn, it can be used for data driven controls
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    76
       For example to change a page template (from one column to two, for example)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    77
       use NextPageTemplate which creates an ActionFlowable.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    78
    '''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    79
    def __init__(self,action=()):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    80
        if type(action) not in (ListType, TupleType):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    81
            action = (action,)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    82
        self.action = tuple(action)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    83
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    84
    def wrap(self, availWidth, availHeight):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    85
        '''Should never be called.'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    86
        raise NotImplementedError
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    87
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    88
    def draw(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    89
        '''Should never be called.'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    90
        raise NotImplementedError
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
    91
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    92
    def apply(self,doc):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    93
        '''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    94
        This is called by the doc.build processing to allow the instance to
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    95
        implement its behaviour
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    96
        '''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    97
        action = self.action[0]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    98
        args = tuple(self.action[1:])
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
    99
        arn = 'handle_'+action
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   100
        try:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   101
            apply(getattr(doc,arn), args)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   102
        except AttributeError, aerr:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   103
            if aerr.args[0]==arn:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   104
                raise NotImplementedError, "Can't handle ActionFlowable(%s)" % action
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   105
            else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   106
                raise
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   107
        except "bogus":
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   108
            t, v, None = sys.exc_info()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   109
            raise t, "%s\n   handle_%s args=%s"%(v,action,args)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   110
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   111
    def __call__(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   112
        return self
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   113
1324
3335a8e81e7b Possible improvements to FrameBreak et al
rgbecker
parents: 1268
diff changeset
   114
class NextFrameFlowable(ActionFlowable):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   115
    def __init__(self,ix,resume=0):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   116
        ActionFlowable.__init__(self,('nextFrame',ix,resume))
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
   117
1324
3335a8e81e7b Possible improvements to FrameBreak et al
rgbecker
parents: 1268
diff changeset
   118
class CurrentFrameFlowable(ActionFlowable):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   119
    def __init__(self,ix,resume=0):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   120
        ActionFlowable.__init__(self,('currentFrame',ix,resume))
1324
3335a8e81e7b Possible improvements to FrameBreak et al
rgbecker
parents: 1268
diff changeset
   121
3335a8e81e7b Possible improvements to FrameBreak et al
rgbecker
parents: 1268
diff changeset
   122
class _FrameBreak(ActionFlowable):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   123
    '''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   124
    A special ActionFlowable that allows setting doc._nextFrameIndex
1324
3335a8e81e7b Possible improvements to FrameBreak et al
rgbecker
parents: 1268
diff changeset
   125
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   126
    eg story.append(FrameBreak('mySpecialFrame'))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   127
    '''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   128
    def __call__(self,ix=None,resume=0):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   129
        r = self.__class__(self.action+(resume,))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   130
        r._ix = ix
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   131
        return r
1324
3335a8e81e7b Possible improvements to FrameBreak et al
rgbecker
parents: 1268
diff changeset
   132
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   133
    def apply(self,doc):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   134
        if getattr(self,'_ix',None): doc._nextFrameIndex = self._ix
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   135
        ActionFlowable.apply(self,doc)
1324
3335a8e81e7b Possible improvements to FrameBreak et al
rgbecker
parents: 1268
diff changeset
   136
3335a8e81e7b Possible improvements to FrameBreak et al
rgbecker
parents: 1268
diff changeset
   137
FrameBreak = _FrameBreak('frameEnd')
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   138
PageBegin = ActionFlowable('pageBegin')
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   139
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   140
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   141
class NextPageTemplate(ActionFlowable):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   142
    """When you get to the next page, use the template specified (change to two column, for example)  """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   143
    def __init__(self,pt):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   144
        ActionFlowable.__init__(self,('nextPageTemplate',pt))
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   145
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
   146
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   147
class PageTemplate:
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   148
    """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   149
    essentially a list of Frames and an onPage routine to call at the start
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   150
    of a page when this is selected. onPageEnd gets called at the end.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   151
    derived classes can also implement beforeDrawPage and afterDrawPage if they want
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   152
    """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   153
    def __init__(self,id=None,frames=[],onPage=_doNothing, onPageEnd=_doNothing,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   154
                 pagesize=defaultPageSize):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   155
        if type(frames) not in (ListType,TupleType): frames = [frames]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   156
        assert filter(lambda x: not isinstance(x,Frame), frames)==[], "frames argument error"
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   157
        self.id = id
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   158
        self.frames = frames
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   159
        self.onPage = onPage
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   160
        self.onPageEnd = onPageEnd
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   161
        self.pagesize = pagesize
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   162
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   163
    def beforeDrawPage(self,canv,doc):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   164
        """Override this if you want additional functionality or prefer
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   165
        a class based page routine.  Called before any flowables for
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   166
        this page are processed."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   167
        pass
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   168
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   169
    def checkPageSize(self,canv,doc):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   170
        '''This gets called by the template framework
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   171
        If canv size != doc size then the canv size is set to
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   172
        the template size or if that's not available to the
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   173
        doc size.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   174
        '''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   175
        #### NEVER EVER EVER COMPARE FLOATS FOR EQUALITY
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   176
        #RGB converting pagesizes to ints means we are accurate to one point
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   177
        #RGB I suggest we should be aiming a little better
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   178
        cp = None
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   179
        dp = None
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   180
        sp = None
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   181
        if canv._pagesize: cp = map(int, canv._pagesize)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   182
        if self.pagesize: sp = map(int, self.pagesize)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   183
        if doc.pagesize: dp = map(int, doc.pagesize)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   184
        if cp!=sp:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   185
            if sp:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   186
                canv.setPageSize(self.pagesize)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   187
            elif cp!=dp:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   188
                canv.setPageSize(doc.pagesize)
936
bd83a2a40227 Dynamic page sizes
rgbecker
parents: 724
diff changeset
   189
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   190
    def afterDrawPage(self, canv, doc):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   191
        """This is called after the last flowable for the page has
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   192
        been processed.  You might use this if the page header or
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   193
        footer needed knowledge of what flowables were drawn on
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   194
        this page."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   195
        pass
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   196
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
   197
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   198
class BaseDocTemplate:
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   199
    """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   200
    First attempt at defining a document template class.
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   201
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   202
    The basic idea is simple.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   203
    0)  The document has a list of data associated with it
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   204
        this data should derive from flowables. We'll have
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   205
        special classes like PageBreak, FrameBreak to do things
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   206
        like forcing a page end etc.
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   207
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   208
    1)  The document has one or more page templates.
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   209
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   210
    2)  Each page template has one or more frames.
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   211
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   212
    3)  The document class provides base methods for handling the
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   213
        story events and some reasonable methods for getting the
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   214
        story flowables into the frames.
214
be55cfb3e54f Added drawPage
rgbecker
parents: 206
diff changeset
   215
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   216
    4)  The document instances can override the base handler routines.
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   217
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   218
    Most of the methods for this class are not called directly by the user,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   219
    but in some advanced usages they may need to be overridden via subclassing.
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   220
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   221
    EXCEPTION: doctemplate.build(...) must be called for most reasonable uses
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   222
    since it builds a document using the page template.
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   223
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   224
    Each document template builds exactly one document into a file specified
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   225
    by the filename argument on initialization.
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   226
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   227
    Possible keyword arguments for the initialization:
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   228
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   229
    pageTemplates: A list of templates.  Must be nonempty.  Names
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   230
      assigned to the templates are used for referring to them so no two used
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   231
      templates should have the same name.  For example you might want one template
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   232
      for a title page, one for a section first page, one for a first page of
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   233
      a chapter and two more for the interior of a chapter on odd and even pages.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   234
      If this argument is omitted then at least one pageTemplate should be provided
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   235
      using the addPageTemplates method before the document is built.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   236
    showBoundary: if set draw a box around the frame boundaries.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   237
    leftMargin:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   238
    rightMargin:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   239
    topMargin:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   240
    bottomMargin:  Margin sizes in points (default 1 inch)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   241
      These margins may be overridden by the pageTemplates.  They are primarily of interest
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   242
      for the SimpleDocumentTemplate subclass.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   243
    allowSplitting:  If set flowables (eg, paragraphs) may be split across frames or pages
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   244
      (default: 1)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   245
    title: Internal title for document (does not automatically display on any page)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   246
    author: Internal author for document (does not automatically display on any page)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   247
    """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   248
    _initArgs = {   'pagesize':defaultPageSize,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   249
                    'pageTemplates':[],
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   250
                    'showBoundary':0,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   251
                    'leftMargin':inch,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   252
                    'rightMargin':inch,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   253
                    'topMargin':inch,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   254
                    'bottomMargin':inch,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   255
                    'allowSplitting':1,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   256
                    'title':None,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   257
                    'author':None,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   258
                    '_pageBreakQuick':1}
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   259
    _invalidInitArgs = ()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   260
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   261
    def __init__(self, filename, **kw):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   262
        """create a document template bound to a filename (see class documentation for keyword arguments)"""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   263
        self.filename = filename
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   264
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   265
        for k in self._initArgs.keys():
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   266
            if not kw.has_key(k):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   267
                v = self._initArgs[k]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   268
            else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   269
                if k in self._invalidInitArgs:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   270
                    raise ValueError, "Invalid argument %s" % k
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   271
                v = kw[k]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   272
            setattr(self,k,v)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   273
        #print "pagesize is", self.pagesize
310
cbec783cfb81 Documentation changes
rgbecker
parents: 305
diff changeset
   274
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   275
        p = self.pageTemplates
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   276
        self.pageTemplates = []
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   277
        self.addPageTemplates(p)
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   278
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   279
        # facility to assist multi-build and cross-referencing.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   280
        # various hooks can put things into here - key is what
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   281
        # you want, value is a page number.  This can then be
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   282
        # passed to indexing flowables.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   283
        self._pageRefs = {}
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   284
        self._indexingFlowables = []
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   285
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   286
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   287
        #callback facility
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   288
        self._onPage = None
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   289
        self._calc()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   290
        self.afterInit()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   291
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   292
    def _calc(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   293
        self._rightMargin = self.pagesize[0] - self.rightMargin
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   294
        self._topMargin = self.pagesize[1] - self.topMargin
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   295
        self.width = self._rightMargin - self.leftMargin
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   296
        self.height = self._topMargin - self.bottomMargin
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   297
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   298
    def setPageCallBack(self, func):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   299
        'Progress monitor - func(pageNo) called on each new page'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   300
        self._onPage = func
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   301
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   302
    def clean_hanging(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   303
        'handle internal postponed actions'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   304
        while len(self._hanging):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   305
            self.handle_flowable(self._hanging)
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   306
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   307
    def addPageTemplates(self,pageTemplates):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   308
        'add one or a sequence of pageTemplates'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   309
        if type(pageTemplates) not in (ListType,TupleType):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   310
            pageTemplates = [pageTemplates]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   311
        #this test below fails due to inconsistent imports!
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   312
        #assert filter(lambda x: not isinstance(x,PageTemplate), pageTemplates)==[], "pageTemplates argument error"
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   313
        for t in pageTemplates:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   314
            self.pageTemplates.append(t)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   315
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   316
    def handle_documentBegin(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   317
        '''implement actions at beginning of document'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   318
        self._hanging = [PageBegin]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   319
        self.pageTemplate = self.pageTemplates[0]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   320
        self.page = 0
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   321
        self.beforeDocument()
253
cfcf8d555a2c Platypus re-organisation
rgbecker
parents: 249
diff changeset
   322
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   323
    def handle_pageBegin(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   324
        '''Perform actions required at beginning of page.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   325
        shouldn't normally be called directly'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   326
        self.page = self.page + 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   327
        self.pageTemplate.beforeDrawPage(self.canv,self)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   328
        self.pageTemplate.checkPageSize(self.canv,self)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   329
        self.pageTemplate.onPage(self.canv,self)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   330
        for f in self.pageTemplate.frames: f._reset()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   331
        self.beforePage()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   332
        if hasattr(self,'_nextFrameIndex'):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   333
            del self._nextFrameIndex
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   334
        self.frame = self.pageTemplate.frames[0]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   335
        self.handle_frameBegin()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   336
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   337
    def handle_pageEnd(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   338
        ''' show the current page
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   339
            check the next page template
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   340
            hang a page begin
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   341
        '''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   342
        self.pageTemplate.afterDrawPage(self.canv, self)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   343
        self.pageTemplate.onPageEnd(self.canv, self)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   344
        self.afterPage()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   345
        self.canv.showPage()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   346
        if hasattr(self,'_nextPageTemplateIndex'):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   347
            self.pageTemplate = self.pageTemplates[self._nextPageTemplateIndex]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   348
            del self._nextPageTemplateIndex
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   349
        self._hanging.append(PageBegin)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   350
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   351
    def handle_pageBreak(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   352
        '''some might choose not to end all the frames'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   353
        if self._pageBreakQuick:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   354
            self.handle_pageEnd()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   355
        else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   356
            n = len(self._hanging)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   357
            while len(self._hanging)==n:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   358
                self.handle_frameEnd()
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   359
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   360
    def handle_frameBegin(self,resume=0):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   361
        '''What to do at the beginning of a frame'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   362
        f = self.frame
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   363
        if f._atTop:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   364
            if self.showBoundary or self.frame.showBoundary:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   365
                self.frame.drawBoundary(self.canv)
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   366
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   367
    def handle_frameEnd(self,resume=0):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   368
        ''' Handles the semantics of the end of a frame. This includes the selection of
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   369
            the next frame or if this is the last frame then invoke pageEnd.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   370
        '''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   371
        if hasattr(self,'_nextFrameIndex'):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   372
            frame = self.pageTemplate.frames[self._nextFrameIndex]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   373
            del self._nextFrameIndex
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   374
            self.handle_frameBegin(resume)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   375
        elif hasattr(self.frame,'lastFrame') or self.frame is self.pageTemplate.frames[-1]:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   376
            self.handle_pageEnd()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   377
            self.frame = None
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   378
        else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   379
            f = self.frame
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   380
            self.frame = self.pageTemplate.frames[self.pageTemplate.frames.index(f) + 1]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   381
            self.handle_frameBegin()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   382
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   383
    def handle_nextPageTemplate(self,pt):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   384
        '''On endPage chenge to the page template with name or index pt'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   385
        if type(pt) is StringType:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   386
            for t in self.pageTemplates:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   387
                if t.id == pt:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   388
                    self._nextPageTemplateIndex = self.pageTemplates.index(t)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   389
                    return
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   390
            raise ValueError, "can't find template('%s')"%pt
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   391
        elif type(pt) is IntType:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   392
            self._nextPageTemplateIndex = pt
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   393
        else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   394
            raise TypeError, "argument pt should be string or integer"
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   395
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   396
    def handle_nextFrame(self,fx):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   397
        '''On endFrame chenge to the frame with name or index fx'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   398
        if type(fx) is StringType:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   399
            for f in self.pageTemplate.frames:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   400
                if f.id == fx:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   401
                    self._nextFrameIndex = self.pageTemplate.frames.index(f)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   402
                    return
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   403
            raise ValueError, "can't find frame('%s')"%fx
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   404
        elif type(fx) is IntType:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   405
            self._nextFrameIndex = fx
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   406
        else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   407
            raise TypeError, "argument fx should be string or integer"
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   408
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   409
    def handle_currentFrame(self,fx):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   410
        '''chenge to the frame with name or index fx'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   411
        if type(fx) is StringType:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   412
            for f in self.pageTemplate.frames:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   413
                if f.id == fx:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   414
                    self._nextFrameIndex = self.pageTemplate.frames.index(f)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   415
                    return
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   416
            raise ValueError, "can't find frame('%s')"%fx
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   417
        elif type(fx) is IntType:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   418
            self._nextFrameIndex = fx
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   419
        else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   420
            raise TypeError, "argument fx should be string or integer"
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   421
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   422
    def handle_breakBefore(self, flowables):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   423
        '''preprocessing step to allow pageBreakBefore and frameBreakBefore attributes'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   424
        first = flowables[0]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   425
        # if we insert a page break before, we'll process that, see it again,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   426
        # and go in an infinite loop.  So we need to set a flag on the object
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   427
        # saying 'skip me'.  This should be unset on the next pass
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   428
        if hasattr(first, '_skipMeNextTime'):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   429
            delattr(first, '_skipMeNextTime')
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   430
            return
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   431
        # this could all be made much quicker by putting the attributes
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   432
        # in to the flowables with a defult value of 0
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   433
        if hasattr(first,'pageBreakBefore') and first.pageBreakBefore == 1:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   434
            first._skipMeNextTime = 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   435
            first.insert(0, PageBreak())
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   436
            return
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   437
        if hasattr(first,'style') and hasattr(first.style, 'pageBreakBefore') and first.style.pageBreakBefore == 1:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   438
            first._skipMeNextTime = 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   439
            flowables.insert(0, PageBreak())
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   440
            return
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   441
        if hasattr(first,'frameBreakBefore') and first.frameBreakBefore == 1:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   442
            first._skipMeNextTime = 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   443
            flowables.insert(0, FrameBreak())
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   444
            return
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   445
        if hasattr(first,'style') and hasattr(first.style, 'frameBreakBefore') and first.style.frameBreakBefore == 1:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   446
            first._skipMeNextTime = 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   447
            flowables.insert(0, FrameBreak())
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   448
            return
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   449
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   450
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   451
    def handle_keepWithNext(self, flowables):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   452
        "implements keepWithNext"
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   453
        i = 0
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   454
        n = len(flowables)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   455
        while i<n and flowables[i].getKeepWithNext(): i = i + 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   456
        if i:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   457
            i = i + 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   458
            K = KeepTogether(flowables[:i])
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   459
            for f in K._flowables:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   460
                f.keepWithNext = 0
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   461
            del flowables[:i]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   462
            flowables.insert(0,K)
1425
fa9f74f1a701 Experiments with platypus
andy_robinson
parents: 1324
diff changeset
   463
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   464
    def handle_flowable(self,flowables):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   465
        '''try to handle one flowable from the front of list flowables.'''
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   466
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   467
        #allow document a chance to look at, modify or ignore
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   468
        #the object(s) about to be processed
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   469
        self.filterFlowables(flowables)
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   470
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   471
        self.handle_breakBefore(flowables)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   472
        self.handle_keepWithNext(flowables)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   473
        f = flowables[0]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   474
        del flowables[0]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   475
        if f is None:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   476
            return
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   477
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   478
        if isinstance(f,PageBreak):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   479
            self.handle_pageBreak()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   480
            self.afterFlowable(f)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   481
        elif isinstance(f,ActionFlowable):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   482
            f.apply(self)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   483
            self.afterFlowable(f)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   484
        else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   485
            #try to fit it then draw it
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   486
            if self.frame.add(f, self.canv, trySplit=self.allowSplitting):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   487
                self.afterFlowable(f)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   488
            else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   489
                #if isinstance(f, KeepTogether): print 'could not add it to frame'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   490
                if self.allowSplitting:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   491
                    # see if this is a splittable thing
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   492
                    S = self.frame.split(f,self.canv)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   493
                    n = len(S)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   494
                else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   495
                    n = 0
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   496
                #if isinstance(f, KeepTogether): print 'n=%d' % n
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   497
                if n:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   498
                    if self.frame.add(S[0], self.canv, trySplit=0):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   499
                        self.afterFlowable(S[0])
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   500
                    else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   501
                        print 'n = %d' % n
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   502
                        raise "LayoutError", "splitting error on page %d in\n%s" % (self.page,f.identity(30))
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   503
                    del S[0]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   504
                    for f in xrange(n-1):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   505
                        flowables.insert(f,S[f])    # put split flowables back on the list
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   506
                else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   507
                    # this must be cleared when they are finally drawn!
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   508
##                  if hasattr(f,'postponed'):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   509
                    if hasattr(f,'_postponed'):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   510
                        message = "Flowable %s too large on page %d" % (f.identity(30), self.page)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   511
                        #show us, it might be handy
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   512
                        #HACK = it seems within tables we sometimes
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   513
                        #get an empty paragraph that won't fit and this
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   514
                        #causes it to fall over.  FIXME FIXME FIXME
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   515
                        raise "LayoutError", message
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   516
##                  f.postponed = 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   517
                    f._postponed = 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   518
                    flowables.insert(0,f)           # put the flowable back
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   519
                    self.handle_frameEnd()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   520
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   521
    #these are provided so that deriving classes can refer to them
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   522
    _handle_documentBegin = handle_documentBegin
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   523
    _handle_pageBegin = handle_pageBegin
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   524
    _handle_pageEnd = handle_pageEnd
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   525
    _handle_frameBegin = handle_frameBegin
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   526
    _handle_frameEnd = handle_frameEnd
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   527
    _handle_flowable = handle_flowable
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   528
    _handle_nextPageTemplate = handle_nextPageTemplate
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   529
    _handle_currentFrame = handle_currentFrame
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   530
    _handle_nextFrame = handle_nextFrame
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   531
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   532
    def _startBuild(self, filename=None, canvasmaker=canvas.Canvas):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   533
        self._calc()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   534
        self.canv = canvasmaker(filename or self.filename,pagesize=self.pagesize)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   535
        if self._onPage:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   536
            self.canv.setPageCallBack(self._onPage)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   537
        self.handle_documentBegin()
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   538
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   539
    def _endBuild(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   540
        if self._hanging!=[] and self._hanging[-1] is PageBegin:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   541
            del self._hanging[-1]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   542
            self.clean_hanging()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   543
        else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   544
            self.clean_hanging()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   545
            self.handle_pageBreak()
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   546
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   547
        self.canv.save()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   548
        if self._onPage:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   549
            self.canv.setPageCallBack(None)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   550
        #AR - hack - for some reason a document did not
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   551
        #have these:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   552
        #if hasattr(self, 'frame'): del self.frame
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   553
        #if hasattr(self, 'pageTemplate'): del self.pageTemplate
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   554
        #del self.frame, self.pageTemplate
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   555
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   556
    def build(self, flowables, filename=None, canvasmaker=canvas.Canvas):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   557
        """Build the document from a list of flowables.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   558
           If the filename argument is provided then that filename is used
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   559
           rather than the one provided upon initialization.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   560
           If the canvasmaker argument is provided then it will be used
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   561
           instead of the default.  For example a slideshow might use
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   562
           an alternate canvas which places 6 slides on a page (by
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   563
           doing translations, scalings and redefining the page break
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   564
           operations).
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   565
        """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   566
        #assert filter(lambda x: not isinstance(x,Flowable), flowables)==[], "flowables argument error"
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   567
        self._startBuild(filename,canvasmaker)
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   568
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   569
        while len(flowables):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   570
            self.clean_hanging()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   571
            self.handle_flowable(flowables)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   572
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   573
        self._endBuild()
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   574
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   575
    def _allSatisfied(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   576
        """Called by multi-build - are all cross-references resolved?"""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   577
        allHappy = 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   578
        for f in self._indexingFlowables:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   579
            if not f.isSatisfied():
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   580
                allHappy = 0
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   581
                break
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   582
        return allHappy
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   583
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   584
    def notify(self, kind, stuff):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   585
        """"Forward to any listeners"""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   586
        for l in self._indexingFlowables:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   587
            l.notify(kind, stuff)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   588
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   589
    def pageRef(self, label):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   590
        """hook to register a page number"""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   591
        if _verbose: print "pageRef called with label '%s' on page %d" % (
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   592
            label, self.page)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   593
        self._pageRefs[label] = self.page
197
b8ca098f5ec7 Initial try at a document template class
rgbecker
parents:
diff changeset
   594
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   595
    def multiBuild(self, story,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   596
                   filename=None,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   597
                   canvasmaker=canvas.Canvas,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   598
                   maxPasses = 10):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   599
        """Makes multiple passes until all indexing flowables
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   600
        are happy."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   601
        self._indexingFlowables = []
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   602
        #scan the story and keep a copy
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   603
        for thing in story:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   604
            if thing.isIndexing():
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   605
                self._indexingFlowables.append(thing)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   606
        #print 'scanned story, found these indexing flowables:\n'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   607
        #print self._indexingFlowables
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   608
        
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   609
        passes = 0
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   610
        while 1:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   611
            passes = passes + 1
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   612
            if _verbose: print 'building pass '+str(passes) + '...',
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   613
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   614
            for fl in self._indexingFlowables:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   615
                fl.beforeBuild()
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   616
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   617
            # work with a copy of the story, since it is consumed
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   618
            tempStory = story[:]
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   619
            self.build(tempStory, filename, canvasmaker)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   620
            #self.notify('debug',None)
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   621
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   622
            #clean up so multi-build does not go wrong - the frame
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   623
            #packer might have tacked an attribute onto some
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   624
            #paragraphs
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   625
            for elem in story:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   626
##              if hasattr(elem, 'postponed'):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   627
##                  del elem.postponed
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   628
                if hasattr(elem, '_postponed'):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   629
                    del elem._postponed
218
274db2129c04 Fixes/Changes to get testplatypus to work with new framework
rgbecker
parents: 214
diff changeset
   630
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   631
            for fl in self._indexingFlowables:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   632
                fl.afterBuild()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   633
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   634
            happy = self._allSatisfied()
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   635
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   636
            if happy:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   637
                ## print 'OK'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   638
                break
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   639
            ## else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   640
                ## print 'failed'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   641
            if passes > maxPasses:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   642
                raise IndexError, "Index entries not resolved after %d passes" % maxPasses
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   643
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   644
        if _verbose: print 'saved'
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   645
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   646
    #these are pure virtuals override in derived classes
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   647
    #NB these get called at suitable places by the base class
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   648
    #so if you derive and override the handle_xxx methods
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   649
    #it's up to you to ensure that they maintain the needed consistency
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   650
    def afterInit(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   651
        """This is called after initialisation of the base class."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   652
        pass
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   653
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   654
    def beforeDocument(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   655
        """This is called before any processing is
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   656
        done on the document."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   657
        pass
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   658
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   659
    def beforePage(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   660
        """This is called at the beginning of page
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   661
        processing, and immediately before the
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   662
        beforeDrawPage method of the current page
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   663
        template."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   664
        pass
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   665
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   666
    def afterPage(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   667
        """This is called after page processing, and
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   668
        immediately after the afterDrawPage method
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   669
        of the current page template."""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   670
        pass
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   671
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   672
    def filterFlowables(self,flowables):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   673
        '''called to filter flowables at the start of the main handle_flowable method.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   674
        Upon return if flowables[0] has been set to None it is discarded and the main
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   675
        method returns.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   676
        '''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   677
        pass
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   678
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   679
    def afterFlowable(self, flowable):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   680
        '''called after a flowable has been rendered'''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   681
        pass
284
eabeb5f4e851 Added UserDocTemplate class, and paragraph.getPlainText()
andy_robinson
parents: 279
diff changeset
   682
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
   683
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   684
class SimpleDocTemplate(BaseDocTemplate):
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   685
    """A special case document template that will handle many simple documents.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   686
       See documentation for BaseDocTemplate.  No pageTemplates are required
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   687
       for this special case.   A page templates are inferred from the
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   688
       margin information and the onFirstPage, onLaterPages arguments to the build method.
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   689
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   690
       A document which has all pages with the same look except for the first
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   691
       page may can be built using this special approach.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   692
    """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   693
    _invalidInitArgs = ('pageTemplates',)
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
   694
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   695
    def handle_pageBegin(self):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   696
        '''override base method to add a change of page template after the firstpage.
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   697
        '''
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   698
        self._handle_pageBegin()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   699
        self._handle_nextPageTemplate('Later')
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   700
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   701
    def build(self,flowables,onFirstPage=_doNothing, onLaterPages=_doNothing):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   702
        """build the document using the flowables.  Annotate the first page using the onFirstPage
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   703
               function and later pages using the onLaterPages function.  The onXXX pages should follow
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   704
               the signature
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   705
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   706
                  def myOnFirstPage(canvas, document):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   707
                      # do annotations and modify the document
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   708
                      ...
1428
13a13044e9a8 Fixed up keepWithNext so it works
rgbecker
parents: 1425
diff changeset
   709
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   710
               The functions can do things like draw logos, page numbers,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   711
               footers, etcetera. They can use external variables to vary
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   712
               the look (for example providing page numbering or section names).
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   713
        """
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   714
        self._calc()    #in case we changed margins sizes etc
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   715
        frameT = Frame(self.leftMargin, self.bottomMargin, self.width, self.height, id='normal')
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   716
        self.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=onFirstPage,pagesize=self.pagesize),
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   717
                        PageTemplate(id='Later',frames=frameT, onPage=onLaterPages,pagesize=self.pagesize)])
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   718
        if onFirstPage is _doNothing and hasattr(self,'onFirstPage'):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   719
            self.pageTemplates[0].beforeDrawPage = self.onFirstPage
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   720
        if onLaterPages is _doNothing and hasattr(self,'onLaterPages'):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   721
            self.pageTemplates[1].beforeDrawPage = self.onLaterPages
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   722
        BaseDocTemplate.build(self,flowables)
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   723
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
   724
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   725
    ##########################################################
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   726
    ##
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   727
    ##   testing
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   728
    ##
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   729
    ##########################################################
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   730
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   731
def randomText():
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   732
    #this may or may not be appropriate in your company
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   733
    from random import randint, choice
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   734
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   735
    RANDOMWORDS = ['strategic','direction','proactive',
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   736
    'reengineering','forecast','resources',
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   737
    'forward-thinking','profit','growth','doubletalk',
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   738
    'venture capital','IPO']
512
c12ae96634d5 Added working table of contents framework
andy_robinson
parents: 500
diff changeset
   739
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   740
    sentences = 5
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   741
    output = ""
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   742
    for sentenceno in range(randint(1,5)):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   743
        output = output + 'Blah'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   744
        for wordno in range(randint(10,25)):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   745
            if randint(0,4)==0:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   746
                word = choice(RANDOMWORDS)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   747
            else:
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   748
                word = 'blah'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   749
            output = output + ' ' +word
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   750
        output = output+'.'
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   751
    return output
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   752
565
179927300074 Minor changes.
dinu_gherman
parents: 551
diff changeset
   753
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   754
if __name__ == '__main__':
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   755
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   756
    def myFirstPage(canvas, doc):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   757
        canvas.saveState()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   758
        canvas.setStrokeColor(red)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   759
        canvas.setLineWidth(5)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   760
        canvas.line(66,72,66,PAGE_HEIGHT-72)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   761
        canvas.setFont('Times-Bold',24)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   762
        canvas.drawString(108, PAGE_HEIGHT-108, "TABLE OF CONTENTS DEMO")
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   763
        canvas.setFont('Times-Roman',12)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   764
        canvas.drawString(4 * inch, 0.75 * inch, "First Page")
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   765
        canvas.restoreState()
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   766
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   767
    def myLaterPages(canvas, doc):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   768
        canvas.saveState()
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   769
        canvas.setStrokeColor(red)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   770
        canvas.setLineWidth(5)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   771
        canvas.line(66,72,66,PAGE_HEIGHT-72)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   772
        canvas.setFont('Times-Roman',12)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   773
        canvas.drawString(4 * inch, 0.75 * inch, "Page %d" % doc.page)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   774
        canvas.restoreState()
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   775
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   776
    def run():
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   777
        objects_to_draw = []
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   778
        from reportlab.lib.styles import ParagraphStyle
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   779
        #from paragraph import Paragraph
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   780
        from doctemplate import SimpleDocTemplate
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   781
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   782
        #need a style
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   783
        normal = ParagraphStyle('normal')
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   784
        normal.firstLineIndent = 18
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   785
        normal.spaceBefore = 6
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   786
        import random
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   787
        for i in range(15):
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   788
            height = 0.5 + (2*random.random())
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   789
            box = XBox(6 * inch, height * inch, 'Box Number %d' % i)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   790
            objects_to_draw.append(box)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   791
            para = Paragraph(randomText(), normal)
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   792
            objects_to_draw.append(para)
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   793
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   794
        SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw,
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   795
            onFirstPage=myFirstPage,onLaterPages=myLaterPages)
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   796
1502
125b52eb0a8e Added callbacks throughout
andy_robinson
parents: 1440
diff changeset
   797
    run()
221
3d71b66b14c6 Changes related to removal of SimpleFlowDocument
rgbecker
parents: 218
diff changeset
   798