implemented font slanting via psfont.map
[PyX/mjg.git] / pyx / type1font.py
blob34f14c10e0091725746cb8654d9aa77d94a018ab
1 # -*- coding: ISO-8859-1 -*-
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>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
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.
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.
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
24 import bbox, canvas, pswriter, pdfwriter
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)
34 class encoding:
36 def __init__(self, name, filename):
37 """ font encoding contained in filename """
38 self.name = name
39 self.filename = filename
42 class encodingfile:
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()
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))
69 def decode(self, charcode):
70 return self.encvector[charcode]
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")
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")
100 class font:
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
107 self.metric = metric
109 if encoding is None:
110 self.name = basefontname
111 else:
112 self.name = "%s-%s" % (basefontname, encoding.name)
114 if slant is None:
115 self.slant = 0.0
116 else:
117 self.name = "%s-slanted%f" % (self.name, self.slant)
120 class text_pt(canvas.canvasitem):
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 = []
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)
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)
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()
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))
165 def processPDF(self, file, writer, context, registry, bbox):
166 registry.add(pdfwriter.PDFfont(self.font, self.chars, writer, registry))
167 bbox += self.bbox()
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 file.write("1 0 %f 1 %f %f Tm (%s) Tj\n" % (self.font.slant, self.x_pt, self.y_pt, outstring))