New trunk
[odoo/odoo.git] / bin / pychart / gs_frontend.py
1 #
2 # Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
3
4 # Jockey is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the
6 # Free Software Foundation; either version 2, or (at your option) any
7 # later version.
8 #
9 # Jockey is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 import pychart_util
15 import theme
16 import sys
17 import os
18 import os.path
19 import pscanvas
20 import tempfile
21 import string
22 import basecanvas
23 from scaling import *
24
25 def get_gs_path():
26     """Guess where the Ghostscript executable is
27     and return its absolute path name."""
28     path = os.defpath
29     if os.environ.has_key("PATH"):
30         path = os.environ["PATH"]
31     for dir in path.split(os.pathsep):
32         for name in ("gs", "gs.exe", "gswin32c.exe"):
33             g = os.path.join(dir, name)
34             if os.path.exists(g):
35                 return g
36     raise Exception, "Ghostscript not found."
37
38 class T(pscanvas.T):
39     def __write_contents(self, fp):
40         fp.write(pscanvas.preamble_text)
41         for name, id in self.__font_ids.items():
42             fp.write("/%s {/%s findfont SF} def\n" % (id, name))
43         fp.write("%d %d translate\n" % (-self.bbox[0], -self.bbox[1]))
44         fp.writelines(self.__output_lines)
45         fp.write("showpage end\n")
46         fp.flush()
47
48     def close(self):
49         # Don't call pscanvas.T.close, as it creates a
50         # ps file. 
51         basecanvas.T.close(self)
52         
53     def start_gs(self, arg):
54         self.bbox = theme.adjust_bounding_box([xscale(self.__xmin),
55                                                yscale(self.__ymin),
56                                                xscale(self.__xmax),
57                                                yscale(self.__ymax)])
58         
59         gs_path = get_gs_path()
60         self.pipe_fp = None
61         if self.__output_lines == []:
62             return
63
64         if sys.platform != "win32" and hasattr(os, "popen"):
65             # UNIX-like systems
66             cmdline = "\"%s\" -q %s -g%dx%d -q >/dev/null 2>&1" % \
67             (gs_path, arg,
68              self.bbox[2] - self.bbox[0],
69              self.bbox[3] - self.bbox[1])
70             self.pipe_fp = os.popen(cmdline, "w")
71             self.__write_contents(self.pipe_fp)
72         else:
73             # XXX should use mktemp, but need to support python<=2.2 as well.
74             fname = tempfile.mktemp("xxx")
75             fp = open(fname, "wb")
76             self.__write_contents(fp)
77             fp.close()
78             cmdline = "\"%s\" -q %s -g%dx%d -q <%s >NUL" % \
79             (gs_path, arg,
80              self.bbox[2] - self.bbox[0],
81              self.bbox[3] - self.bbox[1], fname)
82             os.system(cmdline)
83             os.unlink(fname)
84     def close_gs(self):
85         if self.pipe_fp:
86             self.pipe_fp.close()
87             self.pipe_fp = None