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
29 # fallback implementation for Python 2.2 and below
31 return zip(xrange(len(list)), list)
36 def __init__(self
, name
, filename
):
37 """ font encoding contained in filename """
40 self
.filename
= filename
45 def __init__(self
, name
, filename
):
46 # XXX move the cursor to a module of its own
47 from font
.t1font
import cursor
49 encfile
= open(filename
, "r")
50 c
= cursor(encfile
.read(), "")
54 self
.encname
= c
.gettoken()
57 raise RuntimeError("cannot parse encoding file '%s', expecting '[' got '%s'" % (filename
, 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
)
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
)
77 for i
, glyphname
in enumerate(self
.encvector
):
86 def outputPDF(self
, file, writer
):
91 for i
, glyphname
in enumerate(self
.encvector
):
103 def __init__(self
, basefontname
, filename
, encoding
, metric
):
104 self
.basefontname
= basefontname
105 self
.filename
= filename
106 self
.encoding
= encoding
110 self
.name
= basefontname
112 self
.name
= "%s-%s" % (basefontname
, encoding
.name
)
115 class text_pt(canvas
.canvasitem
):
117 def __init__(self
, x_pt
, y_pt
, font
):
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
)
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
151 for char
in self
.chars
:
152 if char
> 32 and char
< 127 and chr(char
) not in "()[]<>\\":
153 ascii
= "%s" % chr(char
)
155 ascii
= "\\%03o" % char
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
169 for char
in self
.chars
:
170 if 32 <= char
<= 127 and chr(char
) not in "()[]<>\\":
171 ascii
= "%s" % chr(char
)
173 ascii
= "\\%03o" % char
175 file.write("1 0 0 1 %f %f Tm (%s) Tj\n" % (self
.x_pt
, self
.y_pt
, outstring
))