src/reportlab/pdfgen/textobject.py
author rptlab
Tue, 30 Apr 2013 14:28:14 +0100
branchpy33
changeset 3723 99aa837b6703
parent 3721 0c93dd8ff567
child 3731 b233dd0577ff
permissions -rwxr-xr-x
second stage of port to Python 3.3; working hello world
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3617
ae5744e97c42 reportlab: copyright date changes
robin
parents: 3572
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2012
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 455
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2326
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/pdfgen/textobject.py
2317
bab1eeccbe66 Added support for mac_roman encoding and changed to always convert unicode strings to font encoding
mike
parents: 2315
diff changeset
     4
__version__=''' $Id$ '''
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
     5
__doc__="""
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     6
PDFTextObject is an efficient way to add text to a Canvas. Do not
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     7
instantiate directly, obtain one from the Canvas instead.
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     8
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     9
Progress Reports:
3030
8bc2e0f9c080 docstring fixes for pdfgen
damian
parents: 2983
diff changeset
    10
8.83, 2000-01-13, gmcm: created from pdfgen.py
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    11
"""
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    12
import sys
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    13
import string
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    14
from types import *
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    15
from reportlab.lib.colors import Color, CMYKColor, CMYKColorSep, toColor, black, white, _CMYK_black, _CMYK_white
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    16
from reportlab.lib.utils import fp_str, isStrType, isPython3
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
    17
from reportlab.pdfbase import pdfmetrics
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    18
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    19
class _PDFColorSetter:
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    20
    '''Abstracts the color setting operations; used in Canvas and Textobject
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    21
    asseumes we have a _code object'''
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    22
    def _checkSeparation(self,cmyk):
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    23
        if isinstance(cmyk,CMYKColorSep):
3226
cd06c1172678 pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents: 3198
diff changeset
    24
            name,sname = self._doc.addColor(cmyk)
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    25
            if name not in self._colorsUsed:
3226
cd06c1172678 pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents: 3198
diff changeset
    26
                self._colorsUsed[name] = sname
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    27
            return name
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    28
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    29
    #if this is set to a callable(color) --> color it can be used to check color setting
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    30
    #see eg _enforceCMYK/_enforceRGB
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    31
    _enforceColorSpace = None
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    32
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    33
    def setFillColorCMYK(self, c, m, y, k, alpha=None):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    34
         """set the fill color useing negative color values
3030
8bc2e0f9c080 docstring fixes for pdfgen
damian
parents: 2983
diff changeset
    35
         (cyan, magenta, yellow and darkness value).
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    36
         Takes 4 arguments between 0.0 and 1.0"""
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    37
         self.setFillColor((c,m,y,k),alpha=alpha)
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 538
diff changeset
    38
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    39
    def setStrokeColorCMYK(self, c, m, y, k, alpha=None):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    40
         """set the stroke color useing negative color values
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    41
            (cyan, magenta, yellow and darkness value).
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    42
            Takes 4 arguments between 0.0 and 1.0"""
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    43
         self.setStrokeColor((c,m,y,k),alpha=alpha)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    44
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    45
    def setFillColorRGB(self, r, g, b, alpha=None):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    46
        """Set the fill color using positive color description
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    47
           (Red,Green,Blue).  Takes 3 arguments between 0.0 and 1.0"""
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    48
        self.setFillColor((r,g,b),alpha=alpha)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    49
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    50
    def setStrokeColorRGB(self, r, g, b, alpha=None):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    51
        """Set the stroke color using positive color description
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    52
           (Red,Green,Blue).  Takes 3 arguments between 0.0 and 1.0"""
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    53
        self.setStrokeColor((r,g,b),alpha=alpha)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    54
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    55
    def setFillColor(self, aColor, alpha=None):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    56
        """Takes a color object, allowing colors to be referred to by name"""
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    57
        if self._enforceColorSpace:
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    58
            aColor = self._enforceColorSpace(aColor)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    59
        if isinstance(aColor, CMYKColor):
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    60
            d = aColor.density
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    61
            c,m,y,k = (d*aColor.cyan, d*aColor.magenta, d*aColor.yellow, d*aColor.black)
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3226
diff changeset
    62
            self._fillColorObj = aColor
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    63
            name = self._checkSeparation(aColor)
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    64
            if name:
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    65
                self._code.append('/%s cs %s scn' % (name,fp_str(d)))
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    66
            else:
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    67
                self._code.append('%s k' % fp_str(c, m, y, k))
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    68
        elif isinstance(aColor, Color):
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    69
            rgb = (aColor.red, aColor.green, aColor.blue)
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3226
diff changeset
    70
            self._fillColorObj = aColor
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    71
            self._code.append('%s rg' % fp_str(rgb) )
