docs/userguide/platdemos.py
author andy_robinson
Tue, 04 Jul 2000 22:22:40 +0000
changeset 320 7a4ca2b5bb89
parent 256 0dedfa161b21
child 336 c0fcae4341cf
permissions -rw-r--r--
Tidied up genuserguide.py, especially figure handling; began adding PDF special features docco
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
256
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
     1
#platdemos.py
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
     2
"""This includes some demos of platypus for use in the API proposal"""
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
     3
from reportlab.lib import colors
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
     4
from reportlab.pdfgen.canvas import Canvas
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
     5
from reportlab.lib.styles import ParagraphStyle
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
     6
from reportlab.platypus import Frame
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
     7
from reportlab.platypus import Flowable
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
     8
from reportlab.platypus import Paragraph
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
     9
from reportlab.lib.units import inch
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    10
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    11
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    12
captionStyle = ParagraphStyle('Caption',
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    13
                       fontName='Times-Italic',
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    14
                       fontSize=10,
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    15
                       alignment=TA_CENTER)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    16
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    17
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    18
class Figure(Flowable):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    19
    def __init__(self, width, height):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    20
        Flowable.__init__(self)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    21
        self.width = width
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    22
        self.figureHeight = height
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    23
        self.captionHeight = 0  # work out later
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    24
        self.caption = None
320
7a4ca2b5bb89 Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents: 256
diff changeset
    25
        self.captionStyle = ParagraphStyle('Caption',
7a4ca2b5bb89 Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents: 256
diff changeset
    26
                       fontName='Times-Italic',
7a4ca2b5bb89 Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents: 256
diff changeset
    27
                       fontSize=12,
7a4ca2b5bb89 Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents: 256
diff changeset
    28
                       spaceBefore=6,
7a4ca2b5bb89 Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents: 256
diff changeset
    29
                       alignment=TA_CENTER)
256
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    30
        self.spaceBefore = 12
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    31
        self.spaceAfter = 12
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    32
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    33
    def wrap(self, availWidth, availHeight):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    34
        # try to get the caption aligned
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    35
        self.captionPara = Paragraph(self.caption, self.captionStyle)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    36
        (w, h) = self.captionPara.wrap(self.width, availHeight - self.figureHeight)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    37
        self.captionHeight = h
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    38
        self.height = self.captionHeight + self.figureHeight
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    39
        self.dx = 0.5 * (availWidth - self.width)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    40
        return (self.width, self.height)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    41
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    42
    def draw(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    43
        self.canv.translate(self.dx, 0)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    44
        self.drawCaption()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    45
        self.canv.translate(0, self.captionHeight)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    46
        self.drawBorder()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    47
        self.drawFigure()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    48
        
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    49
    def drawBorder(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    50
        self.canv.rect(0, 0, self.width, self.figureHeight)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    51
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    52
    def drawCaption(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    53
        self.captionPara.drawOn(self.canv, 0, 0)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    54
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    55
    def drawFigure(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    56
        pass
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    57
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    58
def drawPage(canvas,x, y, width, height):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    59
    #draws something which looks like a page
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    60
    pth = canvas.beginPath()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    61
    corner = 0.05*width
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    62
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    63
    # shaded backdrop offset a little    
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    64
    canvas.setFillColorRGB(0.5,0.5,0.5)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    65
    canvas.rect(x + corner, y - corner, width, height, stroke=0, fill=1)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    66
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    67
    #'sheet of paper' in light yellow
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    68
    canvas.setFillColorRGB(1,1,0.9)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    69
    canvas.setLineWidth(0)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    70
    canvas.rect(x, y, width, height, stroke=1, fill=1)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    71
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    72
    #reset    
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    73
    canvas.setFillColorRGB(0,0,0)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    74
    canvas.setStrokeColorRGB(0,0,0)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    75
    
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    76
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    77
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    78
class PageFigure(Figure):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    79
    """Shows a blank page in a frame, and draws on that.  Used in
320
7a4ca2b5bb89 Tidied up genuserguide.py, especially figure handling; began
andy_robinson
parents: 256
diff changeset
    80
    illustrations of how PLATYPUS works."""
256
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    81
    def __init__(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    82
        Figure.__init__(self, 3*inch, 3*inch)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    83
        self.caption = 'Figure 1 - a blank page'
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    84
        self.captionStyle = captionStyle
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    85
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    86
    def drawVirtualPage(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    87
        pass
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    88
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    89
    def drawFigure(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    90
        drawPage(self.canv, 0.625*inch, 0.25*inch, 1.75*inch, 2.5*inch)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    91
        self.canv.translate(0.625*inch, 0.25*inch)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    92
        self.canv.scale(1.75/8.27, 2.5/11.69)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    93
        self.drawVirtualPage()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    94
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    95
class PlatPropFigure1(PageFigure):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    96
    """This shows a page with a frame on it"""
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    97
    def __init__(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    98
        PageFigure.__init__(self)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    99
        self.caption = "Figure 1 - a page with a simple frame"
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   100
    def drawVirtualPage(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   101
        demo1(self.canv)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   102
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   103
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   104
def demo1(canvas):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   105
    frame = Frame(
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   106
                    2*inch,     # x
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   107
                    4*inch,     # y at bottom
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   108
                    4*inch,     # width
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   109
                    5*inch,     # height
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   110
                    showBoundary = 1  # helps us see what's going on
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   111
                    )
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   112
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   113
    bodyStyle = ParagraphStyle('Body',
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   114
                       fontName='Times-Roman',
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   115
                       fontSize=24,
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   116
                       leading=28,
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   117
                       spaceBefore=6)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   118
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   119
    para1 = Paragraph('Spam spam spam spam. ' * 5, bodyStyle)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   120
    para2 = Paragraph('Eggs eggs eggs. ' * 5, bodyStyle)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   121
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   122
    mydata = [para1, para2]
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   123
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   124
    #this does the packing and drawing.  The frame will consume
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   125
    #items from the front of the list as it prints them
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   126
    frame.addFromList(mydata,canvas)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   127
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   128
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   129
def test1():
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   130
    c  = Canvas('platdemos.pdf')
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   131
    f = Frame(inch, inch, 6*inch, 9*inch, showBoundary=1)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   132
    v = PlatPropFigure1()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   133
    f.addFromList([v],c)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   134
    c.save()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   135
    
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   136
if __name__ == '__main__':
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   137
    test1()