reportlab/pdfgen/pycanvas.py
author andy_robinson
Wed, 02 Oct 2002 13:23:49 +0000
changeset 1740 71f51fea9301
parent 1739 9ee6c04932fa
child 1742 1dac293eabff
permissions -rw-r--r--
Latest, well commented source
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
     1
# a Pythonesque Canvas v0.5
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     2
# Author : Jerome Alet - <alet@librelogiciel.com>
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     3
# License : ReportLab's license
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     4
#
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     5
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     6
__doc__ = """pycanvas.Canvas : a Canvas class which can also output Python source code.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     7
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     8
pycanvas.Canvas class works exactly like canvas.Canvas, but you can call str() on
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
     9
pycanvas.Canvas instances. Doing so will return the Python source code equivalent
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    10
to your own program, which would, when run, produce the same PDF document as
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    11
your original program.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    12
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    13
Generated Python source code defines a doIt() function which accepts a filename
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    14
or file-like object as its single parameter. The doIt() function will generate
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    15
a PDF document and save it in the file you specified in this argument, and will
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    16
also return you the Generated Python source code, which you can run again
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    17
to produce the very same PDF document and the Python source code, which...
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    18
ad nauseam !
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    19
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    20
the reportlab/test/test_pdfgen_pycanvas.py program is the test suite for
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    21
pycanvas, you can do the following to run it :
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    22
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    23
    First set verbose=1 in reportlab/rl_config.py
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    24
    
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    25
    then from the command interpreter :
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    26
    
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    27
    $ cd reportlab/test
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    28
    $ python test_pdfgen_pycanvas.py >n1.py
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    29
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    30
    this will produce both n1.py and test_pdfgen_pycanvas.pdf
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    31
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    32
    then :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    33
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    34
    $ python n1.py n1.pdf >n2.py
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    35
    $ python n2.py n2.pdf >n3.py
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    36
    $ ...
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    37
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    38
    n1.py, n2.py, n3.py and so on will be identical files.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    39
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    40
    n1.pdf, n2.pdf, n3.pdf and so on will be PDF files
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    41
    similar to test_pdfgen_pycanvas.pdf.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    42
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    43
Alternatively you can import n1.py (or n3.py, or n16384.py if you prefer)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    44
in your own program, and then call its doIt function :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    45
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    46
    import n1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    47
    pythonsource = n1.doIt("myfile.pdf")
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    48
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    49
Why would you want to use such a beast ?
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    50
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    51
    - To linearize a program : optimizing some complex parts for example.
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    52
    
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    53
    - To debug : reading the generated Python source code may help you or
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    54
      the ReportLab team to diagnose problems. The generated code is now
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    55
      clearly commented and shows nesting levels, page numbers, and so
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    56
      on. You can use the generated script when asking for support : we
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    57
      can see the results you obtain without needing your datas or complete
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    58
      application.
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    59
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    60
    - To create standalone scripts : say your program uses a high level
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    61
      environment to generate its output (databases, RML, etc...), using 
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    62
      this class would give you an equivalent program but with complete 
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    63
      independance from the high level environment (e.g. if you don't 
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    64
      have Oracle).
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    65
    
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    66
    - To contribute some nice looking PDF documents to the ReportLab website
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    67
      without having to send a complete application you don't want to
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    68
      distribute.
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    69
      
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    70
    - ... Insert your own ideas here ...
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    71
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    72
    - For fun because you can do it !
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    73
"""
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    74
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    75
import cStringIO
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    76
from reportlab.pdfgen import canvas
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    77
from reportlab.pdfgen import pathobject
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    78
from reportlab.pdfgen import textobject
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    79
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    80
PyHeader = '''#! /usr/bin/env python
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    81
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    82
import sys
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    83
from reportlab.pdfgen import pycanvas
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    84
from reportlab.pdfgen import pathobject
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    85
from reportlab.pdfgen import textobject
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    86
from reportlab.lib.colors import Color
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    87
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    88
def doIt(file) :
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    89
    """Generates a PDF document, save it into file, and returns Python source code.
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    90
    
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    91
       file : either a filename or a file-like object.
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    92
       
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    93
       returns : the equivalent Python source code as a string of text.
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    94
    """'''
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    95
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    96
PyFooter = '''
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    97
    # sends back the equivalent Python source code
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
    98
    return str(c)
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
    99
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   100
if __name__ == "__main__" :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   101
    if len(sys.argv) != 2 :
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   102
        # second argument must be the name of the PDF file to create
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   103
        sys.stderr.write("%s needs one and only one argument\\n" % sys.argv[0])
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   104
        sys.exit(-1)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   105
    else :
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   106
        # we've got a filename, we can proceed.
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   107
        print doIt(sys.argv[1])    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   108
        sys.exit(0)
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   109
'''
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   110
    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   111
