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
28 # fallback implementation for Python 2.2 and below
30 return zip(xrange(len(list)), list)
35 def __init__(self
, name
, filename
):
36 """ font encoding contained in filename """
38 self
.filename
= filename
43 def __init__(self
, name
, filename
):
44 # XXX move the cursor to a module of its own
45 from font
.t1font
import cursor
47 encfile
= open(filename
, "r")
48 c
= cursor(encfile
.read(), "")
52 self
.encname
= c
.gettoken()
55 raise RuntimeError("cannot parse encoding file '%s', expecting '[' got '%s'" % (filename
, 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
)
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
)
75 for i
, glyphname
in enumerate(self
.encvector
):
84 def outputPDF(self
, file, writer
):
89 for i
, glyphname
in enumerate(self
.encvector
):
101 def __init__(self
, basefontname
, filename
, encoding
, metric
):
102 self
.basefontname
= basefontname
103 self
.filename
= filename
104 self
.encoding
= encoding
108 self
.name
= basefontname
110 self
.name
= "%s-%s" % (basefontname
, encoding
.name
)
113 class text_pt(canvas
.canvasitem
):
115 def __init__(self
, x_pt
, y_pt
, font
):
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
)
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
)
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
150 for char
in self
.chars
:
151 if char
> 32 and char
< 127 and chr(char
) not in "()[]<>\\":
152 ascii
= "%s" % chr(char
)
154 ascii
= "\\%03o" % char
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
))
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
168 for char
in self
.chars
:
169 if 32 <= char
<= 127 and chr(char
) not in "()[]<>\\":
170 ascii
= "%s" % chr(char
)
172 ascii
= "\\%03o" % char
174 file.write("1 0 0 1 %f %f Tm (%s) Tj\n" % (self
.x_pt
, self
.y_pt
, outstring
))