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
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 """
39 self
.filename
= filename
44 def __init__(self
, name
, filename
):
45 # XXX move the cursor to a module of its own
46 from font
.t1font
import cursor
48 encfile
= open(filename
, "r")
49 c
= cursor(encfile
.read(), "")
53 self
.encname
= c
.gettoken()
56 raise RuntimeError("cannot parse encoding file '%s', expecting '[' got '%s'" % (filename
, 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
)
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
)
76 for i
, glyphname
in enumerate(self
.encvector
):
85 def outputPDF(self
, file, writer
):
90 for i
, glyphname
in enumerate(self
.encvector
):
102 def __init__(self
, basefontname
, filename
, encoding
, metric
):
103 self
.basefontname
= basefontname
104 self
.filename
= filename
105 self
.encoding
= encoding
109 self
.name
= basefontname
111 self
.name
= "%s-%s" % (basefontname
, encoding
.name
)
114 class text_pt(canvas
.canvasitem
):
116 def __init__(self
, x_pt
, y_pt
, font
):
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
)
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
)
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
))
159 def processPDF(self
, file, writer
, context
, registry
, bbox
):
160 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
))