src/reportlab/platypus/figures.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4370 823a8c33ce43
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2311
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/platypus/figures.py
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     4
"""This includes some demos of platypus for use in the API proposal"""
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4144
diff changeset
     5
__version__='3.3.0'
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
     6
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
     7
import os
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     8
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
     9
from reportlab.lib import colors
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    10
from reportlab.pdfgen.canvas import Canvas
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    11
from reportlab.lib.styles import ParagraphStyle
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    12
from reportlab.lib.utils import recursiveImport, strTypes
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    13
from reportlab.platypus import Frame
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    14
from reportlab.platypus import Flowable
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    15
from reportlab.platypus import Paragraph
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    16
from reportlab.lib.units import inch
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    17
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
    18
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
    19
from reportlab.lib.colors import toColor
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 2991
diff changeset
    20
from reportlab.lib.styles import _baseFontName, _baseFontNameI
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    21
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 2991
diff changeset
    22
captionStyle = ParagraphStyle('Caption', fontName=_baseFontNameI, fontSize=10, alignment=TA_CENTER)
1420
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="",
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 2991
diff changeset
    26
                 captionFont=_baseFontNameI, captionSize=12,
2311
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,
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    30
                 border=None,
2686
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
    31
                 spaceBefore=12,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
    32
                 spaceAfter=12,
2687
9557dac48e77 figures.py: changes required for rml2pdf imageFigure tag
rgbecker
parents: 2686
diff changeset
    33
                 captionGap=None,
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    34
                 captionAlign='centre',
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    35
                 captionPosition='bottom',
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    36
                 hAlign='CENTER',
2686
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
    37
                 ):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    38
        Flowable.__init__(self)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    39
        self.width = width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    40
        self.figureHeight = height
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    41
        self.caption = caption
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    42
        self.captionFont = captionFont
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    43
        self.captionSize = captionSize
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    44
        self.captionTextColor = captionTextColor
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    45
        self.captionBackColor = captionBackColor
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    46
        self.captionGap = captionGap or 0.5*captionSize
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    47
        self.captionAlign = captionAlign
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    48
        self.captionPosition = captionPosition
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    49
        self._captionData = None
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    50
        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
    51
        self.background = background
2311
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
    52
        self.border = border
2686
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
    53
        self.spaceBefore = spaceBefore
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
    54
        self.spaceAfter = spaceAfter
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    55
        self.hAlign=hAlign
2991
d8249ddfb8e4 fixed late figure sequence evaluation
andy
parents: 2964
diff changeset
    56
        self._getCaptionPara()  #Larry Meyn's fix - otherwise they all get the number of the last chapter.
d8249ddfb8e4 fixed late figure sequence evaluation
andy
parents: 2964
diff changeset
    57
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    58
    def _getCaptionPara(self):
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    59
        caption = self.caption
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    60
        captionFont = self.captionFont
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    61
        captionSize = self.captionSize
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    62
        captionTextColor = self.captionTextColor
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    63
        captionBackColor = self.captionBackColor
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    64
        captionAlign = self.captionAlign
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    65
        captionPosition = self.captionPosition
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    66
        if self._captionData!=(caption,captionFont,captionSize,captionTextColor,captionBackColor,captionAlign,captionPosition):
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    67
            self._captionData = (caption,captionFont,captionSize,captionTextColor,captionBackColor,captionAlign,captionPosition)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    68
            if isinstance(caption,Paragraph):
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    69
                self.captionPara = caption
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    70
            elif isinstance(caption,strTypes):
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    71
                self.captionStyle = ParagraphStyle(
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    72
                    'Caption',
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    73
                    fontName=captionFont,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    74
                    fontSize=captionSize,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    75
                    leading=1.2*captionSize,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    76
                    textColor = captionTextColor,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    77
                    backColor = captionBackColor,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    78
                    #seems to be getting ignored
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    79
                    spaceBefore=self.captionGap,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    80
                    alignment=TA_LEFT if captionAlign=='left' else TA_RIGHT if captionAlign=='right' else TA_CENTER,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    81
                    )
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    82
                #must build paragraph now to get sequencing in synch with rest of story
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    83
                self.captionPara = Paragraph(self.caption, self.captionStyle)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    84
            else:
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    85
                raise ValueError('Figure caption of type %r is not a string or Paragraph' % type(caption))
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
    86
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    87
    def wrap(self, availWidth, availHeight):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
    88
        # try to get the caption aligned
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    89
        if self.caption:
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
    90
            self._getCaptionPara()
