reportlab/platypus/figures.py
author andy_robinson
Fri, 28 May 2004 23:41:18 +0000
changeset 2311 b594c6c22203
parent 2200 be0cfccc662a
child 2332 2a7ab4405e18
permissions -rw-r--r--
Prelim support for unicode conversion (disabled by rl_config.autoConvertEncodings at first); borders to drawings in pythonpoint and for figure objects;
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
2311
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/platypus/figures.py,v 1.18 2004/05/28 23:41:18 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"""
2311
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
     6
__version__=''' $Id: figures.py,v 1.18 2004/05/28 23:41:18 andy_robinson Exp $ '''
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
     7
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
     8
import os
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     9
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    10
from reportlab.lib import colors
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    11
from reportlab.pdfgen.canvas import Canvas
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    12
from reportlab.lib.styles import ParagraphStyle
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
    13
from reportlab.lib.utils import recursiveImport
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    14
from reportlab.platypus import Frame
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    15
from reportlab.platypus import Flowable
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    16
from reportlab.platypus import Paragraph
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    17
from reportlab.lib.units import inch
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    18
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    19
from reportlab.lib.validators import isColor
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    20
from reportlab.lib.colors import toColor
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    21
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    22
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
    23
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    24
class Figure(Flowable):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    25
    def __init__(self, width, height, caption="",
2311
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
    26
                 captionFont="Times-Italic", captionSize=12,
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
    27
                 background=None,
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    28
                 captionTextColor=toColor('black'),
2311
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
    29
                 captionBackColor=None,
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
    30
                 border=1):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    31
        Flowable.__init__(self)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    32
        self.width = width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    33
        self.figureHeight = height
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    34
        self.caption = caption
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    35
        self.captionFont = captionFont
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    36
        self.captionSize = captionSize
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    37
        self.captionTextColor = captionTextColor
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    38
        self.captionBackColor = captionBackColor
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    39
        self._captionData = None
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    40
        self.captionHeight = 0  # work out later
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    41
        self.background = background
2311
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
    42
        self.border = border
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    43
        self.spaceBefore = 12
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    44
        self.spaceAfter = 12
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    45
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    46
    def _getCaptionPara(self):
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    47
        caption = self.caption
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    48
        captionFont = self.captionFont
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    49
        captionSize = self.captionSize
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    50
        captionTextColor = self.captionTextColor
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    51
        captionBackColor = self.captionBackColor
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    52
        if self._captionData!=(caption,captionFont,captionSize,captionTextColor,captionBackColor):
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    53
            self._captionData = (caption,captionFont,captionSize,captionTextColor,captionBackColor)
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    54
            self.captionStyle = ParagraphStyle(
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    55
                'Caption',
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    56
                fontName=captionFont,
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    57
                fontSize=captionSize,
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    58
                leading=1.2*captionSize,
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    59
                textColor = captionTextColor,
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    60
                backColor = captionBackColor,
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    61
                spaceBefore=captionSize * 0.5,
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    62
                alignment=TA_CENTER)
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    63
            #must build paragraph now to get sequencing in synch with rest of story
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    64
            self.captionPara = Paragraph(self.caption, self.captionStyle)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    65
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    66
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    67
        # try to get the caption aligned
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    68
        if self.caption:
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    69
            self._getCaptionPara()
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    70
            (w, h) = self.captionPara.wrap(self.width, availHeight - self.figureHeight)
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    71
            self.captionHeight = h
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    72
            self.height = self.captionHeight + self.figureHeight
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    73
            if w>self.width: self.width = w
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    74
        else:
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    75
            self.height = self.figureHeight
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    76
        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
    77
        return (self.width, self.height)
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
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    80
        self.canv.translate(self.dx, 0)
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    81
        if self.caption:
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    82
            self._getCaptionPara()
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    83
            self.drawCaption()
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    84
            self.canv.translate(0, self.captionHeight)
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    85
        if self.background:
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    86
            self.drawBackground()
2311
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
    87
        if self.border:
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
    88
            self.drawBorder()
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    89
        self.drawFigure()
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    90
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    91
    def drawBorder(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    92
        self.canv.rect(0, 0, self.width, self.figureHeight)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    93
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    94
    def _doBackground(self, color):
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    95
        self.canv.saveState()
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    96
        self.canv.setFillColor(self.background)
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    97
        self.canv.rect(0, 0, self.width, self.figureHeight, fill=1)
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    98
        self.canv.restoreState()
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
    99
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   100
    def drawBackground(self):
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   101
        """For use when using a figure on a differently coloured background.
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   102
        Allows you to specify a colour to be used as a background for the figure."""
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   103
        if isColor(self.background):
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   104
            self._doBackground(self.background)
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   105
        else:
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   106
            try:
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   107
                c = toColor(self.background)
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   108
                self._doBackground(c)
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   109
            except:
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   110
                pass
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   111
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   112
    def drawCaption(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   113
        self.captionPara.drawOn(self.canv, 0, 0)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   114
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   115
    def drawFigure(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   116
        pass
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   117
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   118
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
   119
    #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
   120
    pth = canvas.beginPath()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   121
    corner = 0.05*width
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   122
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   123
    # shaded backdrop offset a little
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   124
    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
   125
    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
   126
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   127
    #'sheet of paper' in light yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   128
    canvas.setFillColorRGB(1,1,0.9)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   129
    canvas.setLineWidth(0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   130
    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
   131
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   132
    #reset
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   133
    canvas.setFillColorRGB(0,0,0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   134
    canvas.setStrokeColorRGB(0,0,0)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   135
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   136
class PageFigure(Figure):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   137
    """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
   138
    illustrations of how PLATYPUS works."""
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   139
    def __init__(self, background=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   140
        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
   141
        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
   142
        self.captionStyle = captionStyle
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   143
        self.background = background
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   144
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   145
    def drawVirtualPage(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   146
        pass
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   147
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   148
    def drawFigure(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   149
        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
   150
        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
   151
        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
   152
        self.drawVirtualPage()
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   153
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   154
class PlatPropFigure1(PageFigure):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   155
    """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
   156
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   157
        PageFigure.__init__(self)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   158
        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
   159
    def drawVirtualPage(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   160
        demo1(self.canv)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   161
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   162
class FlexFigure(Figure):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   163
    """Base for a figure class with a caption. Can grow or shrink in proportion"""
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   164
    def __init__(self, width, height, caption, background=None):
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   165
        Figure.__init__(self, width, height, caption,
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   166
                        captionFont="Helvetica-Oblique", captionSize=8,
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   167
                        background=None)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   168
        self.shrinkToFit = 1 #if set and wrap is too tight, shrinks
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   169
        self.growToFit = 1 #if set and wrap is too tight, shrinks
2175
8570f75b54d7 I can only get gensampler to work with these changes
rgbecker
parents: 2170
diff changeset
   170
        self.scaleFactor = None
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   171
        self.captionStyle = ParagraphStyle(
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   172
            'Caption',
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   173
            fontName='Times', #'Helvetica-Oblique',
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2195
diff changeset
   174
            fontSize=4, #8,
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   175
            spaceBefore=9, #3,
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   176
            alignment=TA_CENTER
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   177
            )
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   178
        self._scaleFactor = None
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   179
        self.background = background
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   180
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   181
    def _scale(self,availWidth,availHeight):
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   182
        "Rescale to fit according to the rules, but only once"
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   183
        if self._scaleFactor is None or self.width>availWidth or self.height>availHeight:
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   184
            w, h = Figure.wrap(self, availWidth, availHeight)
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   185
            captionHeight = h - self.figureHeight
2167
4ddf1ef71640 New pagecatcher and drawing tags (experimental)
andy_robinson
parents: 2164
diff changeset
   186
            if self.scaleFactor is None:
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   187
                #scale factor None means auto
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   188
                self._scaleFactor = min(availWidth/self.width,(availHeight-captionHeight)/self.figureHeight)
2167
4ddf1ef71640 New pagecatcher and drawing tags (experimental)
andy_robinson
parents: 2164
diff changeset
   189
            else: #they provided a factor
4ddf1ef71640 New pagecatcher and drawing tags (experimental)
andy_robinson
parents: 2164
diff changeset
   190
                self._scaleFactor = self.scaleFactor
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   191
            if self._scaleFactor<1 and self.shrinkToFit:
2175
8570f75b54d7 I can only get gensampler to work with these changes
rgbecker
parents: 2170
diff changeset
   192
                self.width = self.width * self._scaleFactor - 0.0001
2167
4ddf1ef71640 New pagecatcher and drawing tags (experimental)
andy_robinson
parents: 2164
diff changeset
   193
                self.figureHeight = self.figureHeight * self._scaleFactor
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   194
            elif self._scaleFactor>1 and self.growToFit:
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   195
                self.width = self.width*self._scaleFactor - 0.0001
2167
4ddf1ef71640 New pagecatcher and drawing tags (experimental)
andy_robinson
parents: 2164
diff changeset
   196
                self.figureHeight = self.figureHeight * self._scaleFactor
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   197
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   198
    def wrap(self, availWidth, availHeight):
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   199
        self._scale(availWidth,availHeight)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   200
        return Figure.wrap(self, availWidth, availHeight)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   201
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   202
    def split(self, availWidth, availHeight):
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   203
        self._scale(availWidth,availHeight)
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   204
        return Figure.split(self, availWidth, availHeight)
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   205
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   206
class ImageFigure(FlexFigure):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   207
    """Image with a caption below it"""
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   208
    def __init__(self, filename, caption, background=None):
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   209
        assert os.path.isfile(filename), 'image file %s not found' % filename
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   210
        from reportlab.lib.utils import ImageReader
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   211
        w, h = ImageReader(filename).getSize()
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   212
        self.filename = filename
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   213
        FlexFigure.__init__(self, w, h, caption, background)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   214
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   215
    def drawFigure(self):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   216
        self.canv.drawInlineImage(self.filename,
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   217
                                  0, 0,self.width, self.figureHeight)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2195
diff changeset
   218
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   219
class DrawingFigure(FlexFigure):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   220
    """Drawing with a caption below it.  Clunky, scaling fails."""
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   221
    def __init__(self, modulename, classname, caption, baseDir=None, background=None):
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   222
        module = recursiveImport(modulename, baseDir)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   223
        klass = getattr(module, classname)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   224
        self.drawing = klass()
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   225
        FlexFigure.__init__(self,
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   226
                            self.drawing.width,
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   227
                            self.drawing.height,
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   228
                            caption,
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   229
                            background)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   230
        self.growToFit = 1
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2195
diff changeset
   231
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   232
    def drawFigure(self):
2175
8570f75b54d7 I can only get gensampler to work with these changes
rgbecker
parents: 2170
diff changeset
   233
        self.canv.scale(self._scaleFactor, self._scaleFactor)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   234
        self.drawing.drawOn(self.canv, 0, 0)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   235
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   236
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   237
try:
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   238
    from rlextra.pageCatcher.pageCatcher import restoreForms, storeForms, storeFormsInMemory, restoreFormsInMemory
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   239
    _hasPageCatcher = 1
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   240
except ImportError:
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   241
    _hasPageCatcher = 0
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   242
if _hasPageCatcher:
2158
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   243
    ####################################################################
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   244
    #
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   245
    #    PageCatcher plugins
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   246
    # These let you use our PageCatcher product to add figures
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   247
    # to other documents easily.
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   248
    ####################################################################
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   249
    class PageCatcherCachingMixIn:
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   250
        "Helper functions to cache pages for figures"
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   251
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   252
        def getFormName(self, pdfFileName, pageNo):
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   253
            #naming scheme works within a directory only
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   254
            dirname, filename = os.path.split(pdfFileName)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   255
            root, ext = os.path.splitext(filename)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   256
            return '%s_page%d' % (root, pageNo)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   257
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   258
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   259
        def needsProcessing(self, pdfFileName, pageNo):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   260
            "returns 1 if no forms or form is older"
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   261
            formName = self.getFormName(pdfFileName, pageNo)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   262
            if os.path.exists(formName + '.frm'):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   263
                formModTime = os.stat(formName + '.frm')[8]
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   264
                pdfModTime = os.stat(pdfFileName)[8]
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   265
                return (pdfModTime > formModTime)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   266
            else:
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   267
                return 1
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   268
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   269
        def processPDF(self, pdfFileName, pageNo):
2159
c3bef6ff4917 Bug in one figure
andy_robinson
parents: 2158
diff changeset
   270
            formName = self.getFormName(pdfFileName, pageNo)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   271
            storeForms(pdfFileName, formName + '.frm',
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   272
                                    prefix= formName + '_',
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   273
                                    pagenumbers=[pageNo])
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   274
            #print 'stored %s.frm' % formName
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   275
            return formName + '.frm'
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   276
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   277
    class cachePageCatcherFigureNonA4(FlexFigure, PageCatcherCachingMixIn):
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   278
        """PageCatcher page with a caption below it.  Size to be supplied."""
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   279
        # This should merge with PageFigure into one class that reuses
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   280
        # form information to determine the page orientation...
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   281
        def __init__(self, filename, pageNo, caption, width, height, background=None):
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   282
            self.dirname, self.filename = os.path.split(filename)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   283
            if self.dirname == '':
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   284
                self.dirname = os.curdir
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   285
            self.pageNo = pageNo
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   286
            self.formName = self.getFormName(self.filename, self.pageNo) + '_' + str(pageNo)
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   287
            FlexFigure.__init__(self, width, height, caption, background)
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   288
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   289
        def drawFigure(self):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   290
            self.canv.saveState()
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   291
            if not self.canv.hasForm(self.formName):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   292
                restorePath = self.dirname + os.sep + self.filename
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   293
                #does the form file exist?  if not, generate it.
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   294
                formFileName = self.getFormName(restorePath, self.pageNo) + '.frm'
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   295
                if self.needsProcessing(restorePath, self.pageNo):
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   296
                    #print 'preprocessing PDF %s page %s' % (restorePath, self.pageNo)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   297
                    self.processPDF(restorePath, self.pageNo)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   298
                names = restoreForms(formFileName, self.canv)
2175
8570f75b54d7 I can only get gensampler to work with these changes
rgbecker
parents: 2170
diff changeset
   299
            self.canv.scale(self._scaleFactor, self._scaleFactor)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   300
            self.canv.doForm(self.formName)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   301
            self.canv.restoreState()
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   302
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   303
    class cachePageCatcherFigure(cachePageCatcherFigureNonA4):
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   304
        """PageCatcher page with a caption below it.  Presumes A4, Portrait.
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   305
        This needs our commercial PageCatcher product, or you'll get a blank."""
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   306
        def __init__(self, filename, pageNo, caption, width=595, height=842, background=None):
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   307
            cachePageCatcherFigureNonA4.__init__(self, filename, pageNo, caption, width, height, background=background)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   308
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   309
    class PageCatcherFigureNonA4(FlexFigure):
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   310
        """PageCatcher page with a caption below it.  Size to be supplied."""
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   311
        # This should merge with PageFigure into one class that reuses
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   312
        # form information to determine the page orientation...
2195
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   313
        _cache = {}
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   314
        def __init__(self, filename, pageNo, caption, width, height, background=None, caching=None):
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   315
            fn = self.filename = filename
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   316
            self.pageNo = pageNo
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   317
            fn = fn.replace(os.sep,'_').replace('/','_').replace('\\','_').replace('-','_').replace(':','_')
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   318
            self.prefix = fn.replace('.','_')+'_'+str(pageNo)+'_'
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   319
            self.formName = self.prefix + str(pageNo)
2195
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   320
            self.caching = caching
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   321
            FlexFigure.__init__(self, width, height, caption, background)
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   322
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   323
        def drawFigure(self):
2195
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   324
            if not self.canv.hasForm(self.formName):
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   325
                if self.filename in self._cache:
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   326
                    f,data = self._cache[self.filename]
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   327
                else:
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   328
                    f = open(self.filename,'rb')
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   329
                    pdf = f.read()
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   330
                    f.close()
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   331
                    f, data = storeFormsInMemory(pdf, pagenumbers=[self.pageNo], prefix=self.prefix)
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   332
                    if self.caching=='memory':
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   333
                        self._cache[self.filename] = f, data
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   334
                f = restoreFormsInMemory(data, self.canv)
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   335
            self.canv.saveState()
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   336
            self.canv.scale(self._scaleFactor, self._scaleFactor)
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   337
            self.canv.doForm(self.formName)
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   338
            self.canv.restoreState()
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   339
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   340
    class PageCatcherFigure(PageCatcherFigureNonA4):
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   341
        """PageCatcher page with a caption below it.  Presumes A4, Portrait.
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   342
        This needs our commercial PageCatcher product, or you'll get a blank."""
2195
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   343
        def __init__(self, filename, pageNo, caption, width=595, height=842, background=None, caching=None):
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   344
            PageCatcherFigureNonA4.__init__(self, filename, pageNo, caption, width, height, background=background, caching=caching)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   345
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   346
def demo1(canvas):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   347
    frame = Frame(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   348
                    2*inch,     # x
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   349
                    4*inch,     # y at bottom
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   350
                    4*inch,     # width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   351
                    5*inch,     # height
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   352
                    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
   353
                    )
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   354
    bodyStyle = ParagraphStyle('Body', fontName='Times-Roman', fontSize=24, leading=28, spaceBefore=6)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   355
    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
   356
    para2 = Paragraph('Eggs eggs eggs. ' * 5, bodyStyle)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   357
    mydata = [para1, para2]
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   358
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   359
    #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
   360
    #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
   361
    frame.addFromList(mydata,canvas)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   362
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   363
def test1():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   364
    c  = Canvas('figures.pdf')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   365
    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
   366
    v = PlatPropFigure1()
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   367
    v.captionTextColor = toColor('blue')
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   368
    v.captionBackColor = toColor('lightyellow')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   369
    f.addFromList([v],c)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   370
    c.save()
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   371
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   372
if __name__ == '__main__':
2175
8570f75b54d7 I can only get gensampler to work with these changes
rgbecker
parents: 2170
diff changeset
   373
    test1()