some typos corrected
[PyX/mjg.git] / contrib / dvips.py
blob2979a786bd42a31fce9c8e0792d0c88ba1a36f6e
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2003 Jörg Lehmann <joergl@users.sourceforge.net>
7 # epstopng is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # epstopng is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with epstopng; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # TODO: - set dpi in png (don't know how to do this in PIL)
22 # - this is much too slow --- consider a rewrite in C
25 import getopt, sys
26 from pyx import dvifile, canvas, trafo, unit
29 progname = "dvips v0.1: dvi to ps converter based on PyX"
32 def dvips(dviname, psname, vshift="-1 t cm"):
33 fontmap = dvifile.readfontmap(["psfonts.map"])
34 df = dvifile.dvifile(dviname, fontmap=fontmap)
35 d = canvas.document()
36 nr = 1
37 while 1:
38 c = df.readpage()
39 if c is None: break
40 print "[%d]" % nr,
41 sys.stdout.flush()
42 nr += 1
43 p = canvas.page(paperformat="a4")
44 p.insert(c, [trafo.translate(0, unit.length(vshift)+p.bbox().height())])
45 d.append(p)
46 d.writePSfile(psname)
49 def usage():
50 print progname
51 print "Copyright (C) 2004 Jörg Lehmann <joergl@users.sourceforge.net>"
52 print "usage: dvips [options] <eps-file>"
53 print "-h, --help: show this help"
54 print "-o, --output <file>: output file name (optional)"
57 def main():
58 try:
59 opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output"])
60 except getopt.GetoptError:
61 # print help information and exit:
62 usage()
63 sys.exit(2)
64 output = None
65 for o, a in opts:
66 if o in ("-h", "--help"):
67 usage()
68 sys.exit()
69 if o in ("-o", "--output"):
70 output = a
71 if len(args) == 1:
72 input = args[0]
73 elif len(args):
74 raise RuntimeError("can't handle several input files")
75 else:
76 raise RuntimeError("must specify an input file (reading from stdin is not yet supported)")
77 if not input.endswith(".dvi"):
78 input = input + ".dvi"
79 if output is None:
80 output = input[:-4] + ".ps"
81 dvips(input, output)
83 def profilefunction(f):
84 import hotshot, hotshot.stats
85 prof = hotshot.Profile("test.prof")
86 prof.runcall(f)
87 prof.close()
88 stats = hotshot.stats.load("test.prof")
89 stats.strip_dirs()
90 stats.sort_stats('time', 'calls')
91 stats.print_stats(10)
93 if __name__ == "__main__":
94 main()
95 # profilefunction(main)