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