reportlab/pdfgen/textobject.py
author rgbecker
Wed, 25 Oct 2000 08:57:46 +0000
changeset 494 54257447cfe9
parent 455 3eb389710433
child 533 3d6d265268f5
permissions -rwxr-xr-x
Changed to indirect copyright
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 455
diff changeset
     1
#copyright ReportLab Inc. 2000
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 455
diff changeset
     2
#see license.txt for license details
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 455
diff changeset
     3
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/pdfgen/textobject.py?cvsroot=reportlab
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 455
diff changeset
     4
#$Header: /tmp/reportlab/reportlab/pdfgen/textobject.py,v 1.19 2000/10/25 08:57:45 rgbecker Exp $
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 455
diff changeset
     5
__version__=''' $Id: textobject.py,v 1.19 2000/10/25 08:57:45 rgbecker Exp $ '''
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
     6
__doc__=""" 
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
     7
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
     8
instantiate directly, obtain one from the Canvas instead.
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     9
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    10
Progress Reports:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    11
8.83, 2000-01-13, gmcm:
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    12
    created from pdfgen.py
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    13
"""
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    14
import string
6
eb791971b252 Added license, __version__ and Logi comment
rgbecker
parents: 0
diff changeset
    15
from types import *
43
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 19
diff changeset
    16
from reportlab.lib import colors
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
    17
from reportlab.lib.colors import ColorType
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
    18
from reportlab.lib.utils import fp_str
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    19
106
2cec078fe86d Added defn of _SeqTypes
rgbecker
parents: 105
diff changeset
    20
_SeqTypes=(TupleType,ListType)
2cec078fe86d Added defn of _SeqTypes
rgbecker
parents: 105
diff changeset
    21
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    22
class PDFTextObject:
412
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
    23
    """PDF logically separates text and graphics drawing; text
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
    24
    operations need to be bracketed between BT (Begin text) and
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
    25
    ET operators. This class ensures text operations are
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
    26
    properly encapusalted. Ask the canvas for a text object
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
    27
    with beginText(x, y).  Do not construct one directly. 
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
    28
    Do not use multiple text objects in parallel; PDF is
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
    29
    not multi-threaded!
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
    30
    
fe157c8cf33d Updated old doc string
andy_robinson
parents: 409
diff changeset
    31
    It keeps track of x and y coordinates relative to its origin."""
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    32
    def __init__(self, canvas, x=0,y=0):