3178
92424aba6ad1 textobject.py: fix buglet and make chanegs inspired by henning.vonbargen@arcor.de
rgbecker
parents: 3030
diff changeset
    72
        elif isinstance(aColor,(tuple,list)):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    73
            l = len(aColor)
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    74
            if l==3:
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3226
diff changeset
    75
                self._fillColorObj = aColor
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    76
                self._code.append('%s rg' % fp_str(aColor) )
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    77
            elif l==4:
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    78
                self._fillColorObj = aColor
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    79
                self._code.append('%s k' % fp_str(aColor))
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    80
            else:
2915
f2c1f8ef3b51 reportlab: obsoletisms removed
rgbecker
parents: 2678
diff changeset
    81
                raise ValueError('Unknown color %r' % aColor)
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    82
        elif isStrType(aColor):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    83
            self.setFillColor(toColor(aColor))
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    84
        else:
2915
f2c1f8ef3b51 reportlab: obsoletisms removed
rgbecker
parents: 2678
diff changeset
    85
            raise ValueError('Unknown color %r' % aColor)
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    86
        if alpha is not None:
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    87
            self.setFillAlpha(alpha)
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    88
        elif getattr(aColor, 'alpha', None) is not None:
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    89
            self.setFillAlpha(aColor.alpha)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    90
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
    91
    def setStrokeColor(self, aColor, alpha=None):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    92
        """Takes a color object, allowing colors to be referred to by name"""
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    93
        if self._enforceColorSpace:
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
    94
            aColor = self._enforceColorSpace(aColor)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    95
        if isinstance(aColor, CMYKColor):
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    96
            d = aColor.density
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
    97
            c,m,y,k = (d*aColor.cyan, d*aColor.magenta, d*aColor.yellow, d*aColor.black)
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3226
diff changeset
    98
            self._strokeColorObj = aColor
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
    99
            name = self._checkSeparation(aColor)
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   100
            if name:
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   101
                self._code.append('/%s CS %s SCN' % (name,fp_str(d)))
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   102
            else:
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   103
                self._code.append('%s K' % fp_str(c, m, y, k))
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   104
        elif isinstance(aColor, Color):
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   105
            rgb = (aColor.red, aColor.green, aColor.blue)
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3226
diff changeset
   106
            self._strokeColorObj = aColor
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   107
            self._code.append('%s RG' % fp_str(rgb) )
3178
92424aba6ad1 textobject.py: fix buglet and make chanegs inspired by henning.vonbargen@arcor.de
rgbecker
parents: 3030
diff changeset
   108
        elif isinstance(aColor,(tuple,list)):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   109
            l = len(aColor)
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   110
            if l==3:
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3226
diff changeset
   111
                self._strokeColorObj = aColor
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   112
                self._code.append('%s RG' % fp_str(aColor) )
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   113
            elif l==4:
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
   114
                self._fillColorObj = aColor
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
   115
                self._code.append('%s K' % fp_str(aColor))
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   116
            else:
