add descriptions of deformer examples
[PyX/mjg.git] / pyx / type1font.py
blob1b656e319f1821df175d2a359701d0279779d77b
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2005-2006 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2005-2006 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 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, metric):
103 self.basefontname = basefontname
104 self.filename = filename
105 self.encoding = encoding
106 self.metric = metric
108 if encoding is None:
109 self.name = basefontname
110 else:
111 self.name = "%s-%s" % (basefontname, encoding.name)
114 class text_pt(canvas.canvasitem):
116 def __init__(self, x_pt, y_pt, font):
117 self.font = font
118 self.x_pt = x_pt
119 self.y_pt = y_pt
120 self.width_pt = 0
121 self.height_pt = 0
122 self.depth_pt = 0
123 self.chars = []
125 def addchar(self, char):
126 metric = self.font.metric
127 self.width_pt += metric.getwidth_pt(char)
128 cheight_pt = metric.getwidth_pt(char)
129 if cheight_pt > self.height_pt:
130 self.height_pt = cheight_pt
131 cdepth_pt = metric.getdepth_pt(char)
132 if cdepth_pt > self.depth_pt:
133 self.depth_pt = cdepth_pt
134 self.chars.append(char)
136 def bbox(self):
137 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)
139 def processPS(self, file, writer, context, registry, bbox):
140 # note that we don't register PSfont as it is just a helper resource
141 # which registers the needed components
142 pswriter.PSfont(self.font, self.chars, registry)
143 bbox += self.bbox()
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))
159 def processPDF(self, file, writer, context, registry, bbox):
160 registry.add(pdfwriter.PDFfont(self.font, self.chars, writer, registry))
161 bbox += self.bbox()
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))