2
# Copyright (C) 2000-2005 by Yasushi Saito (yasushi.saito@gmail.com)
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
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
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 = []
33
def __reset_context(self):
34
self.__font_name = None
36
self.__line_style = None
39
self.__txtmtx_pushed = 0
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
46
self.__font_ids[name] = id
55
def moveto(self, x, y):
56
self.__write('%g %g M\n' % (x, y))
58
def set_fill_color(self, color):
59
if self.__color == color:
62
if color.r == color.g and color.r == color.b:
63
self.__write("%g SG\n" % color.r)
65
self.__write("%g %g %g SC\n" % (color.r, color.g, color.b))
67
def set_stroke_color(self, color):
68
self.set_fill_color(color)
70
def set_line_style(self, style):
71
self.set_stroke_color(style.color)
72
if (self.__line_style == style):
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))))
82
self.__line_style = style
90
self.__reset_context()
93
self.__write("clip\n")
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()
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))
103
def push_transformation(self, baseloc, scale, angle, in_text=0):
104
self.__mtx_pushed += 1
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))
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
117
def text_begin(self):
118
self.__txtmtx_pushed += 1
121
self.__txtmtx_pushed -= 1
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)
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):
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))
140
def _path_polygon(self, points):
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]):
147
(xmin, ymin, xmax, ymax) = basecanvas._compute_bounding_box(points)
148
if basecanvas.invisible_p(xmax, ymax):
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])))
156
basecanvas.T._path_polygon(self, points)
158
def lineto(self, x, y):
159
self.__write("%g %g L\n" % (x, y))
161
self.__write("fill\n")
162
def comment(self, str):
164
self.verbatim("%" + str)
165
def verbatim(self, str):
169
basecanvas.T.close(self)
170
if self.__output_lines == []:
173
fp, need_close = self.open_output(self.__out_fname)
175
if self.__nr_gsave != 0:
176
raise Exception, "gsave misnest (%d)" % (self.__nr_gsave)
177
self.write_preamble(fp)
179
fp.writelines(self.__output_lines)
180
fp.writelines(["showpage end\n",
186
def __write(self, str):
187
self.__output_lines.append(str)
189
def writelines(self, l):
190
self.__output_lines.extend(l)
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")
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")
203
bbox = theme.adjust_bounding_box(bbox)
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")
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")
228
newpath x1 y1 moveto x2 y1 lineto x2 y2 lineto x1 y2 lineto closepath
232
/SF {exch scalefont setfont} def
233
/TB {matrix currentmatrix} def
235
/GB {matrix currentmatrix} 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
252
# SL: set line style.
253
# width [dash] x linecap linejoin SL ->