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>
6 # Copyright (C) 2007 Michael Schindler <m-schindler@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
, slant
, metric
):
103 self
.basefontname
= basefontname
104 self
.filename
= filename
105 self
.encoding
= encoding
106 self
.slant
= slant
# None or a float
109 if encoding
is not None and slant
is not None:
110 self
.encname
= "%s-%s" % (basefontname
, encoding
.name
)
111 self
.name
= "%s-slant%f" % (self
.encname
, self
.slant
)
112 elif encoding
is not None:
113 self
.name
= "%s-%s" % (basefontname
, encoding
.name
)
114 elif slant
is not None:
115 self
.name
= "%s-slant%f" % (basefontname
, self
.slant
)
117 self
.name
= basefontname
120 class text_pt(canvas
.canvasitem
):
122 def __init__(self
, x_pt
, y_pt
, font
):
131 def addchar(self
, char
):
132 metric
= self
.font
.metric
133 self
.width_pt
+= metric
.getwidth_pt(char
)
134 cheight_pt
= metric
.getwidth_pt(char
)
135 if cheight_pt
> self
.height_pt
:
136 self
.height_pt
= cheight_pt
137 cdepth_pt
= metric
.getdepth_pt(char
)
138 if cdepth_pt
> self
.depth_pt
:
139 self
.depth_pt
= cdepth_pt
140 self
.chars
.append(char
)
143 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
)
145 def processPS(self
, file, writer
, context
, registry
, bbox
):
146 # note that we don't register PSfont as it is just a helper resource
147 # which registers the needed components
148 pswriter
.PSfont(self
.font
, self
.chars
, registry
)
151 if ( context
.font
is None or
152 context
.font
.name
!= self
.font
.name
or
153 context
.font
.metric
.getsize_pt() != self
.font
.metric
.getsize_pt() ):
154 file.write("/%s %f selectfont\n" % (self
.font
.name
, self
.font
.metric
.getsize_pt()))
155 context
.font
= self
.font
157 for char
in self
.chars
:
158 if char
> 32 and char
< 127 and chr(char
) not in "()[]<>\\":
159 ascii
= "%s" % chr(char
)
161 ascii
= "\\%03o" % char
163 file.write("%g %g moveto (%s) show\n" % (self
.x_pt
, self
.y_pt
, outstring
))
165 def processPDF(self
, file, writer
, context
, registry
, bbox
):
166 registry
.add(pdfwriter
.PDFfont(self
.font
, self
.chars
, writer
, registry
))
169 if ( context
.font
is None or
170 context
.font
.name
!= self
.font
.name
or
171 context
.font
.metric
.getsize_pt() != self
.font
.metric
.getsize_pt() ):
172 file.write("/%s %f Tf\n" % (self
.font
.name
, self
.font
.metric
.getsize_pt()))
173 context
.font
= self
.font
175 for char
in self
.chars
:
176 if 32 <= char
<= 127 and chr(char
) not in "()[]<>\\":
177 ascii
= "%s" % chr(char
)
179 ascii
= "\\%03o" % char
181 if self
.font
.slant
is None:
184 slantvalue
= self
.font
.slant
185 file.write("1 0 %f 1 %f %f Tm (%s) Tj\n" % (slantvalue
, self
.x_pt
, self
.y_pt
, outstring
))