~openerp-groupes/openobject-server/6.0-fix-setup-windows

« back to all changes in this revision

Viewing changes to bin/pychart/pscanvas.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 sys
 
15
import re
 
16
import theme
 
17
import version
 
18
import basecanvas
 
19
from scaling import *
 
20
 
 
21
comment_p = 0
 
22
 
 
23
class T(basecanvas.T):
 
24
    def __init__(self, fname):
 
25
        basecanvas.T.__init__(self)
 
26
        self.__out_fname = fname
 
27
        self.__reset_context()
 
28
        self.__output_lines = []
 
29
        self.__nr_gsave = 0
 
30
        self.__font_ids = {}
 
31
        self.__nr_fonts = 0
 
32
        
 
33
    def __reset_context(self):
 
34
        self.__font_name = None
 
35
        self.__font_size = -1
 
36
        self.__line_style = None
 
37
        self.__color = None
 
38
        self.__mtx_pushed = 0
 
39
        self.__txtmtx_pushed = 0
 
40
 
 
41
    def __intern_font(self, name):
 
42
        if self.__font_ids.has_key(name):
 
43
            return self.__font_ids[name]
 
44
        id = "F%d" % self.__nr_fonts
 
45
        self.__nr_fonts += 1
 
46
        self.__font_ids[name] = id
 
47
        return id
 
48
    
 
49
    def newpath(self):
 
50
        self.__write("N\n")
 
51
    def stroke(self):
 
52
        self.__write("ST\n")
 
53
    def closepath(self):
 
54
        self.__write("CP\n")
 
55
    def moveto(self, x, y):
 
56
        self.__write('%g %g M\n' % (x, y))
 
57
    
 
58
    def set_fill_color(self, color):
 
59
        if self.__color == color:
 
60
            pass
 
61
        else:
 
62
            if color.r == color.g and color.r == color.b:
 
63
                self.__write("%g SG\n" % color.r)
 
64
            else:
 
65
                self.__write("%g %g %g SC\n" % (color.r, color.g, color.b))
 
66
            self.__color = color
 
67
    def set_stroke_color(self, color):
 
68
        self.set_fill_color(color)
 
69
        
 
70
    def set_line_style(self, style):
 
71
        self.set_stroke_color(style.color)
 
72
        if (self.__line_style == style):
 
73
            pass
 
74
        else:
 
75
            self.__write("%g %d %d " % (nscale(style.width), 
 
76
                                      style.cap_style, style.join_style))
 
77
            if style.dash != None:
 
78
                self.__write("[%s] 0 SLD " % 
 
79
                           " ".join(map(str, nscale_seq(style.dash))))
 
80
            else:
 
81
                self.__write("SL ")
 
82
        self.__line_style = style
 
83
            
 
84
    def gsave(self):
 
85
        self.__nr_gsave += 1
 
86
        self.__write("GS\n")
 
87
    def grestore(self):
 
88
        self.__write("GR\n")
 
89
        self.__nr_gsave -= 1
 
90
        self.__reset_context()
 
91
 
 
92
    def clip_sub(self):
 
93
        self.__write("clip\n")
 
94
        
 
95
    def path_arc(self, x, y, radius, ratio, start_angle, end_angle):
 
96
        self.push_transformation((x, y), (1, ratio), None)
 
97
        self.__write("0 0 %g %g %g arc\n" % (radius, start_angle, end_angle))
 
98
        self.pop_transformation()
 
99
 
 
100
    def curveto(self, a,b,c,d,e,f):    
 
101
        self.__write("%g %g %g %g %g %g curveto\n" % (a,b,c,d,e,f))
 
102
 
 
103
    def push_transformation(self, baseloc, scale, angle, in_text=0):
 
104
        self.__mtx_pushed += 1
 
105
        self.__write("GB\n")
 
106
        if baseloc != None:
 
107
            self.__write("%g %g T\n" % (baseloc[0], baseloc[1]))
 
108
        if angle != None and angle != 0:
 
109
            self.__write("%g R\n" % (angle))
 
110
        if scale != None:
 
111
            self.__write("%g %g scale\n" % (scale[0], scale[1]))
 
112
    def pop_transformation(self, in_text=0):
 
113
        if self.__mtx_pushed == 0:
 
114
            raise ValueError, "mtx not pushed"
 
115
        self.__mtx_pushed -= 1
 
116
        self.__write("GE\n")
 
117
    def text_begin(self):
 
118
        self.__txtmtx_pushed += 1
 
119
        self.__write("TB\n")
 
120
    def text_end(self):
 
121
        self.__txtmtx_pushed -= 1
 
122
        self.__write("TE\n")
 
123
    def text_moveto(self, x, y, angle):
 
124
        self.__write("%g %g T " % (x,y))
 
125
        if angle != None and angle != 0:
 
126
            self.__write("%g R " % angle)
 
127
        self.moveto(0, 0)
 
128
        
 
129
    def text_show(self, font_name, size, color, str):
 
130
        self.set_fill_color(color)
 
131
        if (self.__font_name == font_name and self.__font_size == size):
 
132
            pass
 
133
        else:
 
134
            id = self.__intern_font(font_name)
 
135
            self.__write("%g %s\n" % (size, id))
 
136
            self.__font_name = font_name
 
137
            self.__font_size = size
 
