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