~ubuntu-branches/debian/sid/pyx/sid

« back to all changes in this revision

Viewing changes to pyx/type1font.py

  • Committer: Bazaar Package Importer
  • Author(s): Stuart Prescott
  • Date: 2011-05-20 00:13:52 UTC
  • mto: (9.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20110520001352-odcuqpdezuusbbw1
Tags: upstream-0.11.1
Import upstream version 0.11.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: ISO-8859-1 -*-
2
 
#
3
 
#
4
 
# Copyright (C) 2005-2006 J�rg Lehmann <joergl@users.sourceforge.net>
5
 
# Copyright (C) 2005-2006 Andr� Wobst <wobsta@users.sourceforge.net>
6
 
# Copyright (C) 2007 Michael Schindler <m-schindler@users.sourceforge.net>
7
 
#
8
 
# This file is part of PyX (http://pyx.sourceforge.net/).
9
 
#
10
 
# PyX is free software; you can redistribute it and/or modify
11
 
# it under the terms of the GNU General Public License as published by
12
 
# the Free Software Foundation; either version 2 of the License, or
13
 
# (at your option) any later version.
14
 
#
15
 
# PyX is distributed in the hope that it will be useful,
16
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
# GNU General Public License for more details.
19
 
#
20
 
# You should have received a copy of the GNU General Public License
21
 
# along with PyX; if not, write to the Free Software
22
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
23
 
 
24
 
import bbox, canvas, pswriter, pdfwriter
25
 
 
26
 
try:
27
 
    enumerate([])
28
 
except NameError:
29
 
    # fallback implementation for Python 2.2 and below
30
 
    def enumerate(list):
31
 
        return zip(xrange(len(list)), list)
32
 
 
33
 
 
34
 
class encoding:
35
 
 
36
 
    def __init__(self, name, filename):
37
 
        """ font encoding contained in filename """
38
 
        self.name = name
39
 
        self.filename = filename
40
 
 
41
 
 
42
 
class encodingfile:
43
 
 
44
 
    def __init__(self, name, filename):
45
 
        # XXX move the cursor to a module of its own
46
 
        from font.t1font import cursor
47
 
        self.name = name
48
 
        encfile = open(filename, "r")
49
 
        c = cursor(encfile.read(), "")
50
 
        encfile.close()
51
 
 
52
 
        # name of encoding
53
 
        self.encname = c.gettoken()
54
 
        token = c.gettoken()
55
 
        if token != "[":
56
 
            raise RuntimeError("cannot parse encoding file '%s', expecting '[' got '%s'" % (filename, token))
57
 
        self.encvector = []
58
 
        for i in range(256):
59
 
            token = c.gettoken()
60
 
            if token == "]":
61
 
                raise RuntimeError("not enough charcodes in encoding file '%s'" % filename)
62
 
            self.encvector.append(token)
63
 
        if c.gettoken() != "]":
64
 
            raise RuntimeError("too many charcodes in encoding file '%s'" % filename)
65
 
        token = c.gettoken()
66
 
        if token != "def":
67
 
            raise RuntimeError("cannot parse encoding file '%s', expecting 'def' got '%s'" % (filename, token))
68
 
 
69
 
    def decode(self, charcode):
70
 
        return self.encvector[charcode]
71
 
 
72
 
    def outputPS(self, file, writer):
73
 
        file.write("%%%%BeginProcSet: %s\n" % self.name)
74
 
        file.write("/%s\n"
75
 
                   "[" % self.name)
76
 
        for i, glyphname in enumerate(self.encvector):
77
 
            if i and not (i % 8):
78
 
                file.write("\n")
79
 
            else:
80
 
                file.write(" ")
81
 
            file.write(glyphname)
82
 
        file.write(" ] def\n"
83
 
                   "%%EndProcSet\n")
84
 
 
85
 
    def outputPDF(self, file, writer):
86
 
        file.write("<<\n"
87
 
                   "/Type /Encoding\n"
88
 
                   "/Differences\n"
89
 
                   "[ 0")
90
 
        for i, glyphname in enumerate(self.encvector):
91
 
            if i and not (i % 8):
92
 
                file.write("\n")
93
 
            else:
94
 
                file.write(" ")
95
 
            file.write(glyphname)
96
 
        file.write(" ]\n"
97
 
                   ">>\n")
98
 
 
99
 
 
100
 
class font:
101
 
 
102
 
    def __init__(self, basefontname, filename, encoding, slant, metric):
103
 
        self.basefontname = basefontname
104
 
        self.filename = filename
105
 
        self.encoding = encoding
106
 
        self.slant = slant # None or a float
107
 
        self.metric = metric
108
 
 
109
 
        if encoding is not None and slant is not None:
110
 
            self.encname = "%s-%s" % (basefontname, encoding.name)
111
 
            self.name = "%s-slant%f" % (self.encname, self.slant)
112
 
        elif encoding is not None:
113
 
            self.name = "%s-%s" % (basefontname, encoding.name)
114
 
        elif slant is not None:
115
 
            self.name = "%s-slant%f" % (basefontname, self.slant)
116
 
        else:
117
 
            self.name = basefontname
118
 
 
119
 
 
120
 
class text_pt(canvas.canvasitem):
121
 
 
122
 
    def __init__(self, x_pt, y_pt, font):
123
 
        self.font = font
124
 
        self.x_pt = x_pt
125
 
        self.y_pt = y_pt
126
 
        self.width_pt = 0
127
 
        self.height_pt = 0
128
 
        self.depth_pt = 0
129
 
        self.chars = []
130
 
 
131
 
    def addchar(self, char):
132
 
        metric = self.font.metric
133
 
        self.width_pt += metric.getwidth_pt(char)
134
 
        cheight_pt = metric.getwidth_pt(char)
135
 
        if cheight_pt > self.height_pt:
136
 
            self.height_pt = cheight_pt
137
 
        cdepth_pt = metric.getdepth_pt(char)
138
 
        if cdepth_pt > self.depth_pt:
139
 
            self.depth_pt = cdepth_pt
140
 
        self.chars.append(char)
141
 
 
142
 
    def bbox(self):
143
 
        return bbox.bbox_pt(self.x_pt, self.y_pt-self.depth_pt, self.x_pt+self.width_pt, self.y_pt+self.height_pt)
144
 
 
145
 
    def processPS(self, file, writer, context, registry, bbox):
146
 
        # note that we don't register PSfont as it is just a helper resource
147
 
        # which registers the needed components
148
 
        pswriter.PSfont(self.font, self.chars, registry)
149
 
        bbox += self.bbox()
150
 
 
151
 
        if ( context.font is None or
152
 
             context.font.name != self.font.name or
153
 
             context.font.metric.getsize_pt() !=  self.font.metric.getsize_pt() ):
154
 
            file.write("/%s %f selectfont\n" % (self.font.name, self.font.metric.getsize_pt()))
155
 
            context.font = self.font
156
 
        outstring = ""
157
 
        for char in self.chars:
158
 
            if char > 32 and char < 127 and chr(char) not in "()[]<>\\":
159
 
                ascii = "%s" % chr(char)
160
 
            else:
161
 
                ascii = "\\%03o" % char
162
 
            outstring += ascii
163
 
        file.write("%g %g moveto (%s) show\n" % (self.x_pt, self.y_pt, outstring))
164
 
 
165
 
    def processPDF(self, file, writer, context, registry, bbox):
166
 
        registry.add(pdfwriter.PDFfont(self.font, self.chars, writer, registry))
167
 
        bbox += self.bbox()
168
 
 
169
 
        if ( context.font is None or
170
 
             context.font.name != self.font.name or
171
 
             context.font.metric.getsize_pt() !=  self.font.metric.getsize_pt() ):
172
 
            file.write("/%s %f Tf\n" % (self.font.name, self.font.metric.getsize_pt()))
173
 
            context.font = self.font
174
 
        outstring = ""
175
 
        for char in self.chars:
176
 
            if 32 <= char <= 127 and chr(char) not in "()[]<>\\":
177
 
                ascii = "%s" % chr(char)
178
 
            else:
179
 
                ascii = "\\%03o" % char
180
 
            outstring += ascii
181
 
        if self.font.slant is None:
182
 
            slantvalue = 0
183
 
        else:
184
 
            slantvalue = self.font.slant
185
 
        file.write("1 0 %f 1 %f %f Tm (%s) Tj\n" % (slantvalue, self.x_pt, self.y_pt, outstring))
186