one more change to the multipage question
[PyX/mjg.git] / pyx / pswriter.py
blob5cff1b23e19a16c5b451643bb2af173879aeff4a
1 #!/usr/bin/env python
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
27 try:
28 enumerate([])
29 except NameError:
30 # fallback implementation for Python 2.2 and below
31 def enumerate(list):
32 return zip(xrange(len(list)), list)
35 class PSregistry:
37 def __init__(self):
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
41 # kept)
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)
49 else:
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)
59 # Abstract base class
62 class PSresource:
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
70 self.type = type
71 self.id = id
73 def merge(self, other):
74 """ merge self with other, which has to be a resource of the same type and with
75 the same id"""
76 pass
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"
91 self.id = id
92 self.body = body
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")
100 class PSfont:
102 def __init__(self, font, chars, registry):
103 if font.filename:
104 registry.add(PSfontfile(font.basefontname,
105 font.filename,
106 font.encoding,
107 chars))
108 if font.encoding:
109 registry.add(_ReEncodeFont)
110 registry.add(PSfontencoding(font.encoding))
111 registry.add(PSfontreencoding(font.name,
112 font.basefontname,
113 font.encoding.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
136 if encoding is None:
137 self.encodingfilename = None
138 else:
139 self.encodingfilename = encoding.filename
140 self.usedchars = {}
141 for char in chars:
142 self.usedchars[char] = 1
144 def merge(self, other):
145 if self.encodingfilename == other.encodingfilename:
146 self.usedchars.update(other.usedchars)
147 else:
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", """{
200 5 dict
201 begin
202 /newencoding exch def
203 /newfontname exch def
204 /basefontname exch def
205 /basefontdict basefontname findfont def
206 /newfontdict basefontdict maxlength dict def
207 basefontdict {
208 exch dup dup /FID ne exch /Encoding ne and
209 { exch newfontdict 3 1 roll put }
210 { pop pop }
211 ifelse
212 } forall
213 newfontdict /FontName newfontname put
214 newfontdict /Encoding newencoding put
215 newfontname newfontdict definefont pop
217 }""")
220 class epswriter:
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]
226 canvas = page.canvas
228 if not filename.endswith(".eps"):
229 filename = filename + ".eps"
230 try:
231 file = open(filename, "w")
232 except IOError:
233 raise IOError("cannot open output file")
235 bbox = page.bbox()
236 pagetrafo = page.pagetrafo(bbox)
238 # if a page transformation is necessary, we have to adjust the bounding box
239 # accordingly
240 if pagetrafo is not None:
241 bbox.transform(pagetrafo)
243 file.write("%!PS-Adobe-3.0 EPSF-3.0\n")
244 if bbox:
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")
259 acontext = context()
260 # apply a possible page transformation
261 if pagetrafo:
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")
274 class pswriter:
276 def __init__(self, document, filename, writebbox=0):
277 if not filename.endswith(".ps"):
278 filename = filename + ".ps"
279 try:
280 file = open(filename, "w")
281 except IOError:
282 raise IOError("cannot open output file")
284 # calculated bounding boxes of separate pages and the bounding box of the whole document
285 documentbbox = None
286 for page in document.pages:
287 canvas = page.canvas
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
291 # accordingly
292 if page._pagetrafo:
293 page._transformedbbox = page._bbox.transformed(page._pagetrafo)
294 else:
295 page._transformedbbox = page._bbox
296 if page._transformedbbox:
297 if documentbbox:
298 documentbbox += page._transformedbbox
299 else:
300 documentbbox = page._transformedbbox.enlarge(0) # make a copy
302 file.write("%!PS-Adobe-3.0\n")
303 if documentbbox and writebbox:
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
312 paperformats = {}
313 for page in document.pages:
314 if page.paperformat:
315 paperformats[page.paperformat] = page.paperformat
317 first = 1
318 for paperformat in paperformats.values():
319 if first:
320 file.write("%%DocumentMedia: ")
321 first = 0
322 else:
323 file.write("%%+ ")
324 file.write("%s %d %d 75 white ()\n" % (paperformat.name,
325 unit.topt(paperformat.width),
326 unit.topt(paperformat.height)))
328 # file.write(%%DocumentNeededResources: ") # register not downloaded fonts here
330 file.write("%%%%Pages: %d\n" % len(document.pages))
331 file.write("%%PageOrder: Ascend\n")
332 file.write("%%EndComments\n")
334 # document defaults section
335 #file.write("%%BeginDefaults\n")
336 #file.write("%%EndDefaults\n")
338 # document prolog section
339 file.write("%%BeginProlog\n")
340 registry = PSregistry()
341 for page in document.pages:
342 page.canvas.registerPS(registry)
343 registry.outputPS(file, self)
344 file.write("%%EndProlog\n")
346 # document setup section
347 #file.write("%%BeginSetup\n")
348 #file.write("%%EndSetup\n")
350 # pages section
351 for nr, page in enumerate(document.pages):
352 file.write("%%%%Page: %s %d\n" % (page.pagename is None and str(nr+1) or page.pagename, nr+1))
353 if page.paperformat:
354 file.write("%%%%PageMedia: %s\n" % page.paperformat.name)
355 file.write("%%%%PageOrientation: %s\n" % (page.rotated and "Landscape" or "Portrait"))
356 if page._transformedbbox and writebbox:
357 file.write("%%%%PageBoundingBox: %d %d %d %d\n" % page._transformedbbox.lowrestuple_pt())
359 # page setup section
360 file.write("%%BeginPageSetup\n")
361 file.write("/pgsave save def\n")
363 acontext = context()
364 # apply a possible page transformation
365 if page._pagetrafo is not None:
366 page._pagetrafo.outputPS(file, self, acontext)
368 style.linewidth.normal.outputPS(file, self, acontext)
369 file.write("%%EndPageSetup\n")
371 # here comes the actual content
372 page.canvas.outputPS(file, self, acontext)
373 file.write("pgsave restore\n")
374 file.write("showpage\n")
375 file.write("%%PageTrailer\n")
377 file.write("%%Trailer\n")
378 file.write("%%EOF\n")
380 class context:
382 def __init__(self):
383 self.linewidth_pt = None
384 self.colorspace = None
385 self.font = None
387 def __call__(self, **kwargs):
388 newcontext = copy.copy(self)
389 for key, value in kwargs.items():
390 setattr(newcontext, key, value)
391 return newcontext