2915
f2c1f8ef3b51 reportlab: obsoletisms removed
rgbecker
parents: 2678
diff changeset
   117
                raise ValueError('Unknown color %r' % aColor)
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   118
        elif isStrType(aColor):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   119
            self.setStrokeColor(toColor(aColor))
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   120
        else:
2915
f2c1f8ef3b51 reportlab: obsoletisms removed
rgbecker
parents: 2678
diff changeset
   121
            raise ValueError('Unknown color %r' % aColor)
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   122
        if alpha is not None:
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   123
            self.setStrokeAlpha(alpha)
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   124
        elif getattr(aColor, 'alpha', None) is not None:
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   125
            self.setStrokeAlpha(aColor.alpha)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   126
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   127
    def setFillGray(self, gray, alpha=None):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   128
        """Sets the gray level; 0.0=black, 1.0=white"""
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3226
diff changeset
   129
        self._fillColorObj = (gray, gray, gray)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   130
        self._code.append('%s g' % fp_str(gray))
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   131
        if alpha is not None:
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   132
            self.setFillAlpha(alpha)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   133
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   134
    def setStrokeGray(self, gray, alpha=None):
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   135
        """Sets the gray level; 0.0=black, 1.0=white"""
3254
dd9042134279 canvas.py, textobject.py: merge _*ColorRGB/CMYK attributes into ColorObj
rgbecker
parents: 3226
diff changeset
   136
        self._strokeColorObj = (gray, gray, gray)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   137
        self._code.append('%s G' % fp_str(gray))
3277
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   138
        if alpha is not None:
8fe70e71c2db reportlab: add alpha property to colours, patch contributed by Keven D Smith <Kevin.Smith@sixquickrun.com>
rgbecker
parents: 3254
diff changeset
   139
            self.setFillAlpha(alpha)
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   140
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   141
    def setStrokeAlpha(self,a):
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   142
        if not (isinstance(a,(float,int)) and 0<=a<=1):
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   143
            raise ValueError('setStrokeAlpha invalid value %r' % a)
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   144
        getattr(self,'_setStrokeAlpha',lambda x: None)(a)
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   145
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   146
    def setFillAlpha(self,a):
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   147
        if not (isinstance(a,(float,int)) and 0<=a<=1):
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   148
            raise ValueError('setFillAlpha invalid value %r' % a)
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   149
        getattr(self,'_setFillAlpha',lambda x: None)(a)
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   150
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   151
    def setStrokeOverprint(self,a):
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   152
        getattr(self,'_setStrokeOverprint',lambda x: None)(a)
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   153
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   154
    def setFillOverprint(self,a):
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   155
        getattr(self,'_setFillOverprint',lambda x: None)(a)
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   156
3417
6b2786216805 graphics: add support for overprintMask
rgbecker
parents: 3277
diff changeset
   157
    def setOverprintMask(self,a):
6b2786216805 graphics: add support for overprintMask
rgbecker
parents: 3277
diff changeset
   158
        getattr(self,'_setOverprintMask',lambda x: None)(a)
6b2786216805 graphics: add support for overprintMask
rgbecker
parents: 3277
diff changeset
   159
2539
e2b54035d5ec pdfgen: abstract color setting ops to textobject._PDFColorSetter
rgbecker
parents: 2341
diff changeset
   160
class PDFTextObject(_PDFColorSetter):
412
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
   161
    """PDF logically separates text and graphics drawing; text
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
   162
    operations need to be bracketed between BT (Begin text) and
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
   163
    ET operators. This class ensures text operations are
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
   164
    properly encapusalted. Ask the canvas for a text object
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   165
    with beginText(x, y).  Do not construct one directly.
412
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
   166
    Do not use multiple text objects in parallel; PDF is
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
   167
    not multi-threaded!
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   168
412
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
   169
    It keeps track of x and y coordinates relative to its origin."""
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 538
diff changeset
   170
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   171
    def __init__(self, canvas, x=0,y=0):
