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