19
5b4da60fa383 trailing text/Odyssey fix
rgbecker
parents: 16
diff changeset
    33
        self._code = ['BT']    #no point in [] then append RGB
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    34
        self._canvas = canvas  #canvas sets this so it has access to size info
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    35
        self._fontname = self._canvas._fontname
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    36
        self._fontsize = self._canvas._fontsize
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    37
        self._leading = self._canvas._leading
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    38
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    39
        self.setTextOrigin(x, y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    40
            
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    41
    def getCode(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    42
        "pack onto one line; used internally"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    43
        self._code.append('ET')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    44
        return string.join(self._code, ' ')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    45
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    46
    def setTextOrigin(self, x, y):    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    47
        if self._canvas.bottomup:
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
    48
            self._code.append('1 0 0 1 %s Tm' % fp_str(x, y)) #bottom up
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    49
        else:
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
    50
            self._code.append('1 0 0 -1 %s Tm' % fp_str(x, y))  #top down
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    51
        self._x = x
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    52
        self._y = y
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    53
        self._x0 = x #the margin
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    54
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    55
    def setTextTransform(self, a, b, c, d, e, f):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    56
        "Like setTextOrigin, but does rotation, scaling etc."
409
51f898a4dd2d Bernhard herzog inspired fixes
rgbecker
parents: 114
diff changeset
    57
        if not self._canvas.bottomup:
51f898a4dd2d Bernhard herzog inspired fixes
rgbecker
parents: 114
diff changeset
    58
            c = -c    #reverse bottom row of the 2D Transform
51f898a4dd2d Bernhard herzog inspired fixes
rgbecker
parents: 114
diff changeset
    59
            d = -d
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
    60
        self._code.append('%s Tm' % fp_str(a, b, c, d, e, f))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    61
        #we only measure coords relative to present text matrix
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    62
        self._x = e
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    63
        self._y = f
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    64
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    65
    def moveCursor(self, dx, dy):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    66
        """Moves to a point dx, dy away from the start of the
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    67
        current line - NOT from the current point! So if
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    68
        you call it in mid-sentence, watch out."""
455
3eb389710433 Improved optimisation checks
rgbecker
parents: 454
diff changeset
    69
        if self._code and self._code[-1][-3:]==' Td':
434
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    70
            L = string.split(self._code[-1])
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    71
            if len(L)==3:
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    72
                del self._code[-1]
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    73
            else:
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    74
                self._code[-1] = string.join(L[:-4])
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    75
        if dx<>0 or dy<>0: self._code.append('%s Td' % fp_str(dx, -dy))
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    76
 
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    77
    def setXPos(self, dx):
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    78
        """Moves to a point dx away from the start of the
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    79
        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
    80
        you call it in mid-sentence, watch out."""
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    81
        if self._code[-1][-3:]==' Td':
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    82
            L = string.split(self._code[-1])
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    83
            if len(L)==3:
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    84
                del self._code[-1]
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    85
            else:
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    86
                self._code[-1] = string.join(L[:-4])
257af3109bf4 Various brutal changes to paragraph, canvas and textobject for speed/size
rgbecker
parents: 418
diff changeset
    87
        if dx<>0: self._code.append('%s 0 Td' % fp_str(dx))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    88
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    89
    def getCursor(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    90
        """Returns current text position relative to the last origin."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    91
        return (self._x, self._y)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    92
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    93
    def getX(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    94
        """Returns current x position relative to the last origin."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    95
        return self._x
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    96
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    97
    def getY(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    98
        """Returns current y position relative to the last origin."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    99
        return self._y
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   100
114
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   101
    def _setFont(self, psfontname, size):
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   102
        """Sets the font and fontSize
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   103
        Raises a readable exception if an illegal font
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   104
        is supplied.  Font names are case-sensitive! Keeps track
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   105
        of font anme and size for metrics."""
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   106
        self._fontname = psfontname
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   107
        self._fontsize = size
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   108
        pdffontname = self._canvas._doc.getInternalFontName(psfontname)
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   109
        self._code.append('%s %s Tf' % (pdffontname, fp_str(size)))
114
5a7016145b96 Added _setFont for use in layout
rgbecker
parents: 111
diff changeset
   110
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   111
    def setFont(self, psfontname, size, leading = None):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   112
        """Sets the font.  If leading not specified, defaults to 1.2 x
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   113
        font size. Raises a readable exception if an illegal font
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   114
        is supplied.  Font names are case-sensitive! Keeps track
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   115
        of font anme and size for metrics."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   116
        self._fontname = psfontname
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   117
        self._fontsize = size
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   118
        pdffontname = self._canvas._doc.getInternalFontName(psfontname)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   119
        if leading is None:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   120
            leading = size * 1.2
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   121
        self._leading = leading
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   122
        self._code.append('%s %s Tf %s TL' % (pdffontname, fp_str(size), fp_str(leading)))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   123
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   124
    def setCharSpace(self, charSpace):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   125
         """Adjusts inter-character spacing"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   126
         self._charSpace = charSpace
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   127
         self._code.append('%s Tc' % fp_str(charSpace))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   128
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   129
    def setWordSpace(self, wordSpace):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   130
        """Adjust inter-word spacing.  This can be used
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   131
        to flush-justify text - you get the width of the
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   132
        words, and add some space between them."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   133
        self._wordSpace = wordSpace
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   134
        self._code.append('%s Tw' % fp_str(wordSpace))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   135
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   136
    def setHorizScale(self, horizScale):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   137
        "Stretches text out horizontally"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   138
        self._horizScale = 100 + horizScale
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   139
        self._code.append('%s Tz' % fp_str(horizScale))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   140
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   141
    def setLeading(self, leading):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   142
        "How far to move down at the end of a line."
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   143
        self._leading = leading
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   144
        self._code.append('%s TL' % fp_str(leading))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   145
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   146
    def setTextRenderMode(self, mode):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   147
        """Set the text rendering mode.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   148
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   149
        0 = Fill text
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   150
        1 = Stroke text
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   151
        2 = Fill then stroke
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   152
        3 = Invisible
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   153
        4 = Fill text and add to clipping path
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   154
        5 = Stroke text and add to clipping path
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   155
        6 = Fill then stroke and add to clipping path
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   156
        7 = Add to clipping path"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   157
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   158
        assert mode in (0,1,2,3,4,5,6,7), "mode must be in (0,1,2,3,4,5,6,7)"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   159
        self._textRenderMode = mode
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   160
        self._code.append('%d Tr' % mode)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   161
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   162
    def setRise(self, rise):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   163
        "Move text baseline up or down to allow superscrip/subscripts"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   164
        self._rise = rise
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   165
        self._y = self._y - rise    # + ?  _textLineMatrix?
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   166
        self._code.append('%s Ts' % fp_str(rise))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   167
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   168
    def setStrokeColorRGB(self, r, g, b):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   169
        self._strokeColorRGB = (r, g, b)
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   170
        self._code.append('%s RG' % fp_str(r,g,b))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   171
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   172
    def setFillColorRGB(self, r, g, b):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   173
        self._fillColorRGB = (r, g, b)
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   174
        self._code.append('%s rg' % fp_str(r,g,b))
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   175
 
49
f19eb245ce5e Added methods for CMYK color model
andy_robinson
parents: 43
diff changeset
   176
    def setFillColorCMYK(self, c, m, y, k):
f19eb245ce5e Added methods for CMYK color model
andy_robinson
parents: 43
diff changeset
   177
        """Takes 4 arguments between 0.0 and 1.0"""
f19eb245ce5e Added methods for CMYK color model
andy_robinson
parents: 43
diff changeset
   178
        self._fillColorCMYK = (c, m, y, k)
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   179
        self._code.append('%s k' % fp_str(c, m, y, k))
49
f19eb245ce5e Added methods for CMYK color model
andy_robinson
parents: 43
diff changeset
   180
        
f19eb245ce5e Added methods for CMYK color model
andy_robinson
parents: 43
diff changeset
   181
    def setStrokeColorCMYK(self, c, m, y, k):
f19eb245ce5e Added methods for CMYK color model
andy_robinson
parents: 43
diff changeset
   182
        """Takes 4 arguments between 0.0 and 1.0"""
f19eb245ce5e Added methods for CMYK color model
andy_robinson
parents: 43
diff changeset
   183
        self._strokeColorCMYK = (c, m, y, k)
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   184
        self._code.append('%s K' % fp_str(c, m, y, k))
49
f19eb245ce5e Added methods for CMYK color model
andy_robinson
parents: 43
diff changeset
   185
43
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 19
diff changeset
   186
    def setFillColor(self, aColor):
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 19
diff changeset
   187
        """Takes a color object, allowing colors to be referred to by name"""
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   188
        if type(aColor) == ColorType:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   189
            rgb = (aColor.red, aColor.green, aColor.blue)
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   190
            self._fillColorRGB = rgb
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   191
            self._code.append('%s rg' % fp_str(rgb) )
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   192
        elif type(aColor) in _SeqTypes:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   193
            l = len(aColor)
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   194
            if l==3:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   195
                self._fillColorRGB = aColor
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   196
                self._code.append('%s rg' % fp_str(aColor) )
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   197
            elif l==4:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   198
                self.setFillColorCMYK(self, aColor[0], aColor[1], aColor[2], aColor[3])
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   199
            else:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   200
                raise 'Unknown color', str(aColor)
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   201
        else:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   202
            raise 'Unknown color', str(aColor)
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   203
43
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 19
diff changeset
   204
        
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 19
diff changeset
   205
    def setStrokeColor(self, aColor):
71baccf1c57f Canvas has two methods setFillColor(aColor) and setStrokeColor(aColor)
andy_robinson
parents: 19
diff changeset
   206
        """Takes a color object, allowing colors to be referred to by name"""
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   207
        if type(aColor) == ColorType:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   208
            rgb = (aColor.red, aColor.green, aColor.blue)
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   209
            self._strokeColorRGB = rgb
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   210
            self._code.append('%s RG' % fp_str(rgb) )
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   211
        elif type(aColor) in _SeqTypes:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   212
            l = len(aColor)
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   213
            if l==3:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   214
                self._strokeColorRGB = aColor
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   215
                self._code.append('%s RG' % fp_str(aColor) )
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   216
            elif l==4:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   217
                self.setStrokeColorCMYK(self, aColor[0], aColor[1], aColor[2], aColor[3])
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   218
            else:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   219
                raise 'Unknown color', str(aColor)
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   220
        else:
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   221
            raise 'Unknown color', str(aColor)
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   222
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   223
    def setFillGray(self, gray):
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   224
        """Sets the gray level; 0.0=black, 1.0=white"""
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   225
        self._fillColorRGB = (gray, gray, gray)
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   226
        self._code.append('%s g' % fp_str(gray))
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   227
        
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   228
    def setStrokeGray(self, gray):
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   229
        """Sets the gray level; 0.0=black, 1.0=white"""
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   230
        self._strokeColorRGB = (gray, gray, gray)
418
2aede080c040 Converted to using fp_str
rgbecker
parents: 412
diff changeset
   231
        self._code.append('%s G' % fp_str(gray))
100
b98a1487daad Color methods in textobject and canvas now synchronised.
andy_robinson
parents: 49
diff changeset
   232
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   233
111
52f6b8586dbf Added _textOut for use in layout
rgbecker
parents: 106
diff changeset
   234
    def _textOut(self, text, TStar=0):
52f6b8586dbf Added _textOut for use in layout
rgbecker
parents: 106
diff changeset
   235
        "prints string at current point, ignores text cursor"
52f6b8586dbf Added _textOut for use in layout
rgbecker
parents: 106
diff changeset
   236
        text = self._canvas._escape(text)
52f6b8586dbf Added _textOut for use in layout
rgbecker
parents: 106
diff changeset
   237
        self._code.append('(%s) Tj%s' % (text,(TStar and ' T*' or '')))
52f6b8586dbf Added _textOut for use in layout
rgbecker
parents: 106
diff changeset
   238
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   239
    def textOut(self, text):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   240
        "prints string at current point, text cursor moves across"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   241
        text = self._canvas._escape(text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   242
        self._x = self._x + self._canvas.stringWidth(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   243
                    text, self._fontname, self._fontsize)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   244
        self._code.append('(%s) Tj' % text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   245
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   246
    def textLine(self, text=''):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   247
        """prints string at current point, text cursor moves down.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   248
        Can work with no argument to simply move the cursor down."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   249
        text = self._canvas._escape(text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   250
        self._x = self._x0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   251
        if self._canvas.bottomup:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   252
            self._y = self._y - self._leading
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   253
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   254
            self._y = self._y + self._leading
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   255
        self._code.append('(%s) Tj T*' % text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   256
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   257
    def textLines(self, stuff, trim=1):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   258
        """prints multi-line or newlined strings, moving down.  One
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   259
        comon use is to quote a multi-line block in your Python code;
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   260
        since this may be indented, by default it trims whitespace
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   261
        off each line and from the beginning; set trim=0 to preserve
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   262
        whitespace."""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   263
        if type(stuff) == StringType:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   264
            lines = string.split(string.strip(stuff), '\n')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   265
            if trim==1:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   266
                lines = map(string.strip,lines)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   267
        elif type(stuff) == ListType:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   268
            lines = stuff
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   269
        elif type(stuff) == TupleType:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   270
            lines = stuff
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   271
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   272
            assert 1==0, "argument to textlines must be string,, list or tuple"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   273
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   274
        for line in lines:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   275
            escaped_text = self._canvas._escape(line)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   276
            self._code.append('(%s) Tj T*' % escaped_text)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   277
            if self._canvas.bottomup:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   278
                self._y = self._y - self._leading
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   279
            else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   280
                self._y = self._y + self._leading
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   281
        self._x = self._x0
19
5b4da60fa383 trailing text/Odyssey fix
rgbecker
parents: 16
diff changeset
   282
5b4da60fa383 trailing text/Odyssey fix
rgbecker
parents: 16
diff changeset
   283
    def __nonzero__(self):
5b4da60fa383 trailing text/Odyssey fix
rgbecker
parents: 16
diff changeset
   284
        'PDFTextObject is true if it has something done after the init'
5b4da60fa383 trailing text/Odyssey fix
rgbecker
parents: 16
diff changeset
   285
        return self._code != ['BT']