2931
6c123341228e figures.py: fix captionGap=None case
rgbecker
parents: 2930
diff changeset
    91
            w, h = self.captionPara.wrap(self.width, availHeight - self.figureHeight)
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    92
            self.captionHeight = h + self.captionGap
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    93
            self.height = self.captionHeight + self.figureHeight
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    94
            if w>self.width: self.width = w
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    95
        else:
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
    96
            self.height = self.figureHeight
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    97
        if self.hAlign in ('CENTER','CENTRE',TA_CENTER):
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    98
            self.dx = 0.5 * (availWidth - self.width)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
    99
        elif self.hAlign in ('RIGHT',TA_RIGHT):
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   100
            self.dx = availWidth - self.width
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   101
        else:
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   102
            self.dx = 0
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   103
        return (self.width, self.height)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   104
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   105
    def draw(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   106
        self.canv.translate(self.dx, 0)
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   107
        if self.caption and self.captionPosition=='bottom':
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   108
            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
   109
        if self.background:
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   110
            self.drawBackground()
2311
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
   111
        if self.border:
b594c6c22203 Prelim support for unicode conversion (disabled by
andy_robinson
parents: 2200
diff changeset
   112
            self.drawBorder()
4144
5f3f91424f10 figures.py: prevent transformation leakage from drawFigure
robin
parents: 4137
diff changeset
   113
        self.canv.saveState()
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   114
        self.drawFigure()
4144
5f3f91424f10 figures.py: prevent transformation leakage from drawFigure
robin
parents: 4137
diff changeset
   115
        self.canv.restoreState()
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   116
        if self.caption:
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   117
            if self.captionPosition=='bottom':
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   118
                self.canv.translate(0, -self.captionHeight)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   119
            else:
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   120
                self.canv.translate(0, self.figureHeight+self.captionGap)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   121
            self._getCaptionPara()
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   122
            self.drawCaption()
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   123
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   124
    def drawBorder(self):
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   125
        canv = self.canv
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   126
        border = self.border
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   127
        bc = getattr(border,'color',None)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   128
        bw = getattr(border,'width',None)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   129
        bd = getattr(border,'dashArray',None)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   130
        ss = bc or bw or bd
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   131
        if ss:
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   132
            canv.saveState()
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   133
            if bc: canv.setStrokeColor(bc)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   134
            if bw: canv.setLineWidth(bw)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   135
            if bd: canv.setDash(bd)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   136
        canv.rect(0, 0, self.width, self.figureHeight,fill=0,stroke=1)
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   137
        if ss:
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   138
            canv.restoreState()
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   139
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   140
    def _doBackground(self, color):
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   141
        self.canv.saveState()
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   142
        self.canv.setFillColor(self.background)
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   143
        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
   144
        self.canv.restoreState()
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   145
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   146
    def drawBackground(self):
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   147
        """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
   148
        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
   149
        if isColor(self.background):
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   150
            self._doBackground(self.background)
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   151
        else:
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   152
            try:
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   153
                c = toColor(self.background)
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   154
                self._doBackground(c)
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   155
            except:
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   156
                pass
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   157
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   158
    def drawCaption(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   159
        self.captionPara.drawOn(self.canv, 0, 0)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   160
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   161
    def drawFigure(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   162
        pass
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   163
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   164
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
   165
    #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
   166
    pth = canvas.beginPath()
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   167
    corner = 0.05*width
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   168
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   169
    # shaded backdrop offset a little
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   170
    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
   171
    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
   172
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   173
    #'sheet of paper' in light yellow
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   174
    canvas.setFillColorRGB(1,1,0.9)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   175
    canvas.setLineWidth(0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   176
    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
   177
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   178
    #reset
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   179
    canvas.setFillColorRGB(0,0,0)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   180
    canvas.setStrokeColorRGB(0,0,0)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   181
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   182
class PageFigure(Figure):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   183
    """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
   184
    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
   185
    def __init__(self, background=None):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   186
        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
   187
        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
   188
        self.captionStyle = captionStyle
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   189
        self.background = background
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   190
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   191
    def drawVirtualPage(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   192
        pass
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   193
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   194
    def drawFigure(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   195
        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
   196
        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
   197
        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
   198
        self.drawVirtualPage()
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   199
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   200
class PlatPropFigure1(PageFigure):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   201
    """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
   202
    def __init__(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   203
        PageFigure.__init__(self)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   204
        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
   205
    def drawVirtualPage(self):
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   206
        demo1(self.canv)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   207
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   208
class FlexFigure(Figure):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   209
    """Base for a figure class with a caption. Can grow or shrink in proportion"""
2686
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   210
    def __init__(self, width, height, caption, background=None,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   211
                        captionFont='Helvetica-Oblique',captionSize=8,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   212
                        captionTextColor=colors.black,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   213
                        shrinkToFit=1,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   214
                        growToFit=1,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   215
                        spaceBefore=12,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   216
                        spaceAfter=12,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   217
                        captionGap=9,
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   218
                        captionAlign='centre',
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   219
                        captionPosition='top',
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   220
                        scaleFactor=None,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   221
                        hAlign='CENTER',
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   222
                        border=1,
2686
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   223
                        ):
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   224
        Figure.__init__(self, width, height, caption,
2686
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   225
                        captionFont=captionFont,
2687
9557dac48e77 figures.py: changes required for rml2pdf imageFigure tag
rgbecker
parents: 2686
diff changeset
   226
                        captionSize=captionSize,
2686
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   227
                        background=None,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   228
                        captionTextColor=captionTextColor,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   229
                        spaceBefore = spaceBefore,
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   230
                        spaceAfter = spaceAfter,
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   231
                        captionGap=captionGap,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   232
                        captionAlign=captionAlign,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   233
                        captionPosition=captionPosition,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   234
                        hAlign=hAlign,
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   235
                        border=border,
2686
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   236
                        )
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   237
        self.shrinkToFit = shrinkToFit  #if set and wrap is too tight, shrinks
db7092053fda figures.py: minor changes to argument passing etc
rgbecker
parents: 2332
diff changeset
   238
        self.growToFit = growToFit      #if set and wrap is too small, grows
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   239
        self.scaleFactor = scaleFactor
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   240
        self._scaleFactor = None
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   241
        self.background = background
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   242
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   243
    def _scale(self,availWidth,availHeight):
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   244
        "Rescale to fit according to the rules, but only once"
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   245
        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
   246
            w, h = Figure.wrap(self, availWidth, availHeight)
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   247
            captionHeight = h - self.figureHeight
2167
4ddf1ef71640 New pagecatcher and drawing tags (experimental)
andy_robinson
parents: 2164
diff changeset
   248
            if self.scaleFactor is None:
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   249
                #scale factor None means auto
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   250
                self._scaleFactor = min(availWidth/self.width,(availHeight-captionHeight)/self.figureHeight)
2167
4ddf1ef71640 New pagecatcher and drawing tags (experimental)
andy_robinson
parents: 2164
diff changeset
   251
            else: #they provided a factor
4ddf1ef71640 New pagecatcher and drawing tags (experimental)
andy_robinson
parents: 2164
diff changeset
   252
                self._scaleFactor = self.scaleFactor
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   253
            if self._scaleFactor<1 and self.shrinkToFit:
2175
8570f75b54d7 I can only get gensampler to work with these changes
rgbecker
parents: 2170
diff changeset
   254
                self.width = self.width * self._scaleFactor - 0.0001
2167
4ddf1ef71640 New pagecatcher and drawing tags (experimental)
andy_robinson
parents: 2164
diff changeset
   255
                self.figureHeight = self.figureHeight * self._scaleFactor
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   256
            elif self._scaleFactor>1 and self.growToFit:
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   257
                self.width = self.width*self._scaleFactor - 0.0001
2167
4ddf1ef71640 New pagecatcher and drawing tags (experimental)
andy_robinson
parents: 2164
diff changeset
   258
                self.figureHeight = self.figureHeight * self._scaleFactor
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   259
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   260
    def wrap(self, availWidth, availHeight):
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   261
        self._scale(availWidth,availHeight)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   262
        return Figure.wrap(self, availWidth, availHeight)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   263
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   264
    def split(self, availWidth, availHeight):
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   265
        self._scale(availWidth,availHeight)
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   266
        return Figure.split(self, availWidth, availHeight)
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   267
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   268
class ImageFigure(FlexFigure):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   269
    """Image with a caption below it"""
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   270
    def __init__(self, filename, caption, background=None,scaleFactor=None,hAlign='CENTER',border=None):
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   271
        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
   272
        from reportlab.lib.utils import ImageReader
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   273
        w, h = ImageReader(filename).getSize()
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   274
        self.filename = filename
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   275
        FlexFigure.__init__(self, w, h, caption, background,scaleFactor=scaleFactor,hAlign=hAlign,border=border)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   276
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   277
    def drawFigure(self):
4137
7e1936f8193c various fixes to figures.py; version-->3.1.36
robin
parents: 3617
diff changeset
   278
        self.canv.drawImage(self.filename,
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   279
                                  0, 0,self.width, self.figureHeight)
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2195
diff changeset
   280
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   281
class DrawingFigure(FlexFigure):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   282
    """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
   283
    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
   284
        module = recursiveImport(modulename, baseDir)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   285
        klass = getattr(module, classname)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   286
        self.drawing = klass()
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   287
        FlexFigure.__init__(self,
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   288
                            self.drawing.width,
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   289
                            self.drawing.height,
2162
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   290
                            caption,
bdd60afa3e48 Added a background attribute (default None) to most figures - if it's a
johnprecedo
parents: 2159
diff changeset
   291
                            background)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   292
        self.growToFit = 1
2200
be0cfccc662a Fixed up tabs and whitespace in all source files
andy_robinson
parents: 2195
diff changeset
   293
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   294
    def drawFigure(self):
2175
8570f75b54d7 I can only get gensampler to work with these changes
rgbecker
parents: 2170
diff changeset
   295
        self.canv.scale(self._scaleFactor, self._scaleFactor)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   296
        self.drawing.drawOn(self.canv, 0, 0)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   297
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   298
try:
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   299
    from rlextra.pageCatcher.pageCatcher import restoreForms, storeForms, storeFormsInMemory, restoreFormsInMemory
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   300
    _hasPageCatcher = 1
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   301
except ImportError:
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   302
    _hasPageCatcher = 0
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   303
if _hasPageCatcher:
2158
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   304
    ####################################################################
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   305
    #
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   306
    #    PageCatcher plugins
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   307
    # These let you use our PageCatcher product to add figures
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   308
    # to other documents easily.
e9c34b93e317 Whoops, platform specific slashes.
andy_robinson
parents: 2157
diff changeset
   309
    ####################################################################
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   310
    class PageCatcherCachingMixIn:
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   311
        "Helper functions to cache pages for figures"
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   312
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   313
        def getFormName(self, pdfFileName, pageNo):
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   314
            #naming scheme works within a directory only
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   315
            dirname, filename = os.path.split(pdfFileName)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   316
            root, ext = os.path.splitext(filename)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   317
            return '%s_page%d' % (root, pageNo)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   318
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   319
        def needsProcessing(self, pdfFileName, pageNo):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   320
            "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
   321
            formName = self.getFormName(pdfFileName, pageNo)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   322
            if os.path.exists(formName + '.frm'):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   323
                formModTime = os.stat(formName + '.frm')[8]
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   324
                pdfModTime = os.stat(pdfFileName)[8]
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   325
                return (pdfModTime > formModTime)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   326
            else:
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   327
                return 1
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   328
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   329
        def processPDF(self, pdfFileName, pageNo):
2159
c3bef6ff4917 Bug in one figure
andy_robinson
parents: 2158
diff changeset
   330
            formName = self.getFormName(pdfFileName, pageNo)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   331
            storeForms(pdfFileName, formName + '.frm',
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   332
                                    prefix= formName + '_',
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   333
                                    pagenumbers=[pageNo])
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   334
            #print 'stored %s.frm' % formName
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   335
            return formName + '.frm'
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   336
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   337
    class cachePageCatcherFigureNonA4(FlexFigure, PageCatcherCachingMixIn):
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   338
        """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
   339
        # 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
   340
        # 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
   341
        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
   342
            self.dirname, self.filename = os.path.split(filename)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   343
            if self.dirname == '':
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   344
                self.dirname = os.curdir
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   345
            self.pageNo = pageNo
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   346
            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
   347
            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
   348
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   349
        def drawFigure(self):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   350
            self.canv.saveState()
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   351
            if not self.canv.hasForm(self.formName):
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   352
                restorePath = self.dirname + os.sep + self.filename
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   353
                #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
   354
                formFileName = self.getFormName(restorePath, self.pageNo) + '.frm'
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   355
                if self.needsProcessing(restorePath, self.pageNo):
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   356
                    #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
   357
                    self.processPDF(restorePath, self.pageNo)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   358
                names = restoreForms(formFileName, self.canv)
2175
8570f75b54d7 I can only get gensampler to work with these changes
rgbecker
parents: 2170
diff changeset
   359
            self.canv.scale(self._scaleFactor, self._scaleFactor)
2157
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   360
            self.canv.doForm(self.formName)
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   361
            self.canv.restoreState()
cc8354f2171e Added figure classes, made XML editable in XML-SPY
andy_robinson
parents: 1683
diff changeset
   362
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   363
    class cachePageCatcherFigure(cachePageCatcherFigureNonA4):
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   364
        """PageCatcher page with a caption below it.  Presumes A4, Portrait.
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   365
        This needs our commercial PageCatcher product, or you'll get a blank."""
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   366
        def __init__(self, filename, pageNo, caption, width=595, height=842, background=None):
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   367
            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
   368
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   369
    class PageCatcherFigureNonA4(FlexFigure):
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   370
        """PageCatcher page with a caption below it.  Size to be supplied."""
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   371
        # This should merge with PageFigure into one class that reuses
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   372
        # form information to determine the page orientation...
2195
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   373
        _cache = {}
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   374
        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
   375
            fn = self.filename = filename
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   376
            self.pageNo = pageNo
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   377
            fn = fn.replace(os.sep,'_').replace('/','_').replace('\\','_').replace('-','_').replace(':','_')
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   378
            self.prefix = fn.replace('.','_')+'_'+str(pageNo)+'_'
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   379
            self.formName = self.prefix + str(pageNo)
2195
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   380
            self.caching = caching
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   381
            FlexFigure.__init__(self, width, height, caption, background)
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   382
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   383
        def drawFigure(self):
2195
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   384
            if not self.canv.hasForm(self.formName):
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   385
                if self.filename in self._cache:
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   386
                    f,data = self._cache[self.filename]
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   387
                else:
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   388
                    f = open(self.filename,'rb')
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   389
                    pdf = f.read()
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   390
                    f.close()
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   391
                    f, data = storeFormsInMemory(pdf, pagenumbers=[self.pageNo], prefix=self.prefix)
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   392
                    if self.caching=='memory':
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   393
                        self._cache[self.filename] = f, data
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   394
                f = restoreFormsInMemory(data, self.canv)
2188
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   395
            self.canv.saveState()
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   396
            self.canv.scale(self._scaleFactor, self._scaleFactor)
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   397
            self.canv.doForm(self.formName)
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   398
            self.canv.restoreState()
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   399
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   400
    class PageCatcherFigure(PageCatcherFigureNonA4):
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   401
        """PageCatcher page with a caption below it.  Presumes A4, Portrait.
0a4449882a3a Improved FlexFigure scaling, in memory PageCatcherFigures
rgbecker
parents: 2175
diff changeset
   402
        This needs our commercial PageCatcher product, or you'll get a blank."""
2195
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   403
        def __init__(self, filename, pageNo, caption, width=595, height=842, background=None, caching=None):
746193239ca1 Added optional memory caching
rgbecker
parents: 2191
diff changeset
   404
            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
   405
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   406
def demo1(canvas):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   407
    frame = Frame(
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   408
                    2*inch,     # x
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   409
                    4*inch,     # y at bottom
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   410
                    4*inch,     # width
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   411
                    5*inch,     # height
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   412
                    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
   413
                    )
3368
afa025c34493 reportlab: new base font mechanism more fully applied
rgbecker
parents: 2991
diff changeset
   414
    bodyStyle = ParagraphStyle('Body', fontName=_baseFontName, fontSize=24, leading=28, spaceBefore=6)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   415
    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
   416
    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
   417
    mydata = [para1, para2]
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   418
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   419
    #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
   420
    #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
   421
    frame.addFromList(mydata,canvas)
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   422
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   423
def test1():
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   424
    c  = Canvas('figures.pdf')
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   425
    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
   426
    v = PlatPropFigure1()
2191
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   427
    v.captionTextColor = toColor('blue')
11115dca3822 Fix up caption and split methods etc
rgbecker
parents: 2188
diff changeset
   428
    v.captionBackColor = toColor('lightyellow')
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   429
    f.addFromList([v],c)
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1559
diff changeset
   430
    c.save()
1420
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   431
bd1f0adf63d3 Initial checkin, copy of tools/docco/platdemos.py.
dinu_gherman
parents:
diff changeset
   432
if __name__ == '__main__':
2175
8570f75b54d7 I can only get gensampler to work with these changes
rgbecker
parents: 2170
diff changeset
   433
    test1()