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