tests/test_pdfgen_general.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4341 19a971b1bfa4
permissions -rw-r--r--
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     1
#!/bin/env python
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     2
#Copyright ReportLab Europe Ltd. 2000-2017
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     3
#see license.txt for license details
2967
ea62529bd1df reportlab-2.2: first stage changes in on the trunk
rgbecker
parents: 2966
diff changeset
     4
__doc__='testscript for reportlab.pdfgen'
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4212
diff changeset
     5
__version__='3.3.0'
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
     6
#tests and documents new low-level canvas
2987
711910106e3a tests: more changes to testsFolder and friends
rgbecker
parents: 2984
diff changeset
     7
from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, outputfile, printLocation
2984
c63f149d55aa tests.utils --> reportlab.lib.testutils
rgbecker
parents: 2967
diff changeset
     8
setOutDir(__name__)
3794
398ea04239b5 string module usage minimization
robin
parents: 3721
diff changeset
     9
import os
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
    10
import unittest
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    11
from reportlab.pdfgen import canvas   # gmcm 2000/10/13, pdfgen now a package
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    12
from reportlab.lib.units import inch, cm
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    13
from reportlab.lib import colors
3849
8ff9b1fb92cd utils.py: fix isUnicode, rename fileName2Utf8-->fileName2FSEnc
robin
parents: 3800
diff changeset
    14