19
5b4da60fa383 trailing text/Odyssey fix
rgbecker
parents: 16
diff changeset
   172
        self._code = ['BT']    #no point in [] then append RGB
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   173
        self._canvas = canvas  #canvas sets this so it has access to size info
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   174
        self._fontname = self._canvas._fontname
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   175
        self._fontsize = self._canvas._fontsize
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   176
        self._leading = self._canvas._leading
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   177
        self._doc = self._canvas._doc
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3178
diff changeset
   178
        self._colorsUsed = self._canvas._colorsUsed
3443
64d1fba94908 reportlab: added preliminary support for enforcing color spaces
rgbecker
parents: 3417
diff changeset
   179
        self._enforceColorSpace = getattr(canvas,'_enforceColorSpace',None)
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   180
        font = pdfmetrics.getFont(self._fontname)
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   181
        self._curSubset = -1
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   182
        self.setTextOrigin(x, y)
3572
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   183
        self._textRenderMode = 0
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   184
        self._clipping = 0
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   185
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   186
    def getCode(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   187
        "pack onto one line; used internally"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   188
        self._code.append('ET')
3572
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   189
        if self._clipping:
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   190
            self._code.append('%d Tr' % (self._textRenderMode^4))
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   191
        return ' '.join(self._code)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   192
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   193
    def setTextOrigin(self, x, y):
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   194
        if self._canvas.bottomup:
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   195
            self._code.append('1 0 0 1 %s Tm' % fp_str(x, y)) #bottom up
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   196
        else:
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   197
            self._code.append('1 0 0 -1 %s Tm' % fp_str(x, y))  #top down
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   198
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   199
        # The current cursor position is at the text origin
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   200
        self._x0 = self._x = x
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   201
        self._y0 = self._y = y
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   202
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   203
    def setTextTransform(self, a, b, c, d, e, f):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   204
        "Like setTextOrigin, but does rotation, scaling etc."
409
51f898a4dd2d Bernhard herzog inspired fixes
rgbecker
parents: 114
diff changeset
   205
        if not self._canvas.bottomup:
51f898a4dd2d Bernhard herzog inspired fixes
rgbecker
parents: 114
diff changeset
   206
            c = -c    #reverse bottom row of the 2D Transform
51f898a4dd2d Bernhard herzog inspired fixes
rgbecker
parents: 114
diff changeset
   207
            d = -d
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   208
        self._code.append('%s Tm' % fp_str(a, b, c, d, e, f))
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2575
diff changeset
   209
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   210
        # The current cursor position is at the text origin Note that
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   211
        # we aren't keeping track of all the transform on these
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   212
        # coordinates: they are relative to the rotations/sheers
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   213
        # defined in the matrix.
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   214
        self._x0 = self._x = e
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   215
        self._y0 = self._y = f
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   216
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   217
    def moveCursor(self, dx, dy):
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   218
        """Starts a new line at an offset dx,dy from the start of the
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   219
        current line. This does not move the cursor relative to the
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   220
        current position, and it changes the current offset of every
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   221
        future line drawn (i.e. if you next do a textLine() call, it
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   222
        will move the cursor to a position one line lower than the
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   223
        position specificied in this call.  """
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   224
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   225
        # Check if we have a previous move cursor call, and combine
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   226
        # them if possible.
455
3eb389710433 Improved optimisation checks
rgbecker
parents: 454
diff changeset
   227
        if self._code and self._code[-1][-3:]==' Td':
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   228
            L = self._code[-1].split()
434
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
   229
            if len(L)==3:
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
   230
                del self._code[-1]
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
   231
            else:
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   232
                self._code[-1] = ''.join(L[:-4])
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   233
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   234
            # Work out the last movement
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   235
            lastDx = float(L[-3])
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   236
            lastDy = float(L[-2])
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   237
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   238
            # Combine the two movement
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   239
            dx += lastDx
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   240
            dy -= lastDy
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   241
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   242
            # We will soon add the movement to the line origin, so if
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   243
            # we've already done this for lastDx, lastDy, remove it
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   244
            # first (so it will be right when added back again).
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   245
            self._x0 -= lastDx
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   246
            self._y0 -= lastDy
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   247
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   248
        # Output the move text cursor call.
538
c7253e1793af Layout fixes; Td was absolute not relative to the beginning of the line
rgbecker
parents: 533
diff changeset
   249
        self._code.append('%s Td' % fp_str(dx, -dy))
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   250
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   251
        # Keep track of the new line offsets and the cursor position
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   252
        self._x0 += dx
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   253
        self._y0 += dy
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   254
        self._x = self._x0
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   255
        self._y = self._y0
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   256
434
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
   257
    def setXPos(self, dx):
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   258
        """Starts a new line dx away from the start of the
434
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
   259
        current line - NOT from the current point! So if
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
   260
        you call it in mid-sentence, watch out."""
538
c7253e1793af Layout fixes; Td was absolute not relative to the beginning of the line
rgbecker
parents: 533
diff changeset
   261
        self.moveCursor(dx,0)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   262
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   263
    def getCursor(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   264
        """Returns current text position relative to the last origin."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   265
        return (self._x, self._y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   266
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   267
    def getStartOfLine(self):
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   268
        """Returns a tuple giving the text position of the start of the
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   269
        current line."""
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   270
        return (self._x0, self._y0)
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   271
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   272
    def getX(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   273
        """Returns current x position relative to the last origin."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   274
        return self._x
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   275
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   276
    def getY(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   277
        """Returns current y position relative to the last origin."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   278
        return self._y
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   279
114
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   280
    def _setFont(self, psfontname, size):
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   281
        """Sets the font and fontSize
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   282
        Raises a readable exception if an illegal font
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   283
        is supplied.  Font names are case-sensitive! Keeps track
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   284
        of font anme and size for metrics."""
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   285
        self._fontname = psfontname
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   286
        self._fontsize = size
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   287
        font = pdfmetrics.getFont(self._fontname)
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   288
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2575
diff changeset
   289
        if font._dynamicFont:
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   290
            self._curSubset = -1
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   291
        else:
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   292
            pdffontname = self._canvas._doc.getInternalFontName(psfontname)
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   293
            self._code.append('%s %s Tf' % (pdffontname, fp_str(size)))
114
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   294
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   295
    def setFont(self, psfontname, size, leading = None):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   296
        """Sets the font.  If leading not specified, defaults to 1.2 x
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   297
        font size. Raises a readable exception if an illegal font
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   298
        is supplied.  Font names are case-sensitive! Keeps track
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   299
        of font anme and size for metrics."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   300
        self._fontname = psfontname
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   301
        self._fontsize = size
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   302
        if leading is None:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   303
            leading = size * 1.2
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   304
        self._leading = leading
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   305
        font = pdfmetrics.getFont(self._fontname)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2575
diff changeset
   306
        if font._dynamicFont:
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   307
            self._curSubset = -1
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   308
        else:
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   309
            pdffontname = self._canvas._doc.getInternalFontName(psfontname)
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   310
            self._code.append('%s %s Tf %s TL' % (pdffontname, fp_str(size), fp_str(leading)))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   311
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   312
    def setCharSpace(self, charSpace):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   313
         """Adjusts inter-character spacing"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   314
         self._charSpace = charSpace
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   315
         self._code.append('%s Tc' % fp_str(charSpace))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   316
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   317
    def setWordSpace(self, wordSpace):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   318
        """Adjust inter-word spacing.  This can be used
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   319
        to flush-justify text - you get the width of the
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   320
        words, and add some space between them."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   321
        self._wordSpace = wordSpace
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   322
        self._code.append('%s Tw' % fp_str(wordSpace))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   323
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   324
    def setHorizScale(self, horizScale):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   325
        "Stretches text out horizontally"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   326
        self._horizScale = 100 + horizScale
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   327
        self._code.append('%s Tz' % fp_str(horizScale))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   328
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   329
    def setLeading(self, leading):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   330
        "How far to move down at the end of a line."
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   331
        self._leading = leading
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   332
        self._code.append('%s TL' % fp_str(leading))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   333
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   334
    def setTextRenderMode(self, mode):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   335
        """Set the text rendering mode.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   336
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   337
        0 = Fill text
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   338
        1 = Stroke text
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   339
        2 = Fill then stroke
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   340
        3 = Invisible
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   341
        4 = Fill text and add to clipping path
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   342
        5 = Stroke text and add to clipping path
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   343
        6 = Fill then stroke and add to clipping path
3572
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   344
        7 = Add to clipping path
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   345
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   346
        after we start clipping we mustn't change the mode back until after the ET
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   347
        """
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   348
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   349
        assert mode in (0,1,2,3,4,5,6,7), "mode must be in (0,1,2,3,4,5,6,7)"
3572
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   350
        if (mode & 4)!=self._clipping:
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   351
            mode |= 4
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   352
            self._clipping = mode & 4
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   353
        if self._textRenderMode!=mode:
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   354
            self._textRenderMode = mode
d88eb970b7be textobject.py: attempt to fix semantics of PDFTextObject.setTextRender for pdf >=1.4
rgbecker
parents: 3444
diff changeset
   355
            self._code.append('%d Tr' % mode)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   356
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   357
    def setRise(self, rise):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   358
        "Move text baseline up or down to allow superscrip/subscripts"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   359
        self._rise = rise
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   360
        self._y = self._y - rise    # + ?  _textLineMatrix?
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   361
        self._code.append('%s Ts' % fp_str(rise))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   362
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   363
    def _formatText(self, text):
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   364
        "Generates PDF text output operator(s)"
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   365
        canv = self._canvas
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   366
        font = pdfmetrics.getFont(self._fontname)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   367
        R = []
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2575
diff changeset
   368
        if font._dynamicFont:
2217
18ff4aed511d Truetype fonts now convert latin-1 to utf8 if fed non-utf8
andy_robinson
parents: 1690
diff changeset
   369
            #it's a truetype font and should be utf8.  If an error is raised,
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   370
            for subset, t in font.splitString(text, canv._doc):
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2575
diff changeset
   371
                if subset!=self._curSubset:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   372
                    pdffontname = font.getSubsetInternalName(subset, canv._doc)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   373
                    R.append("%s %s Tf %s TL" % (pdffontname, fp_str(self._fontsize), fp_str(self._leading)))
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   374
                    self._curSubset = subset
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   375
                R.append("(%s) Tj" % canv._escape(t))
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   376
        elif font._multiByte:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   377
            #all the fonts should really work like this - let them know more about PDF...
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   378
            R.append("%s %s Tf %s TL" % (
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   379
                canv._doc.getInternalFontName(font.fontName),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   380
                fp_str(self._fontsize),
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   381
                fp_str(self._leading)
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   382
                ))
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   383
            R.append("(%s) Tj" % font.formatForPdf(text))
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   384
        else:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   385
            #convert to T1  coding
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   386
            fc = font
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   387
            if not isPython3 and not isinstance(text,unicode):
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   388
                try:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   389
                    text = text.decode('utf8')
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3617
diff changeset
   390
                except UnicodeDecodeError as e:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   391
                    i,j = e.args[2:4]
2983
0b2e5d0a95ab textobject.py: fix possible negative index
rgbecker
parents: 2964
diff changeset
   392
                    raise UnicodeDecodeError(*(e.args[:4]+('%s\n%s-->%s<--%s' % (e.args[4],text[max(i-10,0):i],text[i:j],text[j:j+10]),)))
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   393
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   394
            for f, t in pdfmetrics.unicode2T1(text,[font]+font.substitutionFonts):
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   395
                if f!=fc:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   396
                    R.append("%s %s Tf %s TL" % (canv._doc.getInternalFontName(f.fontName), fp_str(self._fontsize), fp_str(self._leading)))
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   397
                    fc = f
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   398
                R.append("(%s) Tj" % canv._escape(t))
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   399
            if font!=fc:
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   400
                R.append("%s %s Tf %s TL" % (canv._doc.getInternalFontName(self._fontname), fp_str(self._fontsize), fp_str(self._leading)))
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2539
diff changeset
   401
        return ' '.join(R)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   402
111
52f6b8586dbf Added _textOut for use in layout
rgbecker
parents: 106
diff changeset
   403
    def _textOut(self, text, TStar=0):
52f6b8586dbf Added _textOut for use in layout
rgbecker
parents: 106
diff changeset
   404
        "prints string at current point, ignores text cursor"
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   405
        self._code.append('%s%s' % (self._formatText(text), (TStar and ' T*' or '')))
111
52f6b8586dbf Added _textOut for use in layout
rgbecker
parents: 106
diff changeset
   406
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   407
    def textOut(self, text):
533
3d6d265268f5 Fixed obscure stringwidth bugs in textobject and bullets
andy_robinson
parents: 494
diff changeset
   408
        """prints string at current point, text cursor moves across."""
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   409
        self._x = self._x + self._canvas.stringWidth(text, self._fontname, self._fontsize)
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1635
diff changeset
   410
        self._code.append(self._formatText(text))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   411
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   412
    def textLine(self, text=''):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   413
        """prints string at current point, text cursor moves down.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   414
        Can work with no argument to simply move the cursor down."""
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   415
        # Update the coordinates of the cursor
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   416
        self._x = self._x0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   417
        if self._canvas.bottomup:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   418
            self._y = self._y - self._leading
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   419
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   420
            self._y = self._y + self._leading
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   421
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   422
        # Update the location of the start of the line
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   423
        # self._x0 is unchanged
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   424
        self._y0 = self._y
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   425
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   426
        # Output the text followed by a PDF newline command
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 562
diff changeset
   427
        self._code.append('%s T*' % self._formatText(text))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   428
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   429
    def textLines(self, stuff, trim=1):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   430
        """prints multi-line or newlined strings, moving down.  One
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   431
        comon use is to quote a multi-line block in your Python code;
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   432
        since this may be indented, by default it trims whitespace
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   433
        off each line and from the beginning; set trim=0 to preserve
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   434
        whitespace."""
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   435
        if isStrType(stuff):
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   436
            lines = '\n'.split(stuff.strip())
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   437
            if trim==1:
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   438
                lines = [s.strip() for s in lines]
3178
92424aba6ad1 textobject.py: fix buglet and make chanegs inspired by henning.vonbargen@arcor.de
rgbecker
parents: 3030
diff changeset
   439
        elif isinstance(stuff,(tuple,list)):
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   440
            lines = stuff
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   441
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   442
            assert 1==0, "argument to textlines must be string,, list or tuple"
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 1677
diff changeset
   443
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   444
        # Output each line one at a time. This used to be a long-hand
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   445
        # copy of the textLine code, now called as a method.
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   446
        for line in lines:
2228
72b4068d0ca0 TextObject fixes from Ian Millington
rgbecker
parents: 2217
diff changeset
   447
            self.textLine(line)
19
5b4da60fa383 trailing text/Odyssey fix
rgbecker
parents: 16
diff changeset
   448
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   449
    def __nonzero__(self):
19
5b4da60fa383 trailing text/Odyssey fix
rgbecker
parents: 16
diff changeset
   450
        'PDFTextObject is true if it has something done after the init'
1690
2adbe2b8005a Allow use of a TTFont on different documents at the same time
mgedmin
parents: 1683
diff changeset
   451
        return self._code != ['BT']