def buildargs(*args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   112
    arguments = ""
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   113
    for arg in args :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   114
        arguments += "%s, " % repr(arg)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   115
    for (kw, val) in kwargs.items() :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   116
        arguments += "%s=%s, " % (kw, repr(val))
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   117
    if arguments[-2:] == ", " :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   118
        arguments = arguments[:-2]
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   119
    return arguments    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   120
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   121
# global nesting level
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   122
_in = 0
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   123
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   124
class PDFAction :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   125
    def __init__(self, parent, action) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   126
        self._parent = parent
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   127
        self._action = action
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   128
    
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   129
    def __getattr__(self, name) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   130
        return getattr(getattr(self._parent._object, self._action), name)
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   131
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   132
    def __call__(self, *args, **kwargs) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   133
        global _in
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   134
        if not _in :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   135
            self._parent._parent._PyWrite("    %s.%s(%s)" % (self._parent._name, self._action, apply(buildargs, args, kwargs)))
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   136
        _in += 1
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   137
        retcode = apply(getattr(self._parent._object, self._action), args, kwargs)
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   138
        _in -= 1
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   139
        return retcode
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   140
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   141
class PDFObject :
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   142
    _number = 0
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   143
    def __init__(self, parent) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   144
        self._parent = parent
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   145
        self._initdone = 0
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   146
    
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   147
    def __getattr__(self, name) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   148
        return PDFAction(self, name)
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   149
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   150
    def __repr__(self) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   151
        return self._name
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   152
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   153
    def __call__(self, *args, **kwargs) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   154
        if not self._initdone :
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   155
            self.__class__._number += 1
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   156
            methodname = apply(self._postinit, args, kwargs)
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   157
            self._parent._PyWrite("\n    # create PDF%sObject number %i\n    %s = %s.%s(%s)" % (methodname[5:], self.__class__._number, self._name, self._parent._name, methodname, apply(buildargs, args, kwargs)))
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   158
            self._initdone = 1
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   159
        return self
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   160
    
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   161
class Canvas :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   162
    class TextObject(PDFObject) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   163
        _name = "t"
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   164
        def _postinit(self, *args, **kwargs) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   165
            self._object = apply(textobject.PDFTextObject, (self._parent, ) + args, kwargs)
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   166
            return "beginText"
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   167
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   168
    class PathObject(PDFObject) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   169
        _name = "p"
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   170
        def _postinit(self, *args, **kwargs) :
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   171
            self._object = apply(pathobject.PDFPathObject, args, kwargs)
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   172
            return "beginPath"
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   173
        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   174
    class Action(PDFAction) :
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   175
        def __call__(self, *args, **kwargs) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   176
            global _in
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   177
            try :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   178
                if (not _in) and (self._action != "__nonzero__") :
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   179
                    if self._action == "showPage" :
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   180
                        self._parent._PyWrite("\n    # Ends page %i" % self._parent._pagenumber)
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   181
                    elif self._action == "saveState" :    
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   182
                        state = {}
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   183
                        d = self._parent._object.__dict__
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   184
                        for name in self._parent._object.STATE_ATTRIBUTES:
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   185
                            state[name] = d[name] 
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   186
                        self._parent._PyWrite("\n    # Saves context level %i %s" % (self._parent._contextlevel, state))
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   187
                        self._parent._contextlevel += 1
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   188
                    elif self._action == "restoreState" :    
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   189
                        self._parent._contextlevel -= 1
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   190
                        self._parent._PyWrite("\n    # Restores context level %i %s" % (self._parent._contextlevel, self._parent._object.state_stack[-1]))
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   191
                    elif self._action == "beginForm" :    
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   192
                        self._parent._formnumber += 1
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   193
                        self._parent._PyWrite("\n    # Begins form %i" % self._parent._formnumber)
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   194
                    elif self._action == "endForm" :    
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   195
                        self._parent._PyWrite("\n    # Ends form %i" % self._parent._formnumber)
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   196
                    elif self._action == "save" :    
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   197
                        self._parent._PyWrite("\n    # Saves the PDF document to disk")
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   198
                        
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   199
                    self._parent._PyWrite("    %s.%s(%s)" % (self._parent._name, self._action, apply(buildargs, args, kwargs)))
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   200
                    
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   201
                    if self._action == "showPage" :
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   202
                        self._parent._pagenumber += 1
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   203
                        self._parent._PyWrite("\n    # Begins page %i" % self._parent._pagenumber)
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   204
                    elif self._action == "endForm" :    
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   205
                        self._parent._PyWrite("")
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   206
                _in += 1    
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   207
                retcode = apply(getattr(self._parent._object, self._action), args, kwargs)
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   208
                _in -= 1    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   209
                return retcode
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   210
            except AttributeError :    # __nonzero__, but I don't know why
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   211
                _in -= 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   212
                return 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   213
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   214
    _name = "c"
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   215
    def __init__(self, *args, **kwargs) :
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   216
        self._contextlevel = 0
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   217
        self._pagenumber = 1
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   218
        self._formnumber = 0
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   219
        self._footerpresent = 0
1739
9ee6c04932fa Jerome's latest version, refactored he says
andy_robinson
parents: 1738
diff changeset
   220
        self._object = apply(canvas.Canvas, args, kwargs)
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   221
        self._pyfile = cStringIO.StringIO()
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   222
        self._PyWrite(PyHeader)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   223
        try :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   224
            del kwargs["filename"]
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   225
        except KeyError :    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   226
            pass
1740
71f51fea9301 Latest, well commented source
andy_robinson
parents: 1739
diff changeset
   227
        self._PyWrite("    # create the PDF document\n    %s = pycanvas.Canvas(file, %s)\n\n    # Begins page 1" % (self._name, apply(buildargs, args[1:], kwargs)))
1738
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   228
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   229
    def __str__(self) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   230
        if not self._footerpresent :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   231
            self._PyWrite(PyFooter)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   232
            self._footerpresent = 1
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   233
        return self._pyfile.getvalue()
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   234
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   235
    def __getattr__(self, name) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   236
        if name == "beginPath" :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   237
            return self.PathObject(self)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   238
        elif name == "beginText" :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   239
            return self.TextObject(self)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   240
        else :    
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   241
            return self.Action(self, name)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   242
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   243
    def _PyWrite(self, pycode) :
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   244
        self._pyfile.write("%s\n" % pycode)
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   245
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   246
if __name__ == '__main__':
64f2ccdb91b9 Jerome Alet's Macro-recording canvas
andy_robinson
parents:
diff changeset
   247
    print 'For test scripts, look in reportlab/test'