remove registerPS/registerPDF in favour of registering resourcing during the outputPS...
[PyX/mjg.git] / pyx / type1font.py
blob3053c1cbd5cc1fa088d0a44d592e6b1932aede70
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2005 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2005 André Wobst <wobsta@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 print "x"*100
39 self.name = name
40 self.filename = filename
43 class encodingfile:
45 def __init__(self, name, filename):
46 # XXX move the cursor to a module of its own
47 from font.t1font import cursor
48 self.name = name
49 encfile = open(filename, "r")
50 c = cursor(encfile.read(), "")
51 encfile.close()
53 # name of encoding
54 self.encname = c.gettoken()
55 token = c.gettoken()
56 if token != "[":
57 raise RuntimeError("cannot parse encoding file '%s', expecting '[' got '%s'" % (filename, token))
58 self.encvector = []
59 for i in range(256):
60 token = c.gettoken()
61 if token == "]":
62 raise RuntimeError("not enough charcodes in encoding file '%s'" % filename)
63 self.encvector.append(token)
64 if c.gettoken() != "]":
65 raise RuntimeError("too many charcodes in encoding file '%s'" % filename)
66 token = c.gettoken()
67 if token != "def":
68 raise RuntimeError("cannot parse encoding file '%s', expecting 'def' got '%s'" % (filename, token))
70 def decode(self, charcode):
71 return self.encvector[charcode]
73 def outputPS(self, file, writer):
74 file.write("%%%%BeginProcSet: %s\n" % self.name)
75 file.write("/%s\n"
76 "[" % self.name)
77 for i, glyphname in enumerate(self.encvector):
78 if i and not (i % 8):
79 file.write("\n")
80 else:
81 file.write(" ")
82 file.write(glyphname)
83 file.write(" ] def\n"
84 "%%EndProcSet\n")
86 def outputPDF(self, file, writer):
87 file.write("<<\n"
88 "/Type /Encoding\n"
89 "/Differences\n"
90 "[ 0")
91 for i, glyphname in enumerate(self.encvector):
92 if i and not (i % 8):
93 file.write("\n")
94 else:
95 file.write(" ")
96 file.write(glyphname)
97 file.write(" ]\n"
98 ">>\n")
101 class font:
103 def __init__(self, basefontname, filename, encoding, metric):
104 self.basefontname = basefontname
105 self.filename = filename
106 self.encoding = encoding
107 self.metric = metric
109 if encoding is None:
110 self.name = basefontname
111 else:
112 self.name = "%s-%s" % (basefontname, encoding.name)
115 class text_pt(canvas.canvasitem):
117 def __init__(self, x_pt, y_pt, font):
118 self.font = font
119 self.x_pt = x_pt
120 self.y_pt = y_pt
121 self.width_pt = 0
122 self.height_pt = 0
123 self.depth_pt = 0
124 self.chars = []
126 def addchar(self, char):
127 metric = self.font.metric
128 self.width_pt += metric.getwidth_pt(char)
129 cheight_pt = metric.getwidth_pt(char)
130 if cheight_pt > self.height_pt:
131 self.height_pt = cheight_pt
132 cdepth_pt = metric.getdepth_pt(char)
133 if cdepth_pt > self.depth_pt:
134 self.depth_pt = cdepth_pt
135 self.chars.append(char)
137 def bbox(self):
138 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)
140 def outputPS(self, file, writer, context, registry):
141 # note that we don't register PSfont as it is just a helper resource
142 # which registers the needed components
143 pswriter.PSfont(self.font, self.chars, registry)
145 if ( context.font is None or
146 context.font.name != self.font.name or
147 context.font.metric.getsize_pt() != self.font.metric.getsize_pt() ):
148 file.write("/%s %f selectfont\n" % (self.font.name, self.font.metric.getsize_pt()))
149 context.font = self.font
150 outstring = ""
151 for char in self.chars:
152 if char > 32 and char < 127 and chr(char) not in "()[]<>\\":
153 ascii = "%s" % chr(char)
154 else:
155 ascii = "\\%03o" % char
156 outstring += ascii
157 file.write("%g %g moveto (%s) show\n" % (self.x_pt, self.y_pt, outstring))
160 def outputPDF(self, file, writer, context, registry):
161 registry.add(pdfwriter.PDFfont(self.font, self.chars, writer, registry))
163 if ( context.font is None or
164 context.font.name != self.font.name or
165 context.font.metric.getsize_pt() != self.font.metric.getsize_pt() ):
166 file.write("/%s %f Tf\n" % (self.font.name, self.font.metric.getsize_pt()))
167 context.font = self.font
168 outstring = ""
169 for char in self.chars:
170 if 32 <= char <= 127 and chr(char) not in "()[]<>\\":
171 ascii = "%s" % chr(char)
172 else:
173 ascii = "\\%03o" % char
174 outstring += ascii
175 file.write("1 0 0 1 %f %f Tm (%s) Tj\n" % (self.x_pt, self.y_pt, outstring))