138
        self.__write("(%s) show\n" % (str))
 
139
 
 
140
    def _path_polygon(self, points):
 
141
        if (len(points) == 4 
 
142
            and points[0][0] == points[1][0] 
 
143
            and points[2][0] == points[3][0] 
 
144
            and points[0][1] == points[3][1] 
 
145
            and points[1][1] == points[2][1]):
 
146
            # a rectangle.
 
147
            (xmin, ymin, xmax, ymax) = basecanvas._compute_bounding_box(points)
 
148
            if basecanvas.invisible_p(xmax, ymax):
 
149
                return
 
150
            self.setbb(xmin, ymin)
 
151
            self.setbb(xmax, ymax)
 
152
            self.__write("%g %g %g %g RECT\n" % \
 
153
                       (xscale(points[0][0]), yscale(points[0][1]),
 
154
                        xscale(points[2][0]), yscale(points[2][1])))
 
155
        else:
 
156
            basecanvas.T._path_polygon(self, points)
 
157
            
 
158
    def lineto(self, x, y):
 
159
        self.__write("%g %g L\n" % (x, y))
 
160
    def fill(self):
 
161
        self.__write("fill\n")
 
162
    def comment(self, str):
 
163
        if comment_p:
 
164
            self.verbatim("%" + str)
 
165
    def verbatim(self, str):
 
166
        self.__write(str)
 
167
        
 
168
    def close(self):
 
169
        basecanvas.T.close(self)
 
170
        if self.__output_lines == []:
 
171
            return
 
172
 
 
173
        fp, need_close = self.open_output(self.__out_fname)
 
174
            
 
175
        if self.__nr_gsave != 0:
 
176
            raise Exception, "gsave misnest (%d)" % (self.__nr_gsave)
 
177
        self.write_preamble(fp)
 
178
        
 
179
        fp.writelines(self.__output_lines)
 
180
        fp.writelines(["showpage end\n",
 
181
                       "%%Trailer\n",
 
182
                       "%%EOF\n"])
 
183
        if need_close:
 
184
            fp.close()
 
185
            
 
186
    def __write(self, str):
 
187
        self.__output_lines.append(str)
 
188
    
 
189
    def writelines(self, l):
 
190
        self.__output_lines.extend(l)
 
191
 
 
192
    def write_preamble(self, fp):
 
193
        bbox = [self.__xmin-1, self.__ymin-1, self.__xmax+1, self.__ymax+1]
 
194
        fp.write("%!PS-Adobe-2.0 EPSF-1.2\n")
 
195
        fp.write("%%Title: " + self.title + "\n")
 
196
        fp.write("%%Creator: " + self.creator + "\n")
 
197
        if self.author:
 
198
            fp.write("%%Author: " + self.author + "\n")
 
199
        fp.write("%%CreationDate: " + self.creation_date + "\n")
 
200
        fp.write("%%DocumentFonts: " + " ".join(self.__font_ids.keys()) + "\n")
 
201
        fp.write("%%Pages: 1\n")
 
202
 
 
203
        bbox = theme.adjust_bounding_box(bbox)
 
204
 
 
205
        fp.write("%%%%BoundingBox: %d %d %d %d\n" % \
 
206
                 (round(xscale(bbox[0])),
 
207
                  round(yscale(bbox[1])),
 
208
                  round(xscale(bbox[2])),
 
209
                  round(yscale(bbox[3]))))
 
210
        fp.write("%%EndComments\n")
 
211
        if self.aux_comments != "":
 
212
            for line in self.aux_comments.split("\n"):
 
213
                fp.write("% " + line + "\n")
 
214
 
 
215
        fp.write(preamble_text)
 
216
        for name, id in self.__font_ids.items():
 
217
            fp.write("/%s {/%s findfont SF} def\n" % (id, name))
 
218
        fp.write("%%EndProlog\n%%Page: 1 1\n")
 
219
 
 
220
 
 
221
preamble_text="""
 
222
40 dict begin
 
223
/RECT {4 dict begin
 
224
  /y2 exch def
 
225
  /x2 exch def
 
226
  /y1 exch def
 
227
  /x1 exch def
 
228
  newpath x1 y1 moveto x2 y1 lineto x2 y2 lineto x1 y2 lineto closepath
 
229
  end
 
230
} def
 
231
 
 
232
/SF {exch scalefont setfont} def
 
233
/TB {matrix currentmatrix} def
 
234
/TE {setmatrix} def
 
235
/GB {matrix currentmatrix} def
 
236
/GE {setmatrix} def
 
237
/SG {1 1 1 setrgbcolor setgray} def
 
238
/SC {1 setgray setrgbcolor} def
 
239
/SL {[] 0 setdash setlinejoin setlinecap setlinewidth} def
 
240
/SLD {setdash setlinejoin setlinecap setlinewidth} def
 
241
/M {moveto} def
 
242
/L {lineto} def
 
243
/T {translate} def
 
244
/R {rotate} def
 
245
/N {newpath} def
 
246
/ST {stroke} def
 
247
/CP {closepath} def
 
248
/GR {grestore} def
 
249
/GS {gsave} def
 
250
"""
 
251
 
 
252
# SL: set line style.
 
253
# width [dash] x linecap linejoin SL -> 
 
254
 
 
255
# SF: set font.
 
256
# name size SF -> 
 
257