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 copy
, time
, math
25 import style
, version
, type1font
, unit
30 # fallback implementation for Python 2.2 and below
32 return zip(xrange(len(list)), list)
38 # in order to keep a consistent order of the registered resources we
39 # not only store them in a hash but also keep an ordered list (up to a
40 # possible merging of resources, in which case the first instance is
42 self
.resourceshash
= {}
43 self
.resourceslist
= []
45 def add(self
, resource
):
46 rkey
= (resource
.type, resource
.id)
47 if self
.resourceshash
.has_key(rkey
):
48 self
.resourceshash
[rkey
].merge(resource
)
50 self
.resourceshash
[rkey
] = resource
51 self
.resourceslist
.append(resource
)
53 def outputPS(self
, file, writer
):
54 """ write all PostScript code of the prolog resources """
55 for resource
in self
.resourceslist
:
56 resource
.outputPS(file, writer
, self
)
64 """ a PostScript resource """
66 def __init__(self
, type, id):
67 # Every PSresource has to have a type and a unique id.
68 # Resources with the same type and id will be merged
69 # when they are registered in the PSregistry
73 def merge(self
, other
):
74 """ merge self with other, which has to be a resource of the same type and with
78 def outputPS(self
, file, writer
, registry
):
79 raise NotImplementedError("outputPS not implemented for %s" % repr(self
))
82 # Different variants of prolog items
85 class PSdefinition(PSresource
):
87 """ PostScript function definition included in the prolog """
89 def __init__(self
, id, body
):
90 self
.type = "definition"
94 def outputPS(self
, file, writer
, registry
):
95 file.write("%%%%BeginRessource: %s\n" % self
.id)
96 file.write("%(body)s /%(id)s exch def\n" % self
.__dict
__)
97 file.write("%%EndRessource\n")
102 def __init__(self
, font
, chars
, registry
):
104 registry
.add(PSfontfile(font
.basefontname
,
109 registry
.add(_ReEncodeFont
)
110 registry
.add(PSfontencoding(font
.encoding
))
111 registry
.add(PSfontreencoding(font
.name
,
116 class PSfontfile(PSresource
):
118 """ PostScript font definition included in the prolog """
120 def __init__(self
, name
, filename
, encoding
, chars
):
121 """ include type 1 font defined by the following parameters
123 - name: name of the PostScript font
124 - filename: name (without path) of file containing the font definition
125 - encfilename: name (without path) of file containing used encoding of font
126 or None (if no encoding file used)
127 - chars: character list to fill usedchars
131 # Note that here we only need the encoding for selecting the used glyphs!
133 self
.type = "fontfile"
134 self
.id = self
.name
= name
135 self
.filename
= filename
137 self
.encodingfilename
= None
139 self
.encodingfilename
= encoding
.filename
142 self
.usedchars
[char
] = 1
144 def merge(self
, other
):
145 if self
.encodingfilename
== other
.encodingfilename
:
146 self
.usedchars
.update(other
.usedchars
)
148 self
.usedchars
= None # stripping of font not possible
150 def outputPS(self
, file, writer
, registry
):
151 fontfile
= type1font
.fontfile(self
.name
, self
.filename
, self
.usedchars
, self
.encodingfilename
)
152 fontfile
.outputPS(file, writer
, registry
)
155 class PSfontencoding(PSresource
):
157 """ PostScript font encoding vector included in the prolog """
159 def __init__(self
, encoding
):
160 """ include font encoding vector specified by encoding """
162 self
.type = "fontencoding"
163 self
.id = encoding
.name
164 self
.encoding
= encoding
166 def outputPS(self
, file, writer
, registry
):
167 encodingfile
= type1font
.encodingfile(self
.encoding
.name
, self
.encoding
.filename
)
168 encodingfile
.outputPS(file, writer
, registry
)
171 class PSfontreencoding(PSresource
):
173 """ PostScript font re-encoding directive included in the prolog """
175 def __init__(self
, fontname
, basefontname
, encodingname
):
176 """ include font re-encoding directive specified by
178 - fontname: PostScript FontName of the new reencoded font
179 - basefontname: PostScript FontName of the original font
180 - encname: name of the encoding
181 - font: a reference to the font instance (temporarily added for pdf support)
183 Before being able to reencode a font, you have to include the
184 encoding via a fontencoding prolog item with name=encname
188 self
.type = "fontreencoding"
189 self
.id = self
.fontname
= fontname
190 self
.basefontname
= basefontname
191 self
.encodingname
= encodingname
193 def outputPS(self
, file, writer
, registry
):
194 file.write("%%%%BeginProcSet: %s\n" % self
.fontname
)
195 file.write("/%s /%s %s ReEncodeFont\n" % (self
.basefontname
, self
.fontname
, self
.encodingname
))
196 file.write("%%EndProcSet\n")
199 _ReEncodeFont
= PSdefinition("ReEncodeFont", """{
202 /newencoding exch def
203 /newfontname exch def
204 /basefontname exch def
205 /basefontdict basefontname findfont def
206 /newfontdict basefontdict maxlength dict def
208 exch dup dup /FID ne exch /Encoding ne and
209 { exch newfontdict 3 1 roll put }
213 newfontdict /FontName newfontname put
214 newfontdict /Encoding newencoding put
215 newfontname newfontdict definefont pop
222 def __init__(self
, document
, filename
):
223 if len(document
.pages
) != 1:
224 raise ValueError("EPS file can be construced out of a single page document only")
225 page
= document
.pages
[0]
228 if not filename
.endswith(".eps"):
229 filename
= filename
+ ".eps"
231 file = open(filename
, "w")
233 raise IOError("cannot open output file")
236 pagetrafo
= page
.pagetrafo(bbox
)
238 # if a page transformation is necessary, we have to adjust the bounding box
240 if pagetrafo
is not None:
241 bbox
.transform(pagetrafo
)
243 file.write("%!PS-Adobe-3.0 EPSF-3.0\n")
245 file.write("%%%%BoundingBox: %d %d %d %d\n" % bbox
.lowrestuple_pt())
246 file.write("%%%%HiResBoundingBox: %g %g %g %g\n" % bbox
.highrestuple_pt())
247 file.write("%%%%Creator: PyX %s\n" % version
.version
)
248 file.write("%%%%Title: %s\n" % filename
)
249 file.write("%%%%CreationDate: %s\n" %
250 time
.asctime(time
.localtime(time
.time())))
251 file.write("%%EndComments\n")
253 file.write("%%BeginProlog\n")
254 registry
= PSregistry()
255 canvas
.registerPS(registry
)
256 registry
.outputPS(file, self
)
257 file.write("%%EndProlog\n")
260 # apply a possible page transformation
262 pagetrafo
.outputPS(file, self
, acontext
)
264 style
.linewidth
.normal
.outputPS(file, self
, acontext
)
266 # here comes the canvas content
267 canvas
.outputPS(file, self
, acontext
)
269 file.write("showpage\n")
270 file.write("%%Trailer\n")
271 file.write("%%EOF\n")
276 def __init__(self
, document
, filename
):
277 if not filename
.endswith(".ps"):
278 filename
= filename
+ ".ps"
280 file = open(filename
, "w")
282 raise IOError("cannot open output file")
284 # calculated bounding boxes of separate pages and the bounding box of the whole document
286 for page
in document
.pages
:
288 page
._bbox
= page
.bbox()
289 page
._pagetrafo
= page
.pagetrafo(page
._bbox
)
290 # if a page transformation is necessary, we have to adjust the bounding box
293 page
._transformedbbox
= page
._bbox
.transformed(page
._pagetrafo
)
295 page
._transformedbbox
= page
._bbox
296 if page
._transformedbbox
:
298 documentbbox
+= page
._transformedbbox
300 documentbbox
= page
._transformedbbox
.enlarge(0) # make a copy
302 file.write("%!PS-Adobe-3.0\n")
304 file.write("%%%%BoundingBox: %d %d %d %d\n" % documentbbox
.lowrestuple_pt())
305 file.write("%%%%HiResBoundingBox: %g %g %g %g\n" % documentbbox
.highrestuple_pt())
306 file.write("%%%%Creator: PyX %s\n" % version
.version
)
307 file.write("%%%%Title: %s\n" % filename
)
308 file.write("%%%%CreationDate: %s\n" %
309 time
.asctime(time
.localtime(time
.time())))
311 # required paper formats
313 for page
in document
.pages
:
314 paperformats
[page
.paperformat
] = page
.paperformat
317 for paperformat
in paperformats
.values():
319 file.write("%%DocumentMedia: ")
323 file.write("%s %d %d 75 white ()\n" % (paperformat
.name
,
324 unit
.topt(paperformat
.width
),
325 unit
.topt(paperformat
.height
)))
327 # file.write(%%DocumentNeededResources: ") # register not downloaded fonts here
329 file.write("%%%%Pages: %d\n" % len(document
.pages
))
330 file.write("%%PageOrder: Ascend\n")
331 file.write("%%EndComments\n")
333 # document defaults section
334 #file.write("%%BeginDefaults\n")
335 #file.write("%%EndDefaults\n")
337 # document prolog section
338 file.write("%%BeginProlog\n")
339 registry
= PSregistry()
340 for page
in document
.pages
:
341 page
.canvas
.registerPS(registry
)
342 registry
.outputPS(file, self
)
343 file.write("%%EndProlog\n")
345 # document setup section
346 #file.write("%%BeginSetup\n")
347 #file.write("%%EndSetup\n")
350 for nr
, page
in enumerate(document
.pages
):
351 file.write("%%%%Page: %s %d\n" % (page
.pagename
is None and str(nr
+1) or page
.pagename
, nr
+1))
352 file.write("%%%%PageMedia: %s\n" % page
.paperformat
.name
)
353 file.write("%%%%PageOrientation: %s\n" % (page
.rotated
and "Landscape" or "Portrait"))
354 if page
._transformedbbox
:
355 file.write("%%%%PageBoundingBox: %d %d %d %d\n" % page
._transformedbbox
.lowrestuple_pt())
358 file.write("%%BeginPageSetup\n")
359 file.write("/pgsave save def\n")
362 # apply a possible page transformation
363 if page
._pagetrafo
is not None:
364 page
._pagetrafo
.outputPS(file, self
, acontext
)
366 style
.linewidth
.normal
.outputPS(file, self
, acontext
)
367 file.write("%%EndPageSetup\n")
369 # here comes the actual content
370 page
.canvas
.outputPS(file, self
, acontext
)
371 file.write("pgsave restore\n")
372 file.write("showpage\n")
373 file.write("%%PageTrailer\n")
375 file.write("%%Trailer\n")
376 file.write("%%EOF\n")
381 self
.linewidth_pt
= None
382 self
.colorspace
= None
385 def __call__(self
, **kwargs
):
386 newcontext
= copy
.copy(self
)
387 for key
, value
in kwargs
.items():
388 setattr(newcontext
, key
, value
)