1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
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
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
21 __doc__ = """This module is defines variables for changing the looks
22 of charts. All the variables can be changed either via environment
23 variable PYCHART_OPTIONS or via the command-line options.
25 The value of PYCHART_OPTIONS should be a sequence of var=val separated
26 by space. Below is an example, which tells Pychart to write to file
27 foo.pdf and use Times-Roman as the default font.
29 PYCHART_OPTIONS="output=foo.pdf font-family=Times"
31 The summary of attributes that can be set via PYCHART_OPTIONS follows:
33 output=FILENAME (default: stdout)
35 Set the output file name.
37 format=[ps|pdf|pdf-uncompressed|png|x11|svg] (default: ps)
39 Set the output file format.
41 font-family=NAME (default: Helvetica)
43 Set the default font to be used by texts.
45 font-size=N (default: 9)
47 Set the default font to be used by texts.
48 line-width=X (default: 0.4)
50 Set the default line width, in points. See also
53 scale=X (default: 1.0)
55 Set the scaling factor. The default is 1.0.
57 color=[yes|no] (default: no)
59 If yes, Pychart colorizes default object attributes.
61 You can also set these variables by calling theme.get_options.
66 output_format = None # "ps", "pdf", "png", "x11", or "svg"
70 default_font_family = "Helvetica"
72 default_line_height = None
73 default_font_halign = "L"
74 default_font_valign = "B"
75 default_font_angle = 0
76 default_line_width = 0.4
79 delta_bounding_box = [-3, -3, 3, 3]
83 if str in ("yes", "true", "1"):
88 def parse_bounding_box(arg):
89 global delta_bounding_box, bounding_box
93 raise ValueError, "Need to specify margin=LEFT,BOTTOM,RIGHT,TOP"
97 delta_bounding_box[i] = int(val[1:])
99 delta_bounding_box[i] = int(val[1:])
101 bounding_box[i] = int(val)
103 def adjust_bounding_box(bbox):
104 """Adjust the bounding box as specified by user.
105 Returns the adjusted bounding box.
107 - bbox: Bounding box computed from the canvas drawings.
108 It must be a four-tuple of numbers.
110 for i in range(0, 4):
111 if bounding_box.has_key(i):
112 bbox[i] = bounding_box[i]
114 bbox[i] += delta_bounding_box[i]
117 def parse_option(opt, arg):
118 global use_color, scale_factor, margin
119 global output_format, output_file, compress_output
120 global default_font_family, default_font_size
121 global default_line_height
122 global default_line_width, debug_level
124 if arg in ("ps", "eps"):
127 output_format = "png"
129 output_format = "svg"
131 output_format = "x11"
132 elif arg == "pdf-uncompressed":
133 output_format = "pdf"
135 elif arg in ("pdf-compressed", "pdf"):
136 output_format = "pdf"
139 raise ValueError, "Unknown output option: " + str(arg)
140 elif opt == "output":
145 scale_factor = float(arg)
147 parse_bounding_box(arg)
148 elif opt == "font-family":
149 default_font_family = arg
150 elif opt == "font-size":
151 default_font_size = float(arg)
152 default_line_height = float(arg)
153 elif opt == "line-width":
154 default_line_width = float(arg)
155 elif opt == "debug-level":
156 debug_level = int(arg)
158 raise getopt.GetoptError, "Unknown option: " + opt + " " + arg
160 if os.environ.has_key("PYCHART_OPTIONS"):
161 for opt in os.environ["PYCHART_OPTIONS"].split():
162 opt, arg = opt.split("=")
163 parse_option(opt, arg)
166 def add_reinitialization_hook(proc):
172 print "Usage: %s [options..]" % sys.argv[0]
174 --scale=X: Set the scaling factor to X (default: 1.0).
175 --format=[ps|png|pdf|x11|svg]: Set the output format (default: ps).
176 --font-family=NAME: Set the default font family (default: Helvetica).
177 --font-size=NAME: Set the default font size (default: 9pts).
178 --line-width=NAME: Set the default line width (default: 0.4).
179 --debug-level=N: Set the messaging verbosity (default: 0).
180 --bbox=LEFT,BOTTOM,RIGHT,TOP: Specifies the amount of space (in PS points) to be left in the edges of the picture (default: -1,-1,+1,+1).
184 """This procedure must be called after setting variables in
185 the |theme| module. This procedure propagates the new values of
186 the theme variables to other modules that depend on their values."""
190 def get_options(argv = None):
191 """This procedure takes a list of command line arguments in <argv>
193 options. It returns the non-parsed portion of <argv>. Parameter
195 omitted, in which case its value defaults to |sys.argv[1:]|.
196 The options supported are: "|--format=[ps,png,pdf,x11,svg]|",
197 "|--output=|<file>", "|--color=[yes,no]|"
198 "|--scale=|<X>", "|--font-family=|<name>", "|--font-size=|<X>",
199 "|--line-width=|<X>",
200 "|--debug-level=|<N>", "|bbox=|<left,bottom,right,top>".
201 The below code shows an example.
204 from pychart import *
205 args = theme.get_options()
212 opts, args = getopt.getopt(argv, "d:co:f:",
213 ["format=", "output=", "color=",
214 "scale=", "font-family=", "font-size=",
215 "line-width=", "debug-level=",
217 except getopt.GetoptError, foo:
220 raise getopt.GetoptError
221 for opt, arg in opts:
223 parse_option("debug-level", arg)
225 parse_option("color", None)
227 parse_option("output", arg)
229 parse_option("format", arg)
231 parse_option(opt[2:], arg)