some further work on a preliminary font inclusion in pdf
[PyX/mjg.git] / pyx / canvas.py
blob69a89aeb2df7ef8c8e7efd83317f0706e4b774d2
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2002-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 # XXX what are the correct base classes of clip and pattern
26 """The canvas module provides a PostScript canvas class and related classes
28 A canvas holds a collection of all elements that should be displayed together
29 with their attributes.
30 """
32 import sys, cStringIO, math, time
33 import attr, base, bbox, deco, unit, prolog, style, trafo, version
35 # temporary for pdf fonts:
36 import cStringIO
37 from t1strip import fullfont
38 import pykpathsea
40 try:
41 enumerate([])
42 except NameError:
43 # fallback implementation for Python 2.2. and below
44 def enumerate(list):
45 return zip(xrange(len(list)), list)
47 # known paperformats as tuple (width, height)
49 _paperformats = { "A4" : (210 * unit.t_mm, 297 * unit.t_mm),
50 "A3" : (297 * unit.t_mm, 420 * unit.t_mm),
51 "A2" : (420 * unit.t_mm, 594 * unit.t_mm),
52 "A1" : (594 * unit.t_mm, 840 * unit.t_mm),
53 "A0" : (840 * unit.t_mm, 1188 * unit.t_mm),
54 "A0b" : (910 * unit.t_mm, 1370 * unit.t_mm),
55 "Letter" : (8.5 * unit.t_inch, 11 * unit.t_inch),
56 "Legal" : (8.5 * unit.t_inch, 14 * unit.t_inch)}
59 # clipping class
62 class clip(base.PSCmd):
64 """class for use in canvas constructor which clips to a path"""
66 def __init__(self, path):
67 """construct a clip instance for a given path"""
68 self.path = path
70 def bbox(self):
71 # as a PSCmd a clipping path has NO influence on the bbox...
72 return None
74 def clipbbox(self):
75 # ... but for clipping, we nevertheless need the bbox
76 return self.path.bbox()
78 def outputPS(self, file):
79 file.write("newpath\n")
80 self.path.outputPS(file)
81 file.write("clip\n")
83 def outputPDF(self, file):
84 self.path.outputPDF(file)
85 file.write("W n\n")
88 # general canvas class
91 class _canvas(base.PSCmd):
93 """a canvas is a collection of PSCmds together with PSAttrs"""
95 def __init__(self, attrs=[], texrunner=None):
97 """construct a canvas
99 The canvas can be modfied by supplying args, which have
100 to be instances of one of the following classes:
101 - trafo.trafo (leading to a global transformation of the canvas)
102 - canvas.clip (clips the canvas)
103 - base.PathStyle (sets some global attributes of the canvas)
105 Note that, while the first two properties are fixed for the
106 whole canvas, the last one can be changed via canvas.set().
108 The texrunner instance used for the text method can be specified
109 using the texrunner argument. It defaults to text.defaulttexrunner
113 self.PSOps = []
114 self.trafo = trafo.trafo()
115 self.clipbbox = None
116 if texrunner is not None:
117 self.texrunner = texrunner
118 else:
119 # prevent cyclic imports
120 import text
121 self.texrunner = text.defaulttexrunner
123 for attr in attrs:
124 if isinstance(attr, trafo.trafo_pt):
125 self.trafo = self.trafo*attr
126 self.PSOps.append(attr)
127 elif isinstance(attr, clip):
128 if self.clipbbox is None:
129 self.clipbbox = attr.clipbbox().transformed(self.trafo)
130 else:
131 self.clippbox *= attr.clipbbox().transformed(self.trafo)
132 self.PSOps.append(attr)
133 else:
134 self.set([attr])
136 def bbox(self):
137 """returns bounding box of canvas"""
138 obbox = None
139 for cmd in self.PSOps:
140 if isinstance(cmd, base.PSCmd):
141 abbox = cmd.bbox()
142 if obbox is None:
143 obbox = abbox
144 elif abbox is not None:
145 obbox += abbox
147 # transform according to our global transformation and
148 # intersect with clipping bounding box (which have already been
149 # transformed in canvas.__init__())
150 if obbox is not None and self.clipbbox is not None:
151 return obbox.transformed(self.trafo)*self.clipbbox
152 elif obbox is not None:
153 return obbox.transformed(self.trafo)
154 else:
155 return self.clipbbox
157 def prolog(self):
158 result = []
159 for cmd in self.PSOps:
160 result.extend(cmd.prolog())
161 return result
163 def outputPS(self, file):
164 if self.PSOps:
165 file.write("gsave\n")
166 for cmd in self.PSOps:
167 cmd.outputPS(file)
168 file.write("grestore\n")
170 def outputPDF(self, file):
171 if self.PSOps:
172 file.write("q\n") # gsave
173 for cmd in self.PSOps:
174 cmd.outputPDF(file)
175 file.write("Q\n") # grestore
177 def insert(self, PSOp, attrs=[]):
178 """insert PSOp in the canvas.
180 If attrss are given, a canvas containing the PSOp is inserted applying attrs.
182 returns the PSOp
186 # XXX check for PSOp
188 if attrs:
189 sc = _canvas(attrs)
190 sc.insert(PSOp)
191 self.PSOps.append(sc)
192 else:
193 self.PSOps.append(PSOp)
195 return PSOp
197 def set(self, attrs):
198 """sets styles args globally for the rest of the canvas
201 attr.checkattrs(attrs, [style.strokestyle, style.fillstyle])
202 for astyle in attrs:
203 self.insert(astyle)
205 def draw(self, path, attrs):
206 """draw path on canvas using the style given by args
208 The argument attrs consists of PathStyles, which modify
209 the appearance of the path, PathDecos, which add some new
210 visual elements to the path, or trafos, which are applied
211 before drawing the path.
215 attrs = attr.mergeattrs(attrs)
216 attr.checkattrs(attrs, [deco.deco, style.fillstyle, style.strokestyle, trafo.trafo_pt])
218 for t in attr.getattrs(attrs, [trafo.trafo_pt]):
219 path = path.transformed(t)
221 dp = deco.decoratedpath(path)
223 # set global styles
224 dp.styles = attr.getattrs(attrs, [style.fillstyle, style.strokestyle])
226 # add path decorations and modify path accordingly
227 for adeco in attr.getattrs(attrs, [deco.deco]):
228 dp = adeco.decorate(dp)
230 self.insert(dp)
232 def stroke(self, path, attrs=[]):
233 """stroke path on canvas using the style given by args
235 The argument attrs consists of PathStyles, which modify
236 the appearance of the path, PathDecos, which add some new
237 visual elements to the path, or trafos, which are applied
238 before drawing the path.
242 self.draw(path, [deco.stroked]+list(attrs))
244 def fill(self, path, attrs=[]):
245 """fill path on canvas using the style given by args
247 The argument attrs consists of PathStyles, which modify
248 the appearance of the path, PathDecos, which add some new
249 visual elements to the path, or trafos, which are applied
250 before drawing the path.
254 self.draw(path, [deco.filled]+list(attrs))
256 def settexrunner(self, texrunner):
257 """sets the texrunner to be used to within the text and text_pt methods"""
259 self.texrunner = texrunner
261 def text(self, x, y, atext, *args, **kwargs):
262 """insert a text into the canvas
264 inserts a textbox created by self.texrunner.text into the canvas
266 returns the inserted textbox"""
268 return self.insert(self.texrunner.text(x, y, atext, *args, **kwargs))
271 def text_pt(self, x, y, atext, *args):
272 """insert a text into the canvas
274 inserts a textbox created by self.texrunner.text_pt into the canvas
276 returns the inserted textbox"""
278 return self.insert(self.texrunner.text_pt(x, y, atext, *args))
281 # canvas for patterns
284 class pattern(_canvas, attr.exclusiveattr, style.fillstyle):
286 def __init__(self, painttype=1, tilingtype=1, xstep=None, ystep=None, bbox=None, trafo=None):
287 attr.exclusiveattr.__init__(self, pattern)
288 _canvas.__init__(self)
289 attr.exclusiveattr.__init__(self, pattern)
290 self.id = "pattern%d" % id(self)
291 if painttype not in (1,2):
292 raise ValueError("painttype must be 1 or 2")
293 self.painttype = painttype
294 if tilingtype not in (1,2,3):
295 raise ValueError("tilingtype must be 1, 2 or 3")
296 self.tilingtype = tilingtype
297 self.xstep = xstep
298 self.ystep = ystep
299 self.patternbbox = bbox
300 self.patterntrafo = trafo
302 def bbox(self):
303 return None
305 def outputPS(self, file):
306 file.write("%s setpattern\n" % self.id)
308 def prolog(self):
309 realpatternbbox = _canvas.bbox(self)
310 if self.xstep is None:
311 xstep = unit.topt(realpatternbbox.width())
312 else:
313 xstep = unit.topt(unit.length(self.xstep))
314 if self.ystep is None:
315 ystep = unit.topt(realpatternbbox.height())
316 else:
317 ystep = unit.topt(unit.length(self.ystep))
318 if not xstep:
319 raise ValueError("xstep in pattern cannot be zero")
320 if not ystep:
321 raise ValueError("ystep in pattern cannot be zero")
322 patternbbox = self.patternbbox or realpatternbbox.enlarged("5 pt")
324 patternprefix = "\n".join(("<<",
325 "/PatternType 1",
326 "/PaintType %d" % self.painttype,
327 "/TilingType %d" % self.tilingtype,
328 "/BBox[%s]" % str(patternbbox),
329 "/XStep %g" % xstep,
330 "/YStep %g" % ystep,
331 "/PaintProc {\nbegin\n"))
332 stringfile = cStringIO.StringIO()
333 _canvas.outputPS(self, stringfile)
334 patternproc = stringfile.getvalue()
335 stringfile.close()
336 patterntrafostring = self.patterntrafo is None and "matrix" or str(self.patterntrafo)
337 patternsuffix = "end\n} bind\n>>\n%s\nmakepattern" % patterntrafostring
339 pr = _canvas.prolog(self)
340 pr.append(prolog.definition(self.id, "".join((patternprefix, patternproc, patternsuffix))))
341 return pr
343 pattern.clear = attr.clearclass(pattern)
345 # helper function
347 def calctrafo(abbox, paperformat, margin, rotated, fittosize):
348 """ calculate a trafo which rotates and fits a canvas with
349 bounding box abbox on the given paperformat with a margin on all
350 sides"""
351 margin = unit.length(margin)
352 atrafo = None # global transformation of canvas
354 if rotated:
355 atrafo = trafo.rotate(90, *abbox.center())
357 if paperformat:
358 # center (optionally rotated) output on page
359 try:
360 paperwidth, paperheight = _paperformats[paperformat.capitalize()]
361 except KeyError:
362 raise KeyError, "unknown paperformat '%s'" % paperformat
364 paperwidth -= 2*margin
365 paperheight -= 2*margin
367 if not atrafo: atrafo = trafo.trafo()
369 atrafo = atrafo.translated(margin + 0.5*(paperwidth - abbox.width()) - abbox.left(),
370 margin + 0.5*(paperheight - abbox.height()) - abbox.bottom())
372 if fittosize:
373 # scale output to pagesize - margins
374 if 2*margin > min(paperwidth, paperheight):
375 raise RuntimeError("Margins too broad for selected paperformat. Aborting.")
377 if rotated:
378 sfactor = min(unit.topt(paperheight)/unit.topt(abbox.width()),
379 unit.topt(paperwidth)/unit.topt(abbox.height()))
380 else:
381 sfactor = min(unit.topt(paperwidth)/unit.topt(abbox.width()),
382 unit.topt(paperheight)/unit.topt(abbox.height()))
384 atrafo = atrafo.scaled(sfactor, sfactor, margin + 0.5*paperwidth, margin + 0.5*paperheight)
385 elif fittosize:
386 raise ValueError("must specify paper size for fittosize")
388 return atrafo
391 # The main canvas class
394 class canvas(_canvas):
396 """a canvas is a collection of PSCmds together with PSAttrs"""
398 def writeEPSfile(self, filename, paperformat=None, rotated=0, fittosize=0, margin="1 t cm",
399 bbox=None, bboxenlarge="1 t pt"):
400 """write canvas to EPS file
402 If paperformat is set to a known paperformat, the output will be centered on
403 the page.
405 If rotated is set, the output will first be rotated by 90 degrees.
407 If fittosize is set, then the output is scaled to the size of the
408 page (minus margin). In that case, the paperformat the specification
409 of the paperformat is obligatory.
411 The bbox parameter overrides the automatic bounding box determination.
412 bboxenlarge may be used to enlarge the bbox of the canvas (or the
413 manually specified bbox).
416 if filename[-4:]!=".eps":
417 filename = filename + ".eps"
419 try:
420 file = open(filename, "w")
421 except IOError:
422 raise IOError("cannot open output file")
424 abbox = bbox is not None and bbox or self.bbox()
425 abbox.enlarge(bboxenlarge)
426 ctrafo = calctrafo(abbox, paperformat, margin, rotated, fittosize)
428 # if there has been a global transformation, adjust the bounding box
429 # accordingly
430 if ctrafo: abbox.transform(ctrafo)
432 file.write("%!PS-Adobe-3.0 EPSF 3.0\n")
433 abbox.outputPS(file)
434 file.write("%%%%Creator: PyX %s\n" % version.version)
435 file.write("%%%%Title: %s\n" % filename)
436 file.write("%%%%CreationDate: %s\n" %
437 time.asctime(time.localtime(time.time())))
438 file.write("%%EndComments\n")
440 file.write("%%BeginProlog\n")
442 mergedprolog = []
444 for pritem in self.prolog():
445 for mpritem in mergedprolog:
446 if mpritem.merge(pritem) is None: break
447 else:
448 mergedprolog.append(pritem)
450 for pritem in mergedprolog:
451 pritem.outputPS(file)
453 file.write("%%EndProlog\n")
455 # apply a possible global transformation
456 if ctrafo: ctrafo.outputPS(file)
458 file.write("%f setlinewidth\n" % unit.topt(style.linewidth.normal))
460 # here comes the actual content
461 self.outputPS(file)
463 file.write("showpage\n")
464 file.write("%%Trailer\n")
465 file.write("%%EOF\n")
467 def writePDFfile(self, filename, paperformat=None, rotated=0, fittosize=0, margin="1 t cm",
468 bbox=None, bboxenlarge="1 t pt"):
469 sys.stderr.write("*** PyX Warning: writePDFfile is experimental and supports only a subset of PyX's features\n")
471 if filename[-4:]!=".pdf":
472 filename = filename + ".pdf"
474 try:
475 file = open(filename, "wb")
476 except IOError:
477 raise IOError("cannot open output file")
479 abbox = bbox is not None and bbox or self.bbox()
480 abbox.enlarge(bboxenlarge)
482 ctrafo = calctrafo(abbox, paperformat, margin, rotated, fittosize)
484 # if there has been a global transformation, adjust the bounding box
485 # accordingly
486 if ctrafo: abbox.transform(ctrafo)
488 mergedprolog = []
490 for pritem in self.prolog():
491 for mpritem in mergedprolog:
492 if mpritem.merge(pritem) is None: break
493 else:
494 mergedprolog.append(pritem)
496 file.write("%%PDF-1.4\n%%%s%s%s%s\n" % (chr(195), chr(182), chr(195), chr(169)))
497 reflist = [file.tell()]
498 file.write("1 0 obj\n"
499 "<<\n"
500 "/Type /Catalog\n"
501 "/Outlines 2 0 R\n"
502 "/Pages 3 0 R\n"
503 ">>\n"
504 "endobj\n")
505 reflist.append(file.tell())
506 file.write("2 0 obj\n"
507 "<<\n"
508 "/Type Outlines\n"
509 "/Count 0\n"
510 ">>\n"
511 "endobj\n")
512 reflist.append(file.tell())
513 file.write("3 0 obj\n"
514 "<<\n"
515 "/Type /Pages\n"
516 "/Kids [4 0 R]\n"
517 "/Count 1\n"
518 ">>\n"
519 "endobj\n")
520 reflist.append(file.tell())
521 file.write("4 0 obj\n"
522 "<<\n"
523 "/Type /Page\n"
524 "/Parent 3 0 R\n")
525 abbox.outputPDF(file)
526 file.write("/Contents 5 0 R\n"
527 "/Resources <<\n"
528 "/ProcSet 7 0 R\n")
530 fontnr = 0
531 for pritem in mergedprolog:
532 if isinstance(pritem, prolog.fontreencoding):
533 fontnr += 1
534 file.write("/Font << /%s %d 0 R>>\n" % (pritem.fontname, fontnr+7))
535 fontnr += 3 # further objects due to a font
537 file.write(">>\n"
538 ">>\n"
539 "endobj\n")
540 reflist.append(file.tell())
541 file.write("5 0 obj\n"
542 "<< /Length 6 0 R >>\n"
543 "stream\n")
544 streamstartpos = file.tell()
546 # apply a possible global transformation
547 if ctrafo: ctrafo.outputPDF(file)
548 style.linewidth.normal.outputPDF(file)
550 self.outputPDF(file)
551 streamendpos = file.tell()
552 file.write("endstream\n"
553 "endobj\n")
554 reflist.append(file.tell())
555 file.write("6 0 obj\n"
556 "%s\n"
557 "endobj\n" % (streamendpos - streamstartpos))
558 reflist.append(file.tell())
559 file.write("7 0 obj\n"
560 "[/PDF /Text]\n"
561 "endobj\n")
563 fontnr = 0
564 for pritem in mergedprolog:
565 if isinstance(pritem, prolog.fontreencoding):
566 fontnr += 1
567 reflist.append(file.tell())
568 file.write("%d 0 obj\n"
569 "<<\n"
570 "/Type /Font\n"
571 "/Subtype /Type1\n"
572 "/Name /%s\n"
573 "/BaseFont /%s\n"
574 "/FirstChar 0\n"
575 "/LastChar 255\n"
576 "/Widths %d 0 R\n"
577 "/FontDescriptor %d 0 R\n"
578 "/Encoding /MacRomanEncoding\n" # FIXME
579 ">>\n"
580 "endobj\n" % (fontnr+7, pritem.fontname, pritem.basefontname, fontnr+8, fontnr+9))
581 fontnr += 1
582 reflist.append(file.tell())
583 file.write("%d 0 obj\n"
584 "[ %s ]\n"
585 "endobj\n" % (fontnr+7, " ".join(["255" for i in range(256)])))
586 fontnr += 1
587 reflist.append(file.tell())
588 file.write("%d 0 obj\n"
589 "<<\n"
590 "/Type /FontDescriptor\n"
591 "/FontName /%s\n"
592 "/Flags 0\n" # FIXME
593 "/FontBBox [0 -5 20 10]\n" # FIXME
594 "/ItalicAngle 0\n" # FIXME
595 "/Ascent 20\n" # FIXME
596 "/Descent -5\n" # FIXME
597 "/CapHeight 15\n" # FIXME
598 "/StemV 1\n" # FIXME
599 "/FontFile %d 0 R\n" # FIXME
600 # "/CharSet \n" # fill in when stripping
601 ">>\n"
602 "endobj\n" % (fontnr+7, pritem.fontname, fontnr+8))
604 fontnr += 1
605 reflist.append(file.tell())
607 # we need the lengths before the stream (we do not want to
608 # create another set of references)
609 # hence we use a StringIO here
610 fontfile = cStringIO.StringIO()
611 fullfilename = pykpathsea.find_file(pritem.basefontname.lower(), # FIXME, FIXME, FIXME !!!
612 pykpathsea.kpse_type1_format)
613 lengths = fullfont.fullfont(fontfile, fullfilename)
614 if len(lengths) != 4:
615 raise RuntimeError("bad number of blocks in pdf file")
617 file.write("%d 0 obj\n"
618 "<<\n"
619 "/Length %d\n"
620 "/Length1 %d\n"
621 "/Length2 %d\n"
622 "/Length3 %d\n"
623 ">>\n"
624 "stream\n" % (fontnr+7, lengths[3]-lengths[0],
625 lengths[1]-lengths[0],
626 lengths[2]-lengths[1],
627 lengths[3]-lengths[2]))
628 file.write(fontfile.getvalue())
629 file.write("endstream\n"
630 "endobj\n")
632 xrefpos = file.tell()
633 file.write("xref\n"
634 "0 %d\n" % (len(reflist)+1))
635 file.write("0000000000 65535 f \n")
636 for ref in reflist:
637 file.write("%010i 00000 n \n" % ref)
638 file.write("trailer\n"
639 "<<\n"
640 "/Size 8\n"
641 "/Root 1 0 R\n"
642 ">>\n"
643 "startxref\n"
644 "%i\n"
645 "%%%%EOF\n" % xrefpos)
647 def writetofile(self, filename, *args, **kwargs):
648 if filename[-4:] == ".eps":
649 self.writeEPSfile(filename, *args, **kwargs)
650 elif filename[-4:] == ".pdf":
651 self.writePDFfile(filename, *args, **kwargs)
652 else:
653 sys.stderr.write("*** PyX Warning: deprecated usage of writetofile -- writetofile needs a filename extension or use an explicit call to writeEPSfile or the like\n")
654 self.writeEPSfile(filename, *args, **kwargs)
656 class page(canvas):
658 def __init__(self, attrs=[], texrunner=None, pagename=None, paperformat="a4", rotated=0, fittosize=0,
659 margin="1 t cm", bboxenlarge="1 t pt"):
660 canvas.__init__(self, attrs, texrunner)
661 self.pagename = pagename
662 self.paperformat = paperformat.capitalize()
663 self.rotated = rotated
664 self.fittosize = fittosize
665 self.margin = margin
666 self.bboxenlarge = bboxenlarge
668 def bbox(self):
669 # the bounding box of a page is fixed by its format and an optional rotation
670 pbbox = bbox.bbox(0, 0, *_paperformats[self.paperformat])
671 pbbox.enlarge(self.bboxenlarge)
672 if self.rotated:
673 pbbox.transform(trafo.rotate(90, *pbbox.center()))
674 return pbbox
676 def outputPS(self, file):
677 file.write("%%%%PageMedia: %s\n" % self.paperformat)
678 file.write("%%%%PageOrientation: %s\n" % (self.rotated and "Landscape" or "Portrait"))
679 # file.write("%%%%PageBoundingBox: %d %d %d %d\n" % (math.floor(pbbox.llx_pt), math.floor(pbbox.lly_pt),
680 # math.ceil(pbbox.urx_pt), math.ceil(pbbox.ury_pt)))
682 # page setup section
683 file.write("%%BeginPageSetup\n")
684 file.write("/pgsave save def\n")
685 # for scaling, we need the real bounding box of the page contents
686 pbbox = canvas.bbox(self)
687 pbbox.enlarge(self.bboxenlarge)
688 ptrafo = calctrafo(pbbox, self.paperformat, self.margin, self.rotated, self.fittosize)
689 if ptrafo:
690 ptrafo.outputPS(file)
691 file.write("%f setlinewidth\n" % unit.topt(style.linewidth.normal))
692 file.write("%%EndPageSetup\n")
694 # here comes the actual content
695 canvas.outputPS(self, file)
696 file.write("pgsave restore\n")
697 file.write("showpage\n")
698 # file.write("%%PageTrailer\n")
701 class document:
703 """holds a collection of page instances which are output as pages of a document"""
705 def __init__(self, pages=[]):
706 self.pages = pages
708 def append(self, page):
709 self.pages.append(page)
711 def writePSfile(self, filename):
712 """write pages to PS file """
714 if filename[-3:]!=".ps":
715 filename = filename + ".ps"
717 try:
718 file = open(filename, "w")
719 except IOError:
720 raise IOError("cannot open output file")
722 docbbox = None
723 for apage in self.pages:
724 pbbox = apage.bbox()
725 if docbbox is None:
726 docbbox = pbbox
727 else:
728 docbbox += pbbox
730 # document header
731 file.write("%!PS-Adobe-3.0\n")
732 docbbox.outputPS(file)
733 file.write("%%%%Creator: PyX %s\n" % version.version)
734 file.write("%%%%Title: %s\n" % filename)
735 file.write("%%%%CreationDate: %s\n" %
736 time.asctime(time.localtime(time.time())))
737 # required paper formats
738 paperformats = {}
739 for apage in self.pages:
740 if isinstance(apage, page):
741 paperformats[apage.paperformat] = _paperformats[apage.paperformat]
742 first = 1
743 for paperformat, size in paperformats.items():
744 if first:
745 file.write("%%DocumentMedia: ")
746 first = 0
747 else:
748 file.write("%%+ ")
749 file.write("%s %d %d 75 white ()\n" % (paperformat, unit.topt(size[0]), unit.topt(size[1])))
751 file.write("%%%%Pages: %d\n" % len(self.pages))
752 file.write("%%PageOrder: Ascend\n")
753 file.write("%%EndComments\n")
755 # document default section
756 #file.write("%%BeginDefaults\n")
757 #if paperformat:
758 # file.write("%%%%PageMedia: %s\n" % paperformat)
759 #file.write("%%%%PageOrientation: %s\n" % (rotated and "Landscape" or "Portrait"))
760 #file.write("%%EndDefaults\n")
762 # document prolog section
763 file.write("%%BeginProlog\n")
764 mergedprolog = []
765 for apage in self.pages:
766 for pritem in apage.prolog():
767 for mpritem in mergedprolog:
768 if mpritem.merge(pritem) is None: break
769 else:
770 mergedprolog.append(pritem)
771 for pritem in mergedprolog:
772 pritem.outputPS(file)
773 file.write("%%EndProlog\n")
775 # document setup section
776 #file.write("%%BeginSetup\n")
777 #file.write("%%EndSetup\n")
779 # pages section
780 for nr, apage in enumerate(self.pages):
781 file.write("%%%%Page: %s %d\n" % (apage.pagename is None and str(nr) or apage.pagename , nr+1))
782 apage.outputPS(file)
784 file.write("%%Trailer\n")
785 file.write("%%EOF\n")