new attribute handling in text module completed
[PyX/mjg.git] / pyx / canvas.py
blobcdcba7d7b452c4e12cae2d23b18a9381e0f821f1
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002, 2003 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2002, 2003 André Wobst <wobsta@users.sourceforge.net>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
10 # PyX is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # PyX is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with PyX; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 # XXX remove string module
25 # XXX what is a pattern
26 # XXX what is a color
29 """The canvas module provides a PostScript canvas class and related classes
31 A canvas holds a collection of all elements that should be displayed together
32 with their attributes.
33 """
35 import string, cStringIO, time
36 import attr, base, bbox, deco, unit, prolog, style, trafo, version
38 # known paperformats as tuple(width, height)
40 _paperformats = { "A4" : ("210 t mm", "297 t mm"),
41 "A3" : ("297 t mm", "420 t mm"),
42 "A2" : ("420 t mm", "594 t mm"),
43 "A1" : ("594 t mm", "840 t mm"),
44 "A0" : ("840 t mm", "1188 t mm"),
45 "A0B" : ("910 t mm", "1370 t mm"),
46 "LETTER" : ("8.5 t inch", "11 t inch"),
47 "LEGAL" : ("8.5 t inch", "14 t inch")}
51 # clipping class
54 # XXX help me find my identity
56 class clip(base.PSCmd):
58 """class for use in canvas constructor which clips to a path"""
60 def __init__(self, path):
61 """construct a clip instance for a given path"""
62 self.path = path
64 def bbox(self):
65 # as a PSCmd a clipping path has NO influence on the bbox...
66 return bbox._bbox()
68 def clipbbox(self):
69 # ... but for clipping, we nevertheless need the bbox
70 return self.path.bbox()
72 def write(self, file):
73 _newpath().write(file)
74 self.path.write(file)
75 _clip().write(file)
78 # some very primitive Postscript operators
81 class _newpath(base.PSOp):
82 def write(self, file):
83 file.write("newpath\n")
86 class _stroke(base.PSOp):
87 def write(self, file):
88 file.write("stroke\n")
91 class _fill(base.PSOp):
92 def write(self, file):
93 file.write("fill\n")
96 class _clip(base.PSOp):
97 def write(self, file):
98 file.write("clip\n")
101 class _gsave(base.PSOp):
102 def write(self, file):
103 file.write("gsave\n")
106 class _grestore(base.PSOp):
107 def write(self, file):
108 file.write("grestore\n")
114 class _canvas(base.PSCmd):
116 """a canvas is a collection of PSCmds together with PSAttrs"""
118 def __init__(self, *args):
120 """construct a canvas
122 The canvas can be modfied by supplying args, which have
123 to be instances of one of the following classes:
124 - trafo.trafo (leading to a global transformation of the canvas)
125 - canvas.clip (clips the canvas)
126 - base.PathStyle (sets some global attributes of the canvas)
128 Note that, while the first two properties are fixed for the
129 whole canvas, the last one can be changed via canvas.set()
133 self.PSOps = []
134 self.trafo = trafo.trafo()
135 self.clipbbox = bbox._bbox()
137 # prevent cyclic imports
138 import text
139 self.texrunner = text.defaulttexrunner
141 for arg in args:
142 if isinstance(arg, trafo._trafo):
143 self.trafo = self.trafo*arg
144 self.PSOps.append(arg)
145 elif isinstance(arg, clip):
146 self.clipbbox=(self.clipbbox*
147 arg.clipbbox().transformed(self.trafo))
148 self.PSOps.append(arg)
149 else:
150 self.set(arg)
152 def bbox(self):
153 """returns bounding box of canvas"""
154 obbox = reduce(lambda x,y:
155 isinstance(y, base.PSCmd) and x+y.bbox() or x,
156 self.PSOps,
157 bbox._bbox())
159 # transform according to our global transformation and
160 # intersect with clipping bounding box (which have already been
161 # transformed in canvas.__init__())
162 return obbox.transformed(self.trafo)*self.clipbbox
164 def prolog(self):
165 result = []
166 for cmd in self.PSOps:
167 result.extend(cmd.prolog())
168 return result
170 def write(self, file):
171 if self.PSOps:
172 _gsave().write(file)
173 for cmd in self.PSOps:
174 cmd.write(file)
175 _grestore().write(file)
177 def insert(self, PSOp, args=[]):
178 """insert PSOp in the canvas.
180 If args are given, then insert a canvas containing PSOp applying args.
182 returns the PSOp
186 # XXX check for PSOp
188 if args:
189 sc = _canvas(*args)
190 sc.insert(PSOp)
191 self.PSOps.append(sc)
192 else:
193 self.PSOps.append(PSOp)
195 return PSOp
197 def set(self, attrs):
198 """sets styles args globally for the rest of the canvas
200 returns canvas
204 attr.checkattrs(attrs, [style.strokestyle, style.fillstyle])
205 for astyle in attrs:
206 self.insert(astyle)
207 return self
209 def draw(self, path, attrs):
210 """draw path on canvas using the style given by args
212 The argument attrs consists of PathStyles, which modify
213 the appearance of the path, PathDecos, which add some new
214 visual elements to the path, or trafos, which are applied
215 before drawing the path.
217 returns the canvas
221 attrs = attr.mergeattrs(attrs)
222 attr.checkattrs(attrs, [deco.deco, style.fillstyle, style.strokestyle, trafo._trafo])
224 for t in attr.getattrs(attrs, [trafo._trafo]):
225 path = path.transformed(t)
227 dp = deco.decoratedpath(path)
229 # set global styles
230 dp.styles = attr.getattrs(attrs, [style.fillstyle, style.strokestyle])
232 # add path decorations and modify path accordingly
233 for adeco in attr.getattrs(attrs, [deco.deco]):
234 dp = adeco.decorate(dp)
236 self.insert(dp)
238 return self
240 def stroke(self, path, attrs=[]):
241 """stroke path on canvas using the style given by args
243 The argument attrs consists of PathStyles, which modify
244 the appearance of the path, PathDecos, which add some new
245 visual elements to the path, or trafos, which are applied
246 before drawing the path.
248 returns the canvas
252 return self.draw(path, [deco.stroked]+list(attrs))
254 def fill(self, path, attrs=[]):
255 """fill path on canvas using the style given by args
257 The argument attrs consists of PathStyles, which modify
258 the appearance of the path, PathDecos, which add some new
259 visual elements to the path, or trafos, which are applied
260 before drawing the path.
262 returns the canvas
266 return self.draw(path, [deco.filled]+list(attrs))
268 def settexrunner(self, texrunner):
269 """sets the texrunner to be used to within the text and text_pt methods"""
271 self.texrunner = texrunner
273 def text(self, x, y, atext, *args, **kwargs):
274 """insert a text into the canvas
276 inserts a textbox created by self.texrunner.text into the canvas
278 returns the inserted textbox"""
280 return self.insert(self.texrunner.text(x, y, atext, *args, **kwargs))
283 def text_pt(self, x, y, atext, *args):
284 """insert a text into the canvas
286 inserts a textbox created by self.texrunner.text_pt into the canvas
288 returns the inserted textbox"""
290 return self.insert(self.texrunner.text_pt(x, y, atext, *args))
293 # canvas for patterns
296 class pattern(_canvas, attr.exclusiveattr, style.fillstyle):
298 def __init__(self, painttype=1, tilingtype=1, xstep=None, ystep=None, bbox=None, trafo=None):
299 attr.exclusiveattr.__init__(self, pattern)
300 _canvas.__init__(self)
301 attr.exclusiveattr.__init__(self, pattern)
302 self.id = "pattern%d" % id(self)
303 # XXX: some checks are in order
304 if painttype not in (1,2):
305 raise ValueError("painttype must be 1 or 2")
306 self.painttype = painttype
307 if tilingtype not in (1,2,3):
308 raise ValueError("tilingtype must be 1, 2 or 3")
309 self.tilingtype = tilingtype
310 self.xstep = xstep
311 self.ystep = ystep
312 self.patternbbox = bbox
313 self.patterntrafo = trafo
315 def bbox(self):
316 return bbox._bbox()
318 def write(self, file):
319 file.write("%s setpattern\n" % self.id)
321 def prolog(self):
322 realpatternbbox = _canvas.bbox(self)
323 if self.xstep is None:
324 xstep = unit.topt(realpatternbbox.width())
325 else:
326 xstep = unit.topt(unit.length(self.xstep))
327 if self.ystep is None:
328 ystep = unit.topt(realpatternbbox.height())
329 else:
330 ystep = unit.topt(unit.length(self.ystep))
331 if not xstep:
332 raise ValueError("xstep in pattern cannot be zero")
333 if not ystep:
334 raise ValueError("ystep in pattern cannot be zero")
335 patternbbox = self.patternbbox or realpatternbbox.enlarged("5 pt")
337 patternprefix = string.join(("<<",
338 "/PatternType 1",
339 "/PaintType %d" % self.painttype,
340 "/TilingType %d" % self.tilingtype,
341 "/BBox[%s]" % str(patternbbox),
342 "/XStep %g" % xstep,
343 "/YStep %g" % ystep,
344 "/PaintProc {\nbegin\n"),
345 sep="\n")
346 stringfile = cStringIO.StringIO()
347 _canvas.write(self, stringfile)
348 patternproc = stringfile.getvalue()
349 stringfile.close()
350 patterntrafostring = self.patterntrafo is None and "matrix" or str(self.patterntrafo)
351 patternsuffix = "end\n} bind\n>>\n%s\nmakepattern" % patterntrafostring
353 pr = _canvas.prolog(self)
354 pr.append(prolog.definition(self.id, string.join((patternprefix, patternproc, patternsuffix), "")))
355 return pr
357 pattern.clear = attr.clearclass(pattern)
360 # The main canvas class
363 class canvas(_canvas):
365 """a canvas is a collection of PSCmds together with PSAttrs"""
367 def writetofile(self, filename, paperformat=None, rotated=0, fittosize=0, margin="1 t cm",
368 bbox=None, bboxenlarge="1 t pt"):
369 """write canvas to EPS file
371 If paperformat is set to a known paperformat, the output will be centered on
372 the page.
374 If rotated is set, the output will first be rotated by 90 degrees.
376 If fittosize is set, then the output is scaled to the size of the
377 page (minus margin). In that case, the paperformat the specification
378 of the paperformat is obligatory.
380 The bbox parameter overrides the automatic bounding box determination.
381 bboxenlarge may be used to enlarge the bbox of the canvas (or the
382 manually specified bbox).
385 if filename[-4:]!=".eps":
386 filename = filename + ".eps"
388 try:
389 file = open(filename, "w")
390 except IOError:
391 raise IOError("cannot open output file")
393 abbox = bbox is not None and bbox or self.bbox()
394 abbox = abbox.enlarged(bboxenlarge)
395 ctrafo = None # global transformation of canvas
397 if rotated:
398 ctrafo = trafo._rotate(90,
399 0.5*(abbox.llx+abbox.urx),
400 0.5*(abbox.lly+abbox.ury))
402 if paperformat:
403 # center (optionally rotated) output on page
404 try:
405 width, height = _paperformats[paperformat.upper()]
406 except KeyError:
407 raise KeyError, "unknown paperformat '%s'" % paperformat
408 width = unit.topt(width)
409 height = unit.topt(height)
411 if not ctrafo: ctrafo=trafo.trafo()
413 ctrafo = ctrafo._translated(0.5*(width -(abbox.urx-abbox.llx))-
414 abbox.llx,
415 0.5*(height-(abbox.ury-abbox.lly))-
416 abbox.lly)
418 if fittosize:
419 # scale output to pagesize - margins
420 margin = unit.topt(margin)
421 if 2*margin > min(width, height):
422 raise RuntimeError("Margins too broad for selected paperformat. Aborting.")
424 if rotated:
425 sfactor = min((height-2*margin)/(abbox.urx-abbox.llx),
426 (width-2*margin)/(abbox.ury-abbox.lly))
427 else:
428 sfactor = min((width-2*margin)/(abbox.urx-abbox.llx),
429 (height-2*margin)/(abbox.ury-abbox.lly))
431 ctrafo = ctrafo._scaled(sfactor, sfactor, 0.5*width, 0.5*height)
434 elif fittosize:
435 raise ValueError("must specify paper size for fittosize")
437 # if there has been a global transformation, adjust the bounding box
438 # accordingly
439 if ctrafo: abbox = abbox.transformed(ctrafo)
441 file.write("%!PS-Adobe-3.0 EPSF 3.0\n")
442 abbox.write(file)
443 file.write("%%%%Creator: PyX %s\n" % version.version)
444 file.write("%%%%Title: %s\n" % filename)
445 file.write("%%%%CreationDate: %s\n" %
446 time.asctime(time.localtime(time.time())))
447 file.write("%%EndComments\n")
449 file.write("%%BeginProlog\n")
451 mergedprolog = []
453 for pritem in self.prolog():
454 for mpritem in mergedprolog:
455 if mpritem.merge(pritem) is None: break
456 else:
457 mergedprolog.append(pritem)
459 for pritem in mergedprolog:
460 pritem.write(file)
462 file.write("%%EndProlog\n")
464 # again, if there has occured global transformation, apply it now
465 if ctrafo: ctrafo.write(file)
467 file.write("%f setlinewidth\n" % unit.topt(style.linewidth.normal))
469 # here comes the actual content
470 self.write(file)
472 file.write("showpage\n")
473 file.write("%%Trailer\n")
474 file.write("%%EOF\n")