docs/userguide/platdemos.py
author andy_robinson
Mon, 05 Jun 2000 16:38:53 +0000
changeset 256 0dedfa161b21
child 320 7a4ca2b5bb89
permissions -rw-r--r--
Initial revision
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
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    25
        self.captionStyle = ParagraphStyle('Default')
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    26
        self.spaceBefore = 12
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    27
        self.spaceAfter = 12
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    28
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    29
    def wrap(self, availWidth, availHeight):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    30
        # try to get the caption aligned
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    31
        self.captionPara = Paragraph(self.caption, self.captionStyle)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    32
        (w, h) = self.captionPara.wrap(self.width, availHeight - self.figureHeight)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    33
        self.captionHeight = h
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    34
        self.height = self.captionHeight + self.figureHeight
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    35
        self.dx = 0.5 * (availWidth - self.width)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    36
        return (self.width, self.height)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    37
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    38
    def draw(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    39
        self.canv.translate(self.dx, 0)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    40
        self.drawCaption()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    41
        self.canv.translate(0, self.captionHeight)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    42
        self.drawBorder()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    43
        self.drawFigure()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    44
        
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    45
    def drawBorder(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    46
        self.canv.rect(0, 0, self.width, self.figureHeight)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    47
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    48
    def drawCaption(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    49
        self.captionPara.drawOn(self.canv, 0, 0)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    50
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    51
    def drawFigure(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    52
        pass
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    53
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    54
def drawPage(canvas,x, y, width, height):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    55
    #draws something which looks like a page
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    56
    pth = canvas.beginPath()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    57
    corner = 0.05*width
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    58
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    59
    # shaded backdrop offset a little    
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    60
    canvas.setFillColorRGB(0.5,0.5,0.5)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    61
    canvas.rect(x + corner, y - corner, width, height, stroke=0, fill=1)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    62
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    63
    #'sheet of paper' in light yellow
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    64
    canvas.setFillColorRGB(1,1,0.9)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    65
    canvas.setLineWidth(0)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    66
    canvas.rect(x, y, width, height, stroke=1, fill=1)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    67
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    68
    #reset    
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    69
    canvas.setFillColorRGB(0,0,0)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    70
    canvas.setStrokeColorRGB(0,0,0)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    71
    
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    72
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    73
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    74
class PageFigure(Figure):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    75
    """Shows a blank page in a frame, and draws on that.  Used in
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    76
    illustrations."""
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    77
    def __init__(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    78
        Figure.__init__(self, 3*inch, 3*inch)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    79
        self.caption = 'Figure 1 - a blank page'
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    80
        self.captionStyle = captionStyle
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    81
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    82
    def drawVirtualPage(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    83
        pass
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    84
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    85
    def drawFigure(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    86
        drawPage(self.canv, 0.625*inch, 0.25*inch, 1.75*inch, 2.5*inch)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    87
        self.canv.translate(0.625*inch, 0.25*inch)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    88
        self.canv.scale(1.75/8.27, 2.5/11.69)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    89
        self.drawVirtualPage()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    90
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    91
class PlatPropFigure1(PageFigure):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    92
    """This shows a page with a frame on it"""
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    93
    def __init__(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    94
        PageFigure.__init__(self)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    95
        self.caption = "Figure 1 - a page with a simple frame"
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    96
    def drawVirtualPage(self):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    97
        demo1(self.canv)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    98
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
    99
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   100
def demo1(canvas):
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   101
    frame = Frame(
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   102
                    2*inch,     # x
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   103
                    4*inch,     # y at bottom
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   104
                    4*inch,     # width
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   105
                    5*inch,     # height
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   106
                    showBoundary = 1  # helps us see what's going on
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   107
                    )
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   108
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   109
    bodyStyle = ParagraphStyle('Body',
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   110
                       fontName='Times-Roman',
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   111
                       fontSize=24,
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   112
                       leading=28,
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   113
                       spaceBefore=6)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   114
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   115
    para1 = Paragraph('Spam spam spam spam. ' * 5, bodyStyle)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   116
    para2 = Paragraph('Eggs eggs eggs. ' * 5, bodyStyle)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   117
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   118
    mydata = [para1, para2]
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   119
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   120
    #this does the packing and drawing.  The frame will consume
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   121
    #items from the front of the list as it prints them
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   122
    frame.addFromList(mydata,canvas)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   123
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   124
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   125
def test1():
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   126
    c  = Canvas('platdemos.pdf')
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   127
    f = Frame(inch, inch, 6*inch, 9*inch, showBoundary=1)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   128
    v = PlatPropFigure1()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   129
    f.addFromList([v],c)
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   130
    c.save()
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   131
    
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   132
if __name__ == '__main__':
0dedfa161b21 Initial revision
andy_robinson
parents:
diff changeset
   133
    test1()