from reportlab.lib.utils import haveImages, fileName2FSEnc
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    15
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    16
#################################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    17
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    18
#  first some drawing utilities
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    19
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    20
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    21
################################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    22
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    23
BASEFONT = ('Times-Roman', 10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    24
def framePageForm(c):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    25
    c.beginForm("frame")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    26
    c.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    27
    # forms can't do non-constant operations
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    28
    #canvas.setFont('Times-BoldItalic',20)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    29
    #canvas.drawString(inch, 10.5 * inch, title)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    30
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    31
    #c.setFont('Times-Roman',10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    32
    #c.drawCentredString(4.135 * inch, 0.75 * inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    33
    #                        'Page %d' % c.getPageNumber())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    34
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    35
    #draw a border
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    36
    c.setFillColor(colors.ReportLabBlue)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    37
    c.rect(0.3*inch, inch, 0.5*inch, 10*inch, fill=1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    38
    from reportlab.lib import corp
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    39
    c.translate(0.8*inch, 9.6*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    40
    c.rotate(90)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    41
    logo = corp.ReportLabLogo(width=1.3*inch, height=0.5*inch, powered_by=1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    42
    c.setFillColorRGB(1,1,1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    43
    c.setStrokeColorRGB(1,1,1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    44
    logo.draw(c)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    45
    #c.setStrokeColorRGB(1,0,0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    46
    #c.setLineWidth(5)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    47
    #c.line(0.8 * inch, inch, 0.8 * inch, 10.75 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    48
    #reset carefully afterwards
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    49
    #canvas.setLineWidth(1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    50
    #canvas.setStrokeColorRGB(0,0,0)\
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    51
    c.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    52
    c.endForm()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    53
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    54
def framePage(canvas, title):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    55
    global closeit
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    56
    titlelist.append(title)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    57
    #canvas._inPage0()  # do we need this at all?  would be good to eliminate it
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    58
    canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    59
    canvas.setFont('Times-BoldItalic',20)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    60
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    61
    canvas.drawString(inch, 10.5 * inch, title)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    62
    canvas.bookmarkHorizontalAbsolute(title, 10.8*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    63
    #newsection(title)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    64
    canvas.addOutlineEntry(title+" section", title, level=0, closed=closeit)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    65
    closeit = not closeit # close every other one
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    66
    canvas.setFont('Times-Roman',10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    67
    canvas.drawCentredString(4.135 * inch, 0.75 * inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    68
                            'Page %d' % canvas.getPageNumber())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    69
    canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    70
    canvas.doForm("frame")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    71
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    72
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    73
def makesubsection(canvas, title, horizontal):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    74
    canvas.bookmarkHorizontalAbsolute(title, horizontal)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    75
    #newsubsection(title)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    76
    canvas.addOutlineEntry(title+" subsection", title, level=1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    77
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    78
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    79
# outline helpers
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    80
#outlinenametree = []
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    81
#def newsection(name):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    82
#    outlinenametree.append(name)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    83
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    84
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    85
#def newsubsection(name):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    86
#    thissection = outlinenametree[-1]
3800
e8547b00eb59 attempt to remove old type testing
robin
parents: 3794
diff changeset
    87
#    if not isinstance(thissection,tuple):
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    88
#        subsectionlist = []
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    89
#        thissection = outlinenametree[-1] = (thissection, subsectionlist)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    90
#    else:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    91
#        (sectionname, subsectionlist) = thissection
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    92
#    subsectionlist.append(name)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    93
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    94
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    95
class DocBlock:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    96
    """A DocBlock has a chunk of commentary and a chunk of code.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    97
    It prints the code and commentary, then executes the code,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    98
    which is presumed to draw in a region reserved for it.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
    99
    """
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   100
    def __init__(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   101
        self.comment1 = "A doc block"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   102
        self.code = "canvas.setTextOrigin(cm, cm)\ncanvas.textOut('Hello World')"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   103
        self.comment2 = "That was a doc block"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   104
        self.drawHeight = 0
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   105
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   106
    def _getHeight(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   107
        "splits into lines"
3794
398ea04239b5 string module usage minimization
robin
parents: 3721
diff changeset
   108
        self.comment1lines = self.comment1.split('\n')
398ea04239b5 string module usage minimization
robin
parents: 3721
diff changeset
   109
        self.codelines = self.code.split('\n')
398ea04239b5 string module usage minimization
robin
parents: 3721
diff changeset
   110
        self.comment2lines = self.comment2.split('\n')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   111
        textheight = (len(self.comment1lines) +
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   112
                len(self.code) +
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   113
                len(self.comment2lines) +
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   114
                18)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   115
        return max(textheight, self.drawHeight)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   116
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   117
    def draw(self, canvas, x, y):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   118
        #specifies top left corner
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   119
        canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   120
        height = self._getHeight()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   121
        canvas.rect(x, y-height, 6*inch, height)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   122
        #first draw the text
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   123
        canvas.setTextOrigin(x + 3 * inch, y - 12)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   124
        canvas.setFont('Times-Roman',10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   125
        canvas.textLines(self.comment1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   126
        drawCode(canvas, self.code)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   127
        canvas.textLines(self.comment2)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   128
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   129
        #now a box for the drawing, slightly within rect
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   130
        canvas.rect(x + 9, y - height + 9, 198, height - 18)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   131
        #boundary:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   132
        self.namespace = {'canvas':canvas,'cm': cm,'inch':inch}
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   133
        canvas.translate(x+9, y - height + 9)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   134
        codeObj = compile(self.code, '<sample>','exec')
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3680
diff changeset
   135
        exec(codeObj, self.namespace)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   136
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   137
        canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   138
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   139
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   140
def drawAxes(canvas, label):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   141
    """draws a couple of little rulers showing the coords -
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   142
    uses points as units so you get an imperial ruler
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   143
    one inch on each side"""
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   144
    #y axis
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   145
    canvas.line(0,0,0,72)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   146
    for y in range(9):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   147
        tenths = (y+1) * 7.2
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   148
        canvas.line(-6,tenths,0,tenths)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   149
    canvas.line(-6, 66, 0, 72)  #arrow...
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   150
    canvas.line(6, 66, 0, 72)  #arrow...
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   151
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   152
    canvas.line(0,0,72,0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   153
    for x in range(9):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   154
        tenths = (x+1) * 7.2
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   155
        canvas.line(tenths,-6,tenths, 0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   156
    canvas.line(66, -6, 72, 0)  #arrow...
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   157
    canvas.line(66, +6, 72, 0)  #arrow...
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   158
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   159
    canvas.drawString(18, 30, label)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   160
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   161
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   162
def drawCrossHairs(canvas, x, y):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   163
    """just a marker for checking text metrics - blue for fun"""
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   164
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   165
    canvas.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   166
    canvas.setStrokeColorRGB(0,1,0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   167
    canvas.line(x-6,y,x+6,y)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   168
    canvas.line(x,y-6,x,y+6)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   169
    canvas.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   170
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   171
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   172
def drawCode(canvas, code):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   173
    """Draws a block of text at current point, indented and in Courier"""
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   174
    canvas.addLiteral('36 0 Td')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   175
    canvas.setFillColor(colors.blue)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   176
    canvas.setFont('Courier',10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   177
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   178
    t = canvas.beginText()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   179
    t.textLines(code)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   180
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   181
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   182
    canvas.setFillColor(colors.black)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   183
    canvas.addLiteral('-36 0 Td')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   184
    canvas.setFont('Times-Roman',10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   185
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   186
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   187
def makeDocument(filename, pageCallBack=None):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   188
    #the extra arg is a hack added later, so other
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   189
    #tests can get hold of the canvas just before it is
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   190
    #saved
4179
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   191
    from reportlab.lib.colors import red, green, blue
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   192
    global titlelist, closeit
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   193
    titlelist = []
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   194
    closeit = 0
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   195
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   196
    c = canvas.Canvas(filename)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   197
    c.setPageCompression(0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   198
    c.setPageCallBack(pageCallBack)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   199
    framePageForm(c) # define the frame form
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   200
    c.showOutline()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   201
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   202
    framePage(c, 'PDFgen graphics API test script')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   203
    makesubsection(c, "PDFgen", 10*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   204
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   205
    #quickie encoding test: when canvas encoding not set,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   206
    #the following should do (tm), (r) and (c)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3680
diff changeset
   207
    msg_uni = 'copyright\u00A9 trademark\u2122 registered\u00AE scissors\u2702: ReportLab in unicode!'
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   208
    msg_utf8 = msg_uni.replace('unicode','utf8').encode('utf8')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   209
    c.drawString(100, 100, msg_uni)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   210
    c.drawString(100, 80, msg_utf8)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   211
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   212
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   213
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   214
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   215
    t = c.beginText(inch, 10*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   216
    t.setFont('Times-Roman', 10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   217
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   218
    t.textLines("""
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   219
The ReportLab library permits you to create PDF documents directly from
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   220
your Python code. The "pdfgen" subpackage is the lowest level exposed
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   221
to the user and lets you directly position test and graphics on the
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   222
page, with access to almost the full range of PDF features.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   223
  The API is intended to closely mirror the PDF / Postscript imaging
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   224
model.  There is an almost one to one correspondence between commands
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   225
and PDF operators.  However, where PDF provides several ways to do a job,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   226
we have generally only picked one.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   227
  The test script attempts to use all of the methods exposed by the Canvas
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   228
class, defined in reportlab/pdfgen/canvas.py
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   229
  First, let's look at text output.  There are some basic commands
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   230
to draw strings:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   231
-    canvas.setFont(fontname, fontsize [, leading])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   232
-    canvas.drawString(x, y, text)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   233
-    canvas.drawRightString(x, y, text)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   234
-    canvas.drawCentredString(x, y, text)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   235
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   236
The coordinates are in points starting at the bottom left corner of the
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   237
page.  When setting a font, the leading (i.e. inter-line spacing)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   238
defaults to 1.2 * fontsize if the fontsize is not provided.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   239
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   240
For more sophisticated operations, you can create a Text Object, defined
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   241
in reportlab/pdfgen/testobject.py.  Text objects produce tighter PDF, run
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   242
faster and have many methods for precise control of spacing and position.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   243
Basic usage goes as follows:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   244
-   tx = canvas.beginText(x, y)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   245
-   tx.textOut('Hello')    # this moves the cursor to the right
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   246
-   tx.textLine('Hello again') # prints a line and moves down
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   247
-   y = tx.getY()       # getX, getY and getCursor track position
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   248
-   canvas.drawText(tx)  # all gets drawn at the end
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   249
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   250
The green crosshairs below test whether the text cursor is working
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   251
properly.  They should appear at the bottom left of each relevant
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   252
substring.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   253
""")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   254
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   255
    t.setFillColorRGB(1,0,0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   256
    t.setTextOrigin(inch, 4*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   257
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   258
    t.textOut('textOut moves across:')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   259
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   260
    t.textOut('textOut moves across:')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   261
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   262
    t.textOut('textOut moves across:')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   263
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   264
    t.textLine('')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   265
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   266
    t.textLine('textLine moves down')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   267
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   268
    t.textLine('textLine moves down')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   269
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   270
    t.textLine('textLine moves down')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   271
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   272
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   273
    t.setTextOrigin(4*inch,3.25*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   274
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   275
    t.textLines('This is a multi-line\nstring with embedded newlines\ndrawn with textLines().\n')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   276
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   277
    t.textLines(['This is a list of strings',
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   278
                'drawn with textLines().'])
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   279
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   280
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   281
    t = c.beginText(2*inch,2*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   282
    t.setFont('Times-Roman',10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   283
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   284
    t.textOut('Small text.')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   285
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   286
    t.setFont('Courier',14)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   287
    t.textOut('Bigger fixed width text.')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   288
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   289
    t.setFont('Times-Roman',10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   290
    t.textOut('Small text again.')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   291
    drawCrossHairs(c, t.getX(),t.getY())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   292
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   293
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   294
    #try out the decimal tabs high on the right.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   295
    c.setStrokeColor(colors.silver)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   296
    c.line(7*inch, 6*inch, 7*inch, 4.5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   297
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   298
    c.setFillColor(colors.black)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   299
    c.setFont('Times-Roman',10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   300
    c.drawString(6*inch, 6.2*inch, "Testing decimal alignment")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   301
    c.drawString(6*inch, 6.05*inch, "- aim for silver line")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   302
    c.line(7*inch, 6*inch, 7*inch, 4.5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   303
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   304
    c.drawAlignedString(7*inch, 5.8*inch, "1,234,567.89")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   305
    c.drawAlignedString(7*inch, 5.6*inch, "3,456.789")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   306
    c.drawAlignedString(7*inch, 5.4*inch, "123")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   307
    c.setFillColor(colors.red)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   308
    c.drawAlignedString(7*inch, 5.2*inch, "(7,192,302.30)")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   309
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   310
    #mark the cursor where it stopped
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   311
    c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   312
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   313
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   314
    ##############################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   315
    #
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   316
    # page 2 - line styles
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   317
    #
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   318
    ###############################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   319
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   320
    #page 2 - lines and styles
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   321
    framePage(c, 'Line Drawing Styles')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   322
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   323
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   324
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   325
    # three line ends, lines drawn the hard way
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   326
    #firt make some vertical end markers
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   327
    c.setDash(4,4)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   328
    c.setLineWidth(0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   329
    c.line(inch,9.2*inch,inch, 7.8*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   330
    c.line(3*inch,9.2*inch,3*inch, 7.8*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   331
    c.setDash() #clears it
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   332
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   333
    c.setLineWidth(5)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   334
    c.setLineCap(0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   335
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   336
    p.moveTo(inch, 9*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   337
    p.lineTo(3*inch, 9*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   338
    c.drawPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   339
    c.drawString(4*inch, 9*inch, 'the default - butt caps project half a width')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   340
    makesubsection(c, "caps and joins", 8.5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   341
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   342
    c.setLineCap(1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   343
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   344
    p.moveTo(inch, 8.5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   345
    p.lineTo(3*inch, 8.5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   346
    c.drawPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   347
    c.drawString(4*inch, 8.5*inch, 'round caps')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   348
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   349
    c.setLineCap(2)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   350
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   351
    p.moveTo(inch, 8*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   352
    p.lineTo(3*inch, 8*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   353
    c.drawPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   354
    c.drawString(4*inch, 8*inch, 'square caps')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   355
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   356
    c.setLineCap(0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   357
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   358
    # three line joins
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   359
    c.setLineJoin(0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   360
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   361
    p.moveTo(inch, 7*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   362
    p.lineTo(2*inch, 7*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   363
    p.lineTo(inch, 6.7*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   364
    c.drawPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   365
    c.drawString(4*inch, 6.8*inch, 'Default - mitered join')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   366
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   367
    c.setLineJoin(1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   368
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   369
    p.moveTo(inch, 6.5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   370
    p.lineTo(2*inch, 6.5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   371
    p.lineTo(inch, 6.2*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   372
    c.drawPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   373
    c.drawString(4*inch, 6.3*inch, 'round join')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   374
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   375
    c.setLineJoin(2)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   376
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   377
    p.moveTo(inch, 6*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   378
    p.lineTo(2*inch, 6*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   379
    p.lineTo(inch, 5.7*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   380
    c.drawPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   381
    c.drawString(4*inch, 5.8*inch, 'bevel join')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   382
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   383
    c.setDash(6,6)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   384
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   385
    p.moveTo(inch, 5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   386
    p.lineTo(3*inch, 5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   387
    c.drawPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   388
    c.drawString(4*inch, 5*inch, 'dash 6 points on, 6 off- setDash(6,6) setLineCap(0)')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   389
    makesubsection(c, "dash patterns", 5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   390
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   391
    c.setLineCap(1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   392
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   393
    p.moveTo(inch, 4.5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   394
    p.lineTo(3*inch, 4.5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   395
    c.drawPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   396
    c.drawString(4*inch, 4.5*inch, 'dash 6 points on, 6 off- setDash(6,6) setLineCap(1)')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   397
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   398
    c.setLineCap(0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   399
    c.setDash([1,2,3,4,5,6],0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   400
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   401
    p.moveTo(inch, 4.0*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   402
    p.lineTo(3*inch, 4.0*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   403
    c.drawPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   404
    c.drawString(4*inch, 4*inch, 'dash growing - setDash([1,2,3,4,5,6],0) setLineCap(0)')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   405
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   406
    c.setLineCap(1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   407
    c.setLineJoin(1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   408
    c.setDash(32,12)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   409
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   410
    p.moveTo(inch, 3.0*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   411
    p.lineTo(2.5*inch, 3.0*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   412
    p.lineTo(inch, 2*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   413
    c.drawPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   414
    c.drawString(4*inch, 3*inch, 'dash pattern, join and cap style interacting - ')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   415
    c.drawString(4*inch, 3*inch - 12, 'round join & miter results in sausages')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   416
    c.textAnnotation('Annotation',Rect=(4*inch, 3*inch-72, inch,inch-12))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   417
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   418
    c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   419
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   420
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   421
##############################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   422
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   423
# higher level shapes
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   424
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   425
###############################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   426
    framePage(c, 'Shape Drawing Routines')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   427
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   428
    t = c.beginText(inch, 10*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   429
    t.textLines("""
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   430
Rather than making your own paths, you have access to a range of shape routines.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   431
These are built in pdfgen out of lines and bezier curves, but use the most compact
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   432
set of operators possible.  We can add any new ones that are of general use at no
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   433
cost to performance.""")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   434
    t.textLine()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   435
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   436
    #line demo
3574
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   437
    makesubsection(c, "lines", 9*inch)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   438
    c.line(inch, 9*inch, 3*inch, 9*inch)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   439
    t.setTextOrigin(4*inch, 9*inch)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   440
    t.textLine('canvas.line(x1, y1, x2, y2)')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   441
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   442
    #bezier demo - show control points
3574
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   443
    makesubsection(c, "bezier curves", 8.5*inch)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   444
    (x1, y1, x2, y2, x3, y3, x4, y4) = (
3574
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   445
                        inch, 7.8*inch,
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   446
                        1.2*inch, 8.8 * inch,
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   447
                        3*inch, 8.8 * inch,
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   448
                        3.5*inch, 8.05 * inch
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   449
                        )
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   450
    c.bezier(x1, y1, x2, y2, x3, y3, x4, y4)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   451
    c.setDash(3,3)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   452
    c.line(x1,y1,x2,y2)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   453
    c.line(x3,y3,x4,y4)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   454
    c.setDash()
3574
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   455
    t.setTextOrigin(4*inch, 8.3 * inch)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   456
    t.textLine('canvas.bezier(x1, y1, x2, y2, x3, y3, x4, y4)')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   457
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   458
    #rectangle
3574
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   459
    makesubsection(c, "rectangles", 8*inch)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   460
    c.rect(inch, 7 * inch, 2 * inch, 0.75 * inch)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   461
    t.setTextOrigin(4*inch, 7.375 * inch)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   462
    t.textLine('canvas.rect(x, y, width, height) - x,y is lower left')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   463
3574
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   464
    c.roundRect(inch,6.25*inch,2*inch,0.6*inch,0.1*inch)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   465
    t.setTextOrigin(4*inch, 6.55*inch)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   466
    t.textLine('canvas.roundRect(x,y,width,height,radius)')
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   467
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   468
    makesubsection(c, "arcs", 8*inch)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   469
    c.arc(inch,5*inch,3*inch,6*inch,0,90)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   470
    t.setTextOrigin(4*inch, 5.5*inch)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   471
    t.textLine('canvas.arc(x1, y1, x2, y2, startDeg, extentDeg)')
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   472
    t.textLine('Note that this is an elliptical arc, not just circular!')
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   473
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   474
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   475
    #wedge
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   476
    makesubsection(c, "wedges", 5*inch)
3574
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   477
    c.wedge(inch, 4.5*inch, 3*inch, 3.5*inch, 0, 315)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   478
    t.setTextOrigin(4*inch, 4*inch)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   479
    t.textLine('canvas.wedge(x1, y1, x2, y2, startDeg, extentDeg)')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   480
    t.textLine('Note that this is an elliptical arc, not just circular!')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   481
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   482
    #wedge the other way
3574
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   483
    c.wedge(inch, 3.75*inch, 3*inch, 2.75*inch, 0, -45)
30bd82d9ab4f test_pdfgen_general.py: add roundRect & arc
rgbecker
parents: 3571
diff changeset
   484
    t.setTextOrigin(4*inch, 3*inch)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   485
    t.textLine('Use a negative extent to go clockwise')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   486
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   487
    #circle
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   488
    makesubsection(c, "circles", 3.5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   489
    c.circle(1.5*inch, 2*inch, 0.5 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   490
    c.circle(3*inch, 2*inch, 0.5 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   491
    t.setTextOrigin(4*inch, 2 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   492
    t.textLine('canvas.circle(x, y, radius)')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   493
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   494
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   495
    c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   496
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   497
##############################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   498
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   499
# Page 4 - fonts
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   500
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   501
###############################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   502
    framePage(c, "Font Control")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   503
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   504
    c.drawString(inch, 10*inch, 'Listing available fonts...')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   505
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   506
    y = 9.5*inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   507
    for fontname in c.getAvailableFonts():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   508
        c.setFont(fontname,24)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   509
        c.drawString(inch, y, 'This should be %s' % fontname)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   510
        y = y - 28
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   511
    makesubsection(c, "fonts and colors", 4*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   512
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   513
    c.setFont('Times-Roman', 12)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   514
    t = c.beginText(inch, 4*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   515
    t.textLines("""Now we'll look at the color functions and how they interact
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   516
    with the text.  In theory, a word is just a shape; so setFillColorRGB()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   517
    determines most of what you see.  If you specify other text rendering
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   518
    modes, an outline color could be defined by setStrokeColorRGB() too""")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   519
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   520
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   521
    t = c.beginText(inch, 2.75 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   522
    t.setFont('Times-Bold',36)
4179
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   523
    t.setFillColor(green)  #green
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   524
    t.textLine('Green fill, no stroke')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   525
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   526
    #t.setStrokeColorRGB(1,0,0)  #ou can do this in a text object, or the canvas.
4179
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   527
    t.setStrokeColor(red)  #ou can do this in a text object, or the canvas.
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   528
    t.setTextRenderMode(2)   # fill and stroke
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   529
    t.textLine('Green fill, red stroke - yuk!')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   530
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   531
    t.setTextRenderMode(0)   # back to default - fill only
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   532
    t.setFillColorRGB(0,0,0)   #back to default
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   533
    t.setStrokeColorRGB(0,0,0) #ditto
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   534
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   535
    c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   536
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   537
#########################################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   538
#
4179
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   539
#  Page 5 - character spacing
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   540
#
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   541
#########################################################################
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   542
    framePage(c, "Character Spacing")
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   543
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   544
    c.drawString(inch, 10*inch, 'Listing available fonts...')
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   545
    def drawBox(x,y,width,height,color=green,anchor='start'):
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   546
        if anchor=='end':
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   547
            x -= width
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   548
        elif anchor in ('centre','center'):
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   549
            x = 0.5*width
4179
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   550
        c.saveState()
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   551
        c.setDash(2,2)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   552
        c.setStrokeColor(color)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   553
        c.setLineWidth(0.01)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   554
        c.rect(x,y,width,height,fill=0,stroke=1)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   555
        c.restoreState()
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   556
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   557
    y = 9.5*inch
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   558
    x = 72
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   559
    def drawString(x,y, s,fontName,fontSize,cs=1,anchor='start'):
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   560
        c.setFont(fontName, fontSize)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   561
        y -= 12
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   562
        w = c.stringWidth(s)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   563
        func = c.drawString if anchor=='start' else c.drawRightString if anchor=='end' else c.drawCentredString
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   564
        func(x, y, s)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   565
        drawBox(x,y,w,10,anchor=anchor)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   566
        y -= 12
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   567
        func(x, y, s,charSpace=cs)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   568
        n = len(s) - 1
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   569
        drawBox(x,y,w+n,10,anchor=anchor)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   570
        y -= 12
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   571
        return x,y
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   572
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   573
    x,y = drawString(x,y,u'Hello World','Helvetica',10)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   574
    x,y = drawString(x,y,u'M','Helvetica',10)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   575
    x,y = drawString(x,y,u'MM','Helvetica',10)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   576
    x,y = drawString(x,y,u'M M','Helvetica',10)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   577
    x,y = drawString(x,y,u'M    M','Helvetica',10)
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   578
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   579
    x = 3*72
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   580
    y = 9.5*inch
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   581
    x,y = drawString(x,y,u'Hello World','Helvetica',10,anchor='end')
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   582
    x,y = drawString(x,y,u'M','Helvetica',10,anchor='end')
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   583
    x,y = drawString(x,y,u'MM','Helvetica',10,anchor='end')
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   584
    x,y = drawString(x,y,u'M M','Helvetica',10,anchor='end')
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   585
    x,y = drawString(x,y,u'M    M','Helvetica',10,anchor='end')
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   586
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   587
    c.showPage()
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   588
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   589
#########################################################################
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   590
#
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   591
#  Page 6 - coord transforms
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   592
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   593
#########################################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   594
    framePage(c, "Coordinate Transforms")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   595
    c.setFont('Times-Roman', 12)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   596
    t = c.beginText(inch, 10 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   597
    t.textLines("""This shows coordinate transformations.  We draw a set of axes,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   598
    moving down the page and transforming space before each one.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   599
    You can use saveState() and restoreState() to unroll transformations.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   600
    Note that functions which track the text cursor give the cursor position
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   601
    in the current coordinate system; so if you set up a 6 inch high frame
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   602
    2 inches down the page to draw text in, and move the origin to its top
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   603
    left, you should stop writing text after six inches and not eight.""")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   604
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   605
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   606
    drawAxes(c, "0.  at origin")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   607
    c.addLiteral('%about to translate space')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   608
    c.translate(2*inch, 7 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   609
    drawAxes(c, '1. translate near top of page')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   610
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   611
    c.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   612
    c.translate(1*inch, -2 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   613
    drawAxes(c, '2. down 2 inches, across 1')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   614
    c.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   615
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   616
    c.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   617
    c.translate(0, -3 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   618
    c.scale(2, -1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   619
    drawAxes(c, '3. down 3 from top, scale (2, -1)')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   620
    c.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   621
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   622
    c.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   623
    c.translate(0, -5 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   624
    c.rotate(-30)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   625
    drawAxes(c, "4. down 5, rotate 30' anticlockwise")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   626
    c.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   627
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   628
    c.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   629
    c.translate(3 * inch, -5 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   630
    c.skew(0,30)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   631
    drawAxes(c, "5. down 5, 3 across, skew beta 30")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   632
    c.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   633
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   634
    c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   635
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   636
#########################################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   637
#
4179
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   638
#  Page 7 - clipping
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   639
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   640
#########################################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   641
    framePage(c, "Clipping")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   642
    c.setFont('Times-Roman', 12)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   643
    t = c.beginText(inch, 10 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   644
    t.textLines("""This shows clipping at work. We draw a chequerboard of rectangles
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   645
    into a path object, and clip it.  This then forms a mask which limits the region of
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   646
    the page on which one can draw.  This paragraph was drawn after setting the clipping
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   647
    path, and so you should only see part of the text.""")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   648
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   649
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   650
    c.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   651
    #c.setFillColorRGB(0,0,1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   652
    p = c.beginPath()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   653
    #make a chesboard effect, 1 cm squares
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   654
    for i in range(14):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   655
        x0 = (3 + i) * cm
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   656
        for j in range(7):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   657
            y0 = (16 + j) * cm
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   658
            p.rect(x0, y0, 0.85*cm, 0.85*cm)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   659
    c.addLiteral('%Begin clip path')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   660
    c.clipPath(p)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   661
    c.addLiteral('%End clip path')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   662
    t = c.beginText(3 * cm, 22.5 * cm)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   663
    t.textLines("""This shows clipping at work.  We draw a chequerboard of rectangles
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   664
    into a path object, and clip it.  This then forms a mask which limits the region of
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   665
    the page on which one can draw.  This paragraph was drawn after setting the clipping
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   666
    path, and so you should only see part of the text.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   667
        This shows clipping at work.  We draw a chequerboard of rectangles
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   668
    into a path object, and clip it.  This then forms a mask which limits the region of
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   669
    the page on which one can draw.  This paragraph was drawn after setting the clipping
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   670
    path, and so you should only see part of the text.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   671
        This shows clipping at work.  We draw a chequerboard of rectangles
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   672
    into a path object, and clip it.  This then forms a mask which limits the region of
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   673
    the page on which one can draw.  This paragraph was drawn after setting the clipping
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   674
    path, and so you should only see part of the text.""")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   675
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   676
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   677
    c.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   678
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   679
    t = c.beginText(inch, 5 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   680
    t.textLines("""You can also use text as an outline for clipping with the text render mode.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   681
        The API is not particularly clean on this and one has to follow the right sequence;
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   682
        this can be optimized shortly.""")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   683
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   684
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   685
    #first the outline
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   686
    c.saveState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   687
    t = c.beginText(inch, 3.0 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   688
    t.setFont('Helvetica-BoldOblique',108)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   689
    t.setTextRenderMode(5)  #stroke and add to path
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   690
    t.textLine('Python!')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   691
    t.setTextRenderMode(0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   692
    c.drawText(t)    #this will make a clipping mask
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   693
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   694
    #now some small stuff which wil be drawn into the current clip mask
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   695
    t = c.beginText(inch, 4 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   696
    t.setFont('Times-Roman',6)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   697
    t.textLines((('spam ' * 40) + '\n') * 15)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   698
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   699
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   700
    #now reset canvas to get rid of the clipping mask
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   701
    c.restoreState()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   702
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   703
    c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   704
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   705
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   706
#########################################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   707
#
4179
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   708
#  Page 8 - images
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   709
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   710
#########################################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   711
    framePage(c, "Images")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   712
    c.setFont('Times-Roman', 12)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   713
    t = c.beginText(inch, 10 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   714
    if not haveImages:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   715
        c.drawString(inch, 11*inch,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   716
                     "Python or Java Imaging Library not found! Below you see rectangles instead of images.")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   717
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   718
    t.textLines("""PDFgen uses the Python Imaging Library (or, under Jython, java.awt.image and javax.imageio)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   719
        to process a very wide variety of image formats.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   720
        This page shows image capabilities.  If I've done things right, the bitmap should have
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   721
        its bottom left corner aligned with the crosshairs.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   722
        There are two methods for drawing images.  The recommended use is to call drawImage.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   723
        This produces the smallest PDFs and the fastest generation times as each image's binary data is
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   724
        only embedded once in the file.  Also you can use advanced features like transparency masks.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   725
        You can also use drawInlineImage, which puts images in the page stream directly.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   726
        This is slightly faster for Acrobat to render or for very small images, but wastes
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   727
        space if you use images more than once.""")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   728
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   729
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   730
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   731
    if haveImages:
2987
711910106e3a tests: more changes to testsFolder and friends
rgbecker
parents: 2984
diff changeset
   732
        from reportlab.lib.testutils import testsFolder
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
   733
        gif = os.path.join(testsFolder,'pythonpowered.gif')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   734
        c.drawInlineImage(gif,2*inch, 7*inch)
3927
fefdbfd0d800 add greyscale image and tests thereof
robin
parents: 3849
diff changeset
   735
        c.drawInlineImage(os.path.join(testsFolder,'pythonpowered-gs.gif'),4*inch, 7.5*inch)
4082
6aa0842ec65b pdfimages.py: fix bug reported by hetsch@bitbucket
robin
parents: 4067
diff changeset
   736
        tif = os.path.join(testsFolder,'test-cross.tiff')   #example of a mode '1' image
6aa0842ec65b pdfimages.py: fix bug reported by hetsch@bitbucket
robin
parents: 4067
diff changeset
   737
        c.drawInlineImage(tif,1*inch, 1*inch)
6aa0842ec65b pdfimages.py: fix bug reported by hetsch@bitbucket
robin
parents: 4067
diff changeset
   738
        from reportlab.lib.utils import Image as PilImage
6aa0842ec65b pdfimages.py: fix bug reported by hetsch@bitbucket
robin
parents: 4067
diff changeset
   739
        if PilImage:
6aa0842ec65b pdfimages.py: fix bug reported by hetsch@bitbucket
robin
parents: 4067
diff changeset
   740
            c.drawInlineImage(PilImage.open(tif),1.25*inch, 1*inch)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   741
    else:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   742
        c.rect(2*inch, 7*inch, 110, 44)
3927
fefdbfd0d800 add greyscale image and tests thereof
robin
parents: 3849
diff changeset
   743
        c.rect(4*inch, 7*inch, 110, 44)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   744
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   745
    c.line(1.5*inch, 7*inch, 4*inch, 7*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   746
    c.line(2*inch, 6.5*inch, 2*inch, 8*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   747
    c.drawString(4.5 * inch, 7.25*inch, 'inline image drawn at natural size')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   748
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   749
    if haveImages:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   750
        c.drawInlineImage(gif,2*inch, 5*inch, inch, inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   751
    else:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   752
        c.rect(2*inch, 5*inch, inch, inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   753
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   754
    c.line(1.5*inch, 5*inch, 4*inch, 5*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   755
    c.line(2*inch, 4.5*inch, 2*inch, 6*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   756
    c.drawString(4.5 * inch, 5.25*inch, 'inline image distorted to fit box')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   757
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   758
    c.drawString(1.5 * inch, 4*inch, 'Image XObjects can be defined once in the file and drawn many times.')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   759
    c.drawString(1.5 * inch, 3.75*inch, 'This results in faster generation and much smaller files.')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   760
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   761
    for i in range(5):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   762
        if haveImages:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   763
            (w, h) = c.drawImage(gif, (1.5 + i)*inch, 3*inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   764
        else:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   765
            (w, h) = (144, 10)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   766
            c.rect((1.5 + i)*inch, 3*inch, 110, 44)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   767
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   768
    myMask = [254,255,222,223,0,1]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   769
    c.drawString(1.5 * inch, 2.5*inch, "The optional 'mask' parameter lets you define transparent colors. We used a color picker")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   770
    c.drawString(1.5 * inch, 2.3*inch, "to determine that the yellow in the image above is RGB=(225,223,0).  We then define a mask")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   771
    c.drawString(1.5 * inch, 2.1*inch, "spanning these RGB values:  %s.  The background vanishes!!" % myMask)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   772
    c.drawString(2.5*inch, 1.2*inch, 'This would normally be obscured')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   773
    if haveImages:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   774
        c.drawImage(gif, 1*inch, 1.2*inch, w, h, mask=myMask)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   775
        c.drawImage(gif, 3*inch, 1.2*inch, w, h, mask='auto')
4086
7df61e325601 utils.py: ImageReader handle png indexed with transparency specially
robin
parents: 4084
diff changeset
   776
        c.drawImage(os.path.join(testsFolder,'test-rgba.png'),5*inch,1.2*inch,width=10,height=10,mask='auto')
7df61e325601 utils.py: ImageReader handle png indexed with transparency specially
robin
parents: 4084
diff changeset
   777
        c.drawImage(os.path.join(testsFolder,'test-indexed.png'),5.5*inch,1.2*inch,width=10,height=10,mask='auto')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   778
    else:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   779
        c.rect(1*inch, 1.2*inch, w, h)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   780
        c.rect(3*inch, 1.2*inch, w, h)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   781
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   782
    c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   783
    c.drawString(1*inch, 10.25*inch, "For rgba type images we can use the alpha channel if we set mask='auto'.")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   784
    c.drawString(1*inch, 10.25*inch-14.4, "The first image is solid red with variable alpha.")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   785
    c.drawString(1*inch, 10.25*inch-2*14.4, "The second image is white alpha=0% to purple=100%")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   786
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   787
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3680
diff changeset
   788
    for i in range(8):
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   789
        c.drawString(1*inch,8*inch+i*14.4,"mask=None   Line %d"%i)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   790
        c.drawString(3*inch,8*inch+i*14.4,"mask='auto' Line %d"%i)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   791
        c.drawString(1*inch,6*inch+i*14.4,"mask=None   Line %d"%i)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   792
        c.drawString(3*inch,6*inch+i*14.4,"mask='auto' Line %d"%i)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   793
    w = 100
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   794
    h = 75
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   795
    c.rect(1*inch, 8+14.4*inch, w, h)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   796
    c.rect(3*inch, 8+14.4*inch, w, h)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   797
    c.rect(1*inch, 6+14.4*inch, w, h)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   798
    c.rect(3*inch, 6+14.4*inch, w, h)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   799
    if haveImages:
2987
711910106e3a tests: more changes to testsFolder and friends
rgbecker
parents: 2984
diff changeset
   800
        from reportlab.lib.testutils import testsFolder
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
   801
        png = os.path.join(testsFolder,'solid_red_alpha.png')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   802
        c.drawImage(png, 1*inch, 8*inch+14.4, w, h, mask=None)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   803
        c.drawImage(png, 3*inch, 8*inch+14.4, w, h, mask='auto')
2966
c9df63ccabdf reportlab-2.2: major changes to make tests run
rgbecker
parents: 2963
diff changeset
   804
        png = os.path.join(testsFolder,'alpha_test.png')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   805
        c.drawImage(png, 1*inch, 6*inch+14.4, w, h, mask=None)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   806
        c.drawImage(png, 3*inch, 6*inch+14.4, w, h, mask='auto')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   807
    c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   808
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   809
    if haveImages:
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   810
        import shutil
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   811
        c.drawString(1*inch, 10.25*inch, 'This jpeg is actually a gif')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   812
        jpg = outputfile('_i_am_actually_a_gif.jpg')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   813
        shutil.copyfile(gif,jpg)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   814
        c.drawImage(jpg, 1*inch, 9.25*inch, w, h, mask='auto')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   815
        tjpg = os.path.join(os.path.dirname(os.path.dirname(gif)),'docs','images','lj8100.jpg')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   816
        if os.path.isfile(tjpg):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   817
            c.drawString(4*inch, 10.25*inch, 'This gif is actually a jpeg')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   818
            tgif = outputfile(os.path.basename('_i_am_actually_a_jpeg.gif'))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   819
            shutil.copyfile(tjpg,tgif)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   820
            c.drawImage(tgif, 4*inch, 9.25*inch, w, h, mask='auto')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   821
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   822
        c.drawString(inch, 9.0*inch, 'Image positioning tests with preserveAspectRatio')
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   823
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   824
        #preserveAspectRatio test
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   825
        c.drawString(inch, 8.8*inch, 'Both of these should appear within the boxes, vertically centered')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   826
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   827
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   828
        x, y, w, h = inch, 6.75* inch, 2*inch, 2*inch
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   829
        c.rect(x, y, w, h)
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   830
        (w2, h2) = c.drawImage(gif,  #anchor southwest, drawImage
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   831
                    x, y, width=w, height=h,
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   832
                    preserveAspectRatio=True,
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   833
                    anchor='c'
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   834
                    )
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   835
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   836
        #now test drawInlineImage across the page
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   837
        x = 5 * inch
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   838
        c.rect(x, y, w, h)
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   839
        (w2, h2) = c.drawInlineImage(gif,  #anchor southwest, drawInlineImage
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   840
                    x, y, width=w, height=h,
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   841
                    preserveAspectRatio=True,
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   842
                    anchor='c'
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   843
                    )
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   844
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   845
        c.drawString(inch, 5.75*inch,
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   846
        'anchored by respective corners - use both a wide and a tall one as tests')
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   847
        x = 0.25 * inch
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   848
        for anchor in ['nw','n','ne','w','c','e','sw','s','se']:
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   849
            x += 0.75*inch
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   850
            c.rect(x, 5*inch, 0.6*inch, 0.6*inch)
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   851
            c.drawImage(
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   852
                    gif, x, 5*inch,
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   853
                    width=0.6*inch, height=0.6*inch,
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   854
                    preserveAspectRatio=True,
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   855
                    anchor=anchor
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   856
                    )
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   857
            c.drawString(x, 4.9*inch, anchor)
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   858
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   859
        x = 0.25 * inch
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   860
        tall_red = os.path.join(testsFolder,'tall_red.png')
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   861
        for anchor in ['nw','n','ne','w','c','e','sw','s','se']:
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   862
            x += 0.75*inch
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   863
            c.rect(x, 4*inch, 0.6*inch, 0.6*inch)
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   864
            c.drawImage(
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   865
                    tall_red, x, 4*inch,
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
   866
                    width=0.6*inch, height=0.6*inch,
3092
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   867
                    preserveAspectRatio=True,
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   868
                    anchor=anchor
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   869
                    )
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   870
            c.drawString(x, 3.9*inch, anchor)
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   871
83c2b4c1aa4e changed anchor for drawImage
andy
parents: 2987
diff changeset
   872
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   873
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   874
        c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   875
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   876
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   877
#########################################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   878
#
4179
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   879
#  Page 9 - Forms and simple links
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   880
#
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   881
#########################################################################
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   882
    framePage(c, "Forms and Links")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   883
    c.setFont('Times-Roman', 12)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   884
    t = c.beginText(inch, 10 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   885
    t.textLines("""Forms are sequences of text or graphics operations
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   886
      which are stored only once in a PDF file and used as many times
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   887
      as desired.  The blue logo bar to the left is an example of a form
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   888
      in this document.  See the function framePageForm in this demo script
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   889
      for an example of how to use canvas.beginForm(name, ...) ... canvas.endForm().
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   890
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   891
      Documents can also contain cross references where (for example) a rectangle
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   892
      on a page may be bound to a position on another page.  If the user clicks
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   893
      on the rectangle the PDF viewer moves to the bound position on the other
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   894
      page.  There are many other types of annotations and links supported by PDF.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   895
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   896
      For example, there is a bookmark to each page in this document and below
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   897
      is a browsable index that jumps to those pages. In addition we show two
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   898
      URL hyperlinks; for these, you specify a rectangle but must draw the contents
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   899
      or any surrounding rectangle yourself.
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   900
      """)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   901
    c.drawText(t)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   902
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   903
    nentries = len(titlelist)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   904
    xmargin = 3*inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   905
    xmax = 7*inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   906
    ystart = 6.54*inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   907
    ydelta = 0.4*inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   908
    for i in range(nentries):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   909
        yposition = ystart - i*ydelta
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   910
        title = titlelist[i]
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   911
        c.drawString(xmargin, yposition, title)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   912
        c.linkAbsolute(title, title, (xmargin-ydelta/4, yposition-ydelta/4, xmax, yposition+ydelta/2))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   913
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   914
    # test URLs
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   915
    r1 = (inch, 3*inch, 5*inch, 3.25*inch) # this is x1,y1,x2,y2
4179
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   916
    c.linkURL('http://www.reportlab.com/', r1, thickness=1, color=green)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   917
    c.drawString(inch+3, 3*inch+6, 'Hyperlink to www.reportlab.com, with green border')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   918
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   919
    r1 = (inch, 2.5*inch, 5*inch, 2.75*inch) # this is x1,y1,x2,y2
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   920
    c.linkURL('mailto:reportlab-users@egroups.com', r1) #, border=0)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   921
    c.drawString(inch+3, 2.5*inch+6, 'mailto: hyperlink, without border')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   922
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   923
    r1 = (inch, 2*inch, 5*inch, 2.25*inch) # this is x1,y1,x2,y2
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   924
    c.linkURL('http://www.reportlab.com/', r1,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   925
                      thickness=2,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   926
                      dashArray=[2,4],
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   927
                      color=colors.magenta)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   928
    c.drawString(inch+3, 2*inch+6, 'Hyperlink with custom border style')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   929
3849
8ff9b1fb92cd utils.py: fix isUnicode, rename fileName2Utf8-->fileName2FSEnc
robin
parents: 3800
diff changeset
   930
    xpdf = fileName2FSEnc(outputfile('test_hello.pdf').replace('\\','/'))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   931
    link = 'Hard link to %s, with red border' % xpdf
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   932
    r1 = (inch, 1.5*inch, inch+2*3+c.stringWidth(link,c._fontname, c._fontsize), 1.75*inch) # this is x1,y1,x2,y2
4179
30d5db452712 added support for drawString charSpace; version-->3.1.51
robin
parents: 4088
diff changeset
   933
    c.linkURL(xpdf, r1, thickness=1, color=red, kind='GoToR')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   934
    c.drawString(inch+3, 1.5*inch+6, link )
3571
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   935
    c.showPage()
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   936
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   937
    ############# colour gradients
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   938
    title = 'Gradients code contributed by Peter Johnson <johnson.peter@gmail.com>'
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   939
    c.drawString(1*inch,10.8*inch,title)
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   940
    c.addOutlineEntry(title+" section", title, level=0, closed=True)
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   941
    c.bookmarkHorizontalAbsolute(title, 10.8*inch)
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   942
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   943
    c.saveState()
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   944
    p = c.beginPath()
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   945
    p.moveTo(1*inch,2*inch)
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   946
    p.lineTo(1.5*inch,2.5*inch)
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   947
    p.curveTo(2*inch,3*inch,3.0*inch,3*inch,4*inch,2.9*inch)
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   948
    p.lineTo(5.5*inch,2.1*inch)
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   949
    p.close()
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   950
    c.clipPath(p)
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   951
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   952
    # Draw a linear gradient from (0, 2*inch) to (5*inch, 3*inch), from orange to white.
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   953
    # The gradient will extend past the endpoints (so you probably want a clip path in place)
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   954
    c.linearGradient(1*inch, 2*inch, 6*inch, 3*inch, (red, blue))
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   955
    c.restoreState()
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   956
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   957
    # Draw a radial gradient with a radius of 3 inches.
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   958
    # The color starts orange and stays orange until 20% of the radius,
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   959
    # then fades to white at 80%, and ends up green at 3 inches from the center.
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   960
    # Since extend is false, the gradient stops drawing at the edge of the circle.
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   961
    c.radialGradient(4*inch, 6*inch, 3*inch, (red, green, blue), (0.2, 0.8, 1.0), extend=False)
5fbf027dd6c3 test_pdfgen_general.py: add examples of gradients
rgbecker
parents: 3469
diff changeset
   962
    c.showPage()
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   964
    ### now do stuff for the outline
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   965
    #for x in outlinenametree: print x
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   966
    #stop
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3201
diff changeset
   967
    #c.setOutlineNames0(*outlinenametree)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   968
    return c
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   969
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   970
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   971
def run(filename):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   972
    c = makeDocument(filename)
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3680
diff changeset
   973
    c.setAuthor('R\xfcp\xe9rt B\xe8\xe4r')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   974
    c.setTitle('R\xc3\xbcp\xc3\xa9rt B\xc3\xa8\xc3\xa4r\'s Book')
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3680
diff changeset
   975
    c.setCreator('Some Creator')
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3680
diff changeset
   976
    c.setSubject('Some Subject')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   977
    c.save()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   978
    c = makeDocument(filename)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   979
    import os
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   980
    f = os.path.splitext(filename)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   981
    f = open('%sm%s' % (f[0],f[1]),'wb')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   982
    f.write(c.getpdfdata())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   983
    f.close()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   984
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   985
def pageShapes(c):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   986
    """Demonstrates the basic lines and shapes"""
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   987
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   988
    c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   989
    framePage(c, "Basic line and shape routines""")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   990
    c.setTextOrigin(inch, 10 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   991
    c.setFont('Times-Roman', 12)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   992
    c.textLines("""pdfgen provides some basic routines for drawing straight and curved lines,
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   993
    and also for solid shapes.""")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   994
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   995
    y = 9 * inch
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   996
    d = DocBlock()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   997
    d.comment1 = 'Lesson one'
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
   998
    d.code = "canvas.textOut('hello, world')"
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3680
diff changeset
   999
    print(d.code)
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1000
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1001
    d.comment2 = 'Lesson two'
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1002
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1003
    d.draw(c, inch, 9 * inch)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1004
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1005
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1006
class PdfgenTestCase(unittest.TestCase):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1007
    "Make documents with lots of Pdfgen features"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1008
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1009
    def test0(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1010
        "Make a PDFgen document with most graphics features"
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1011
        run(outputfile('test_pdfgen_general.pdf'))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1012
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1013
    def test1(self):
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1014
        c=canvas.Canvas(outputfile('test_pdfgen_obscure.pdf'))
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1015
        c.setViewerPreference('PrintScaling','None')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1016
        c.setViewerPreference('HideToolbar','true')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1017
        c.setViewerPreference('HideMenubar','true')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1018
        c.addPageLabel(0, prefix="Front")
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1019
        c.addPageLabel(1, style='ROMAN_LOWER', start=2)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1020
        c.addPageLabel(8, style='ARABIC')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1021
        # (These are fixes for missing pages)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1022
        c.addPageLabel(11, style='ARABIC',start=6)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1023
        c.addPageLabel(17, style='ARABIC', start=14)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1024
        c.addPageLabel(21, style='ARABIC', start=22)
4195
5abb3b7f93b1 added rl_settings.errorOnDuplicatePageLabelPage & support in PageLabel class for stable sorting
robin
parents: 4179
diff changeset
  1025
        #check that duplicate page start will not cause sort error in python 3.x
5abb3b7f93b1 added rl_settings.errorOnDuplicatePageLabelPage & support in PageLabel class for stable sorting
robin
parents: 4179
diff changeset
  1026
        c.addPageLabel(98, style='ROMAN_LOWER', start=99, prefix='r')
5abb3b7f93b1 added rl_settings.errorOnDuplicatePageLabelPage & support in PageLabel class for stable sorting
robin
parents: 4179
diff changeset
  1027
        c.addPageLabel(98, style='ARABIC', start=99, prefix='A')
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1028
        c.addPageLabel(99, style='LETTERS_UPPER')
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1029
        c.addPageLabel(102, prefix="Back",start=1)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1030
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1031
        # Make some (mostly) empty pages
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3680
diff changeset
  1032
        for i in range(113):
4195
5abb3b7f93b1 added rl_settings.errorOnDuplicatePageLabelPage & support in PageLabel class for stable sorting
robin
parents: 4179
diff changeset
  1033
            c.drawString(100, 100, 'This is page '+str(i))
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1034
            c.showPage()
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1035
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1036
        # Output the PDF
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1037
        c.save()
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1038
3108
1e67b49dbf22 tests: add autocropmarks test & subject value
rgbecker
parents: 3092
diff changeset
  1039
    def test2(self):
3160
3c34cc4aece0 tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents: 3108
diff changeset
  1040
        c=canvas.Canvas('test_pdfgen_autocropmarks.pdf',cropMarks=True)
3108
1e67b49dbf22 tests: add autocropmarks test & subject value
rgbecker
parents: 3092
diff changeset
  1041
        c.saveState()
1e67b49dbf22 tests: add autocropmarks test & subject value
rgbecker
parents: 3092
diff changeset
  1042
        c.setStrokeColor((1,0,0))
1e67b49dbf22 tests: add autocropmarks test & subject value
rgbecker
parents: 3092
diff changeset
  1043
        c.rect(0,0,c._pagesize[0],c._pagesize[1],stroke=1)
1e67b49dbf22 tests: add autocropmarks test & subject value
rgbecker
parents: 3092
diff changeset
  1044
        c.restoreState()
1e67b49dbf22 tests: add autocropmarks test & subject value
rgbecker
parents: 3092
diff changeset
  1045
        c.drawString(72,c._pagesize[1]-72,'Auto Crop Marks')
1e67b49dbf22 tests: add autocropmarks test & subject value
rgbecker
parents: 3092
diff changeset
  1046
        c.showPage()
3160
3c34cc4aece0 tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents: 3108
diff changeset
  1047
        c.saveState()
3c34cc4aece0 tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents: 3108
diff changeset
  1048
        c.setStrokeColor((1,0,0))
3c34cc4aece0 tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents: 3108
diff changeset
  1049
        c.rect(0,0,c._pagesize[0],c._pagesize[1],stroke=1)
3c34cc4aece0 tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents: 3108
diff changeset
  1050
        c.restoreState()
3c34cc4aece0 tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents: 3108
diff changeset
  1051
        c.drawString(72,c._pagesize[1]-72,'Auto Crop Marks Another Page')
3c34cc4aece0 tests: add paraFontSizeHeightOffset check and autocropmarks stuff
rgbecker
parents: 3108
diff changeset
  1052
        c.showPage()
3108
1e67b49dbf22 tests: add autocropmarks test & subject value
rgbecker
parents: 3092
diff changeset
  1053
        c.save()
1e67b49dbf22 tests: add autocropmarks test & subject value
rgbecker
parents: 3092
diff changeset
  1054
3199
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1055
    def test3(self):
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1056
        '''some special properties'''
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1057
        palette = [
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1058
                    colors.CMYKColorSep(0.6,0.34,0,0.1,spotName='625C',density=1),
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1059
                    colors.CMYKColorSep(0.13,0.51,0.87,0.48,spotName='464c',density=1),
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1060
                    ]
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1061
        canv = canvas.Canvas(   'test_pdfgen_general_spots.pdf',
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1062
                        pagesize=(346,102),
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1063
                        )
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1064
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1065
        canv.setLineWidth(1)
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1066
        canv.setStrokeColor(colors.CMYKColor(0,0,0,1))
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1067
        x=10
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1068
        y=10
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1069
        for c in palette:
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1070
            c.density = 1.0
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1071
            canv.setFillColor(c)
3201
2d81dd43d7eb test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents: 3199
diff changeset
  1072
            canv.setFont('Helvetica',20)
3199
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1073
            canv.drawString(x,80,'This is %s' % c.spotName)
3201
2d81dd43d7eb test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents: 3199
diff changeset
  1074
            canv.setFont('Helvetica',6)
3199
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1075
            canv.rect(x,y,50,50,fill=1)
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1076
            canv.setFillColor(c.clone(density=0.5))
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1077
            canv.rect(x+55,y,20,20,fill=1)
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1078
            canv.setFillColor(colors.CMYKColor(0,0,1,0))
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1079
            canv.rect(x+80,y,30,30,fill=1)
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1080
            canv.rect(x+120,y,30,30,fill=1)
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1081
            alpha = c is palette[0] and 1 or 0.5
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1082
            op = c is palette[0] and True or False
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1083
            canv.setFillAlpha(alpha)
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1084
            canv.setFillColor(colors.CMYKColor(1,0,0,0))
3201
2d81dd43d7eb test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents: 3199
diff changeset
  1085
            canv.drawString(x+80+1,y+3,'OP=%d' % int(False))
2d81dd43d7eb test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents: 3199
diff changeset
  1086
            canv.drawString(x+80+1,y+23,'Alpha=%.1f' % alpha)
3199
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1087
            canv.rect(x+90,y+10,10,10,fill=1)
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1088
            canv.setFillOverprint(op)
3201
2d81dd43d7eb test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents: 3199
diff changeset
  1089
            canv.drawString(x+120+1,y+3,'OP=%d' % int(op))
2d81dd43d7eb test_pdfgen_general.py: add in some OP/Alpha strings
rgbecker
parents: 3199
diff changeset
  1090
            canv.drawString(x+120+1,y+23,'Alpha=%.1f' % alpha)
3199
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1091
            canv.rect(x+130,y+10,10,10,fill=1)
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1092
            canv.setFillAlpha(1)
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1093
            canv.setFillOverprint(False)
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1094
            x += canv._pagesize[0]*0.5
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1095
        canv.showPage()
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1096
        canv.save()
d490c9f4590d reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3160
diff changeset
  1097
3447
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1098
    def test4(self):
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1099
        sc = colors.CMYKColorSep
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1100
        rgb = ['red','green','blue', 'black']
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1101
        cmykb = [(0,0,0,1)]
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1102
        cmyk = [(1,0,0,0),(0,1,0,0),(0,0,1,0)]+cmykb
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1103
        seps = [sc(1,1,0,0,spotName='sep0'),sc(0,1,1,0,spotName='sep1')]
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1104
        sepb = [sc(0,0,0,1,spotName='sepb')]
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1105
        #these should all work
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1106
        trySomeColors(rgb+cmyk+seps)
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1107
        trySomeColors(rgb,'rgb')
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1108
        trySomeColors(cmyk,'cmyk')
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1109
        trySomeColors(seps+cmyk,'sep_cmyk')
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1110
        trySomeColors(seps+sepb,'sep')  #we need a fake black for now
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1111
        trySomeColors(seps+['black']+cmykb,'sep_black')
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1112
        self.assertRaises(ValueError,trySomeColors,rgb+cmyk+seps,'rgb')
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1113
        self.assertRaises(ValueError,trySomeColors,rgb+cmyk,'rgb')
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1114
        self.assertRaises(ValueError,trySomeColors,rgb+seps,'rgb')
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1115
        trySomeColors(rgb+sepb,'rgb')   #should work because blacks are convertible
3447
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1116
        trySomeColors(rgb+cmykb,'rgb')
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1117
        self.assertRaises(ValueError,trySomeColors,cmyk+rgb+seps,'cmyk')
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1118
        trySomeColors(cmyk+['black']+seps,'cmyk')   #OK because black & seps are convertible
3464
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1119
4084
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1120
    def test5(self,uopw=None):
3464
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1121
        from reportlab.lib.pagesizes import A4,LETTER
4084
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1122
        if uopw:
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1123
            from reportlab.lib import pdfencrypt
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1124
            encrypt = pdfencrypt.StandardEncryption(uopw[0], uopw[1])
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1125
            encrypt.setAllPermissions(0)
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1126
            encrypt.canPrint = 1
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1127
            canv = canvas.Canvas(outputfile('test_pdfgen_general_page_sizes_encrypted.pdf'),pagesize=A4)
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1128
            canv._doc.encrypt = encrypt
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1129
        else:
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1130
            canv = canvas.Canvas(outputfile('test_pdfgen_general_page_sizes.pdf'),pagesize=A4)
3464
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1131
        canv.setFont('Helvetica',10)
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1132
        S = A4
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1133
        canv.drawString(0,S[1]-10,'Top Left=(%s,%s) Page Size=%s x %s' % (0,S[1],S[0],S[1]))
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1134
        canv.drawCentredString(0.5*S[0],0.5*S[1],'Center =(%s,%s) Page Size=%s x %s' % (0.5*S[0],0.5*S[1],S[0],S[1]))
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1135
        canv.drawRightString(S[0],2,'Bottom Right=(%s,%s) Page Size=%s x %s' % (S[0],0,S[0],S[1]))
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1136
        canv.showPage()
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1137
        S = LETTER
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1138
        canv.setPageSize(S)
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1139
        canv.drawString(0,S[1]-10,'Top Left=(%s,%s) Page Size=%s x %s' % (0,S[1],S[0],S[1]))
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1140
        canv.drawCentredString(0.5*S[0],0.5*S[1],'Center =(%s,%s) Page Size=%s x %s' % (0.5*S[0],0.5*S[1],S[0],S[1]))
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1141
        canv.drawRightString(S[0],2,'Bottom Right=(%s,%s) Page Size=%s x %s' % (S[0],0,S[0],S[1]))
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1142
        canv.showPage()
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1143
        S = A4
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1144
        canv.setPageSize(S)
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1145
        canv.setPageRotation(180)
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1146
        canv.drawString(0,S[1]-10,'Top Left=(%s,%s) Page Size=%s x %s' % (0,S[1],S[0],S[1]))
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1147
        canv.drawCentredString(0.5*S[0],0.5*S[1],'Center =(%s,%s) Page Size=%s x %s' % (0.5*S[0],0.5*S[1],S[0],S[1]))
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1148
        canv.drawRightString(S[0],2,'Bottom Right=(%s,%s) Page Size=%s x %s' % (S[0],0,S[0],S[1]))
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1149
        canv.showPage()
3469
be341d58c699 test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents: 3464
diff changeset
  1150
        S = A4[1],A4[0]
be341d58c699 test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents: 3464
diff changeset
  1151
        canv.setPageSize(S)
be341d58c699 test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents: 3464
diff changeset
  1152
        canv.setPageRotation(0)
be341d58c699 test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents: 3464
diff changeset
  1153
        canv.drawString(0,S[1]-30,'Top Left=(%s,%s) Page Size=%s x %s' % (0,S[1],S[0],S[1]))
be341d58c699 test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents: 3464
diff changeset
  1154
        canv.drawCentredString(0.5*S[0],0.5*S[1],'Center =(%s,%s) Page Size=%s x %s' % (0.5*S[0],0.5*S[1],S[0],S[1]))
be341d58c699 test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents: 3464
diff changeset
  1155
        canv.drawRightString(S[0],32,'Bottom Right=(%s,%s) Page Size=%s x %s' % (S[0],0,S[0],S[1]))
be341d58c699 test_pdfgen_general.py: add another sample page with rotated text
rgbecker
parents: 3464
diff changeset
  1156
        canv.showPage()
3464
635569457733 test_pdfgen_general.py: add simple test of page size and rotation changing
rgbecker
parents: 3456
diff changeset
  1157
        canv.save()
3447
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1158
4084
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1159
    def test6(self):
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1160
        self.test5(('User','Password'))
746fe2e51840 test_pdfgen_general.py: added multi-page encryption test
robin
parents: 4067
diff changeset
  1161
3667
ab79820991f9 added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents: 3617
diff changeset
  1162
    def testMultipleSavesOk(self):
ab79820991f9 added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents: 3617
diff changeset
  1163
        c=canvas.Canvas(outputfile('test_pdfgen_savetwice.pdf'))
ab79820991f9 added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents: 3617
diff changeset
  1164
        c.drawString(100, 700, 'Hello. This was saved twice')
ab79820991f9 added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents: 3617
diff changeset
  1165
        c.showPage()
ab79820991f9 added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents: 3617
diff changeset
  1166
ab79820991f9 added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents: 3617
diff changeset
  1167
        # Output the PDF
ab79820991f9 added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents: 3617
diff changeset
  1168
        stuff = c.getpdfdata()
ab79820991f9 added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents: 3617
diff changeset
  1169
        #multiple calls to save / getpdfdata used to cause errors
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1170
        stuff = c.getpdfdata()
3667
ab79820991f9 added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents: 3617
diff changeset
  1171
4212
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1172
    def testBoxes(self):
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1173
        c=canvas.Canvas(outputfile('test_pdfgen_boxes.pdf'))
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1174
        w,h = c._pagesize
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1175
        c.setCropBox((0.1,0.1,w-0.2,h-0.2))
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1176
        c.setBleedBox((-0.1,-0.1,w+0.2,h+0.2))
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1177
        c.setArtBox((0.2,0.2,w-0.4,h-0.4))
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1178
        c.setTrimBox((0.01,0.01,w-0.02,h-0.02))
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1179
        c.drawString(100, 700, 'Hello World!')
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1180
        c.showPage()
c903815560d1 add support for crop/art/trim/bleed boxes; version --> 3.2.2
robin
parents: 4195
diff changeset
  1181
        c.save()
3667
ab79820991f9 added test case for multiple save error
Andy Robinson <andy@reportlab.com>
parents: 3617
diff changeset
  1182
3447
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1183
def trySomeColors(C,enforceColorSpace=None):
3849
8ff9b1fb92cd utils.py: fix isUnicode, rename fileName2Utf8-->fileName2FSEnc
robin
parents: 3800
diff changeset
  1184
    from reportlab.lib.utils import getBytesIO
8ff9b1fb92cd utils.py: fix isUnicode, rename fileName2Utf8-->fileName2FSEnc
robin
parents: 3800
diff changeset
  1185
    out=getBytesIO()
3447
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1186
    canv = canvas.Canvas(out,enforceColorSpace=enforceColorSpace)
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1187
    canv.setFont('Helvetica',10)
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1188
    x = 0
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1189
    y = 0
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1190
    w,h = canv._pagesize
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1191
    for c in C:
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1192
        if y+10>h:
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1193
            y = 0
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1194
            x += 10
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1195
        canv.setFillColor(c)
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1196
        canv.rect(x,y,10,10,fill=1,stroke=0)
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1197
        y += 10
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1198
    canv.showPage()
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1199
    canv.save()
70d7f0450df0 test_pdfgen_general.py: add enforceColorSpace tests
rgbecker
parents: 3326
diff changeset
  1200
2963
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1201
def makeSuite():
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1202
    return makeSuiteForClasses(PdfgenTestCase)
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1203
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1204
#noruntests
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1205
if __name__ == "__main__":
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1206
    unittest.TextTestRunner().run(makeSuite())
c414c0ab69e7 reportlab-2.2: first stage of major re-org
rgbecker
parents:
diff changeset
  1207
    printLocation()