matrix class
[PyX/mjg.git] / pyx / deco.py
blob3021d6fd9e1a779bd4ad163d0c302e4badaede2f
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2003-2004 Michael Schindler <m-schindler@users.sourceforge.net>
7 # Copyright (C) 2002-2004 André Wobst <wobsta@users.sourceforge.net>
9 # This file is part of PyX (http://pyx.sourceforge.net/).
11 # PyX is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # PyX is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with PyX; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 # TODO:
26 # - should we improve on the arc length -> arg parametrization routine or
27 # should we at least factor it out?
29 import sys, math
30 import attr, base, canvas, color, helper, path, style, trafo, unit
33 # Decorated path
36 class decoratedpath(base.canvasitem):
37 """Decorated path
39 The main purpose of this class is during the drawing
40 (stroking/filling) of a path. It collects attributes for the
41 stroke and/or fill operations.
42 """
44 def __init__(self, path, strokepath=None, fillpath=None,
45 styles=None, strokestyles=None, fillstyles=None,
46 subcanvas=None):
48 self.path = path
50 # path to be stroked or filled (or None)
51 self.strokepath = strokepath
52 self.fillpath = fillpath
54 # global style for stroking and filling and subdps
55 self.styles = helper.ensurelist(styles)
57 # styles which apply only for stroking and filling
58 self.strokestyles = helper.ensurelist(strokestyles)
59 self.fillstyles = helper.ensurelist(fillstyles)
61 # the canvas can contain additional elements of the path, e.g.,
62 # arrowheads,
63 if subcanvas is None:
64 self.subcanvas = canvas.canvas()
65 else:
66 self.subcanvas = subcanvas
69 def bbox(self):
70 scbbox = self.subcanvas.bbox()
71 pbbox = self.path.bbox()
72 if scbbox is not None:
73 return scbbox+pbbox
74 else:
75 return pbbox
77 def prolog(self):
78 result = []
79 for style in list(self.styles) + list(self.fillstyles) + list(self.strokestyles):
80 result.extend(style.prolog())
81 result.extend(self.subcanvas.prolog())
82 return result
84 def outputPS(self, file):
85 # draw (stroke and/or fill) the decoratedpath on the canvas
86 # while trying to produce an efficient output, e.g., by
87 # not writing one path two times
89 # small helper
90 def _writestyles(styles, file=file):
91 for style in styles:
92 style.outputPS(file)
94 # apply global styles
95 if self.styles:
96 file.write("gsave\n")
97 _writestyles(self.styles)
99 if self.fillpath is not None:
100 file.write("newpath\n")
101 self.fillpath.outputPS(file)
103 if self.strokepath==self.fillpath:
104 # do efficient stroking + filling
105 file.write("gsave\n")
107 if self.fillstyles:
108 _writestyles(self.fillstyles)
110 file.write("fill\n")
111 file.write("grestore\n")
113 if self.strokestyles:
114 file.write("gsave\n")
115 _writestyles(self.strokestyles)
117 file.write("stroke\n")
119 if self.strokestyles:
120 file.write("grestore\n")
121 else:
122 # only fill fillpath - for the moment
123 if self.fillstyles:
124 file.write("gsave\n")
125 _writestyles(self.fillstyles)
127 file.write("fill\n")
129 if self.fillstyles:
130 file.write("grestore\n")
132 if self.strokepath is not None and self.strokepath!=self.fillpath:
133 # this is the only relevant case still left
134 # Note that a possible stroking has already been done.
136 if self.strokestyles:
137 file.write("gsave\n")
138 _writestyles(self.strokestyles)
140 file.write("newpath\n")
141 self.strokepath.outputPS(file)
142 file.write("stroke\n")
144 if self.strokestyles:
145 file.write("grestore\n")
147 if self.strokepath is None and self.fillpath is None:
148 raise RuntimeError("Path neither to be stroked nor filled")
150 # now, draw additional elements of decoratedpath
151 self.subcanvas.outputPS(file)
153 # restore global styles
154 if self.styles:
155 file.write("grestore\n")
157 def outputPDF(self, file):
158 # draw (stroke and/or fill) the decoratedpath on the canvas
160 def _writestyles(styles, file=file):
161 for style in styles:
162 style.outputPDF(file)
164 def _writestrokestyles(strokestyles, file=file):
165 for style in strokestyles:
166 if isinstance(style, color.color):
167 style.outputPDF(file, fillattr=0)
168 else:
169 style.outputPDF(file)
171 def _writefillstyles(fillstyles, file=file):
172 for style in fillstyles:
173 if isinstance(style, color.color):
174 style.outputPDF(file, strokeattr=0)
175 else:
176 style.outputPDF(file)
178 # apply global styles
179 if self.styles:
180 file.write("q\n") # gsave
181 _writestyles(self.styles)
183 if self.fillpath is not None:
184 self.fillpath.outputPDF(file)
186 if self.strokepath==self.fillpath:
187 # do efficient stroking + filling
188 file.write("q\n") # gsave
190 if self.fillstyles:
191 _writefillstyles(self.fillstyles)
192 if self.strokestyles:
193 _writestrokestyles(self.strokestyles)
195 file.write("B\n") # both stroke and fill
196 file.write("Q\n") # grestore
197 else:
198 # only fill fillpath - for the moment
199 if self.fillstyles:
200 file.write("q\n") # gsave
201 _writefillstyles(self.fillstyles)
203 file.write("f\n") # fill
205 if self.fillstyles:
206 file.write("Q\n") # grestore
208 if self.strokepath is not None and self.strokepath!=self.fillpath:
209 # this is the only relevant case still left
210 # Note that a possible stroking has already been done.
212 if self.strokestyles:
213 file.write("q\n") # gsave
214 _writestrokestyles(self.strokestyles)
216 self.strokepath.outputPDF(file)
217 file.write("S\n") # stroke
219 if self.strokestyles:
220 file.write("Q\n") # grestore
222 if self.strokepath is None and self.fillpath is None:
223 raise RuntimeError("Path neither to be stroked nor filled")
225 # now, draw additional elements of decoratedpath
226 self.subcanvas.outputPDF(file)
228 # restore global styles
229 if self.styles:
230 file.write("Q\n") # grestore
233 # Path decorators
236 class deco:
238 """decorators
240 In contrast to path styles, path decorators depend on the concrete
241 path to which they are applied. In particular, they don't make
242 sense without any path and can thus not be used in canvas.set!
246 def decorate(self, dp):
247 """apply a style to a given decoratedpath object dp
249 decorate accepts a decoratedpath object dp, applies PathStyle
250 by modifying dp in place and returning the new dp.
253 pass
256 # stroked and filled: basic decos which stroked and fill,
257 # respectively the path
260 class _stroked(deco, attr.exclusiveattr):
262 """stroked is a decorator, which draws the outline of the path"""
264 def __init__(self, styles=[]):
265 attr.exclusiveattr.__init__(self, _stroked)
266 self.styles = attr.mergeattrs(styles)
267 attr.checkattrs(self.styles, [style.strokestyle])
269 def __call__(self, styles=[]):
270 # XXX or should we also merge self.styles
271 return _stroked(styles)
273 def decorate(self, dp):
274 dp.strokepath = dp.path
275 dp.strokestyles = self.styles
276 return dp
278 stroked = _stroked()
279 stroked.clear = attr.clearclass(_stroked)
282 class _filled(deco, attr.exclusiveattr):
284 """filled is a decorator, which fills the interior of the path"""
286 def __init__(self, styles=[]):
287 attr.exclusiveattr.__init__(self, _filled)
288 self.styles = attr.mergeattrs(styles)
289 attr.checkattrs(self.styles, [style.fillstyle])
291 def __call__(self, styles=[]):
292 # XXX or should we also merge self.styles
293 return _filled(styles)
295 def decorate(self, dp):
296 dp.fillpath = dp.path
297 dp.fillstyles = self.styles
298 return dp
300 filled = _filled()
301 filled.clear = attr.clearclass(_filled)
304 # Arrows
307 # two helper functions which construct the arrowhead and return its size, respectively
309 def _arrowheadtemplatelength(anormpath, size):
310 "calculate length of arrowhead template (in parametrisation of anormpath)"
311 # get tip (tx, ty)
312 tx, ty = anormpath.begin()
314 # obtain arrow template by using path up to first intersection
315 # with circle around tip (as suggested by Michael Schindler)
316 ipar = anormpath.intersect(path.circle(tx, ty, size))
317 if ipar[0]:
318 alen = ipar[0][0]
319 else:
320 # if this doesn't work, use first order conversion from pts to
321 # the bezier curve's parametrization
322 tvec = anormpath.tangent(0)
323 tlen = tvec.arclen_pt()
324 try:
325 alen = unit.topt(size)/tlen
326 except ArithmeticError:
327 # take maximum, we can get
328 alen = anormpath.range()
329 if alen > anormpath.range(): alen = anormpath.range()
331 return alen
334 def _arrowhead(anormpath, size, angle, constriction):
336 """helper routine, which returns an arrowhead for a normpath
338 returns arrowhead at begin of anormpath with size,
339 opening angle and relative constriction
342 alen = _arrowheadtemplatelength(anormpath, size)
343 tx, ty = anormpath.begin()
345 # now we construct the template for our arrow but cutting
346 # the path a the corresponding length
347 arrowtemplate = anormpath.split([alen])[0]
349 # from this template, we construct the two outer curves
350 # of the arrow
351 arrowl = arrowtemplate.transformed(trafo.rotate(-angle/2.0, tx, ty))
352 arrowr = arrowtemplate.transformed(trafo.rotate( angle/2.0, tx, ty))
354 # now come the joining backward parts
355 if constriction:
356 # arrow with constriction
358 # constriction point (cx, cy) lies on path
359 cx, cy = anormpath.at(constriction*alen)
361 arrowcr= path.line(*(arrowr.end()+(cx,cy)))
363 arrow = arrowl.reversed() << arrowr << arrowcr
364 arrow.append(path.closepath())
365 else:
366 # arrow without constriction
367 arrow = arrowl.reversed() << arrowr
368 arrow.append(path.closepath())
370 return arrow
373 _base = 6 * unit.v_pt
375 class arrow(deco, attr.attr):
377 """arrow is a decorator which adds an arrow to either side of the path"""
379 def __init__(self, attrs=[], position=0, size=_base, angle=45, constriction=0.8):
380 self.attrs = attr.mergeattrs([style.linestyle.solid, filled] + attrs)
381 attr.checkattrs(self.attrs, [deco, style.fillstyle, style.strokestyle])
382 self.position = position
383 self.size = size
384 self.angle = angle
385 self.constriction = constriction
387 def __call__(self, attrs=None, position=None, size=None, angle=None, constriction=None):
388 if attrs is None:
389 attrs = self.attrs
390 if position is None:
391 position = self.position
392 if size is None:
393 size = self.size
394 if angle is None:
395 angle = self.angle
396 if constriction is None:
397 constriction = self.constriction
398 return arrow(attrs=attrs, position=position, size=size, angle=angle, constriction=constriction)
400 def decorate(self, dp):
401 # XXX raise exception error, when strokepath is not defined
403 # convert to normpath if necessary
404 if isinstance(dp.strokepath, path.normpath):
405 anormpath = dp.strokepath
406 else:
407 anormpath = path.normpath(dp.path)
408 if self.position:
409 anormpath = anormpath.reversed()
411 # add arrowhead to decoratedpath
412 dp.subcanvas.draw(_arrowhead(anormpath, self.size, self.angle, self.constriction),
413 self.attrs)
415 # calculate new strokepath
416 alen = _arrowheadtemplatelength(anormpath, self.size)
417 if self.constriction:
418 ilen = alen*self.constriction
419 else:
420 ilen = alen
422 # correct somewhat for rotation of arrow segments
423 ilen = ilen*math.cos(math.pi*self.angle/360.0)
425 # this is the rest of the path, we have to draw
426 anormpath = anormpath.split([ilen])[1]
428 # go back to original orientation, if necessary
429 if self.position:
430 anormpath.reverse()
432 # set the new (shortened) strokepath
433 dp.strokepath = anormpath
435 return dp
437 arrow.clear = attr.clearclass(arrow)
439 # arrows at begin of path
440 barrow = arrow(position=0)
441 barrow.SMALL = barrow(size=_base/math.sqrt(64))
442 barrow.SMALl = barrow(size=_base/math.sqrt(32))
443 barrow.SMAll = barrow(size=_base/math.sqrt(16))
444 barrow.SMall = barrow(size=_base/math.sqrt(8))
445 barrow.Small = barrow(size=_base/math.sqrt(4))
446 barrow.small = barrow(size=_base/math.sqrt(2))
447 barrow.normal = barrow(size=_base)
448 barrow.large = barrow(size=_base*math.sqrt(2))
449 barrow.Large = barrow(size=_base*math.sqrt(4))
450 barrow.LArge = barrow(size=_base*math.sqrt(8))
451 barrow.LARge = barrow(size=_base*math.sqrt(16))
452 barrow.LARGe = barrow(size=_base*math.sqrt(32))
453 barrow.LARGE = barrow(size=_base*math.sqrt(64))
455 # arrows at end of path
456 earrow = arrow(position=1)
457 earrow.SMALL = earrow(size=_base/math.sqrt(64))
458 earrow.SMALl = earrow(size=_base/math.sqrt(32))
459 earrow.SMAll = earrow(size=_base/math.sqrt(16))
460 earrow.SMall = earrow(size=_base/math.sqrt(8))
461 earrow.Small = earrow(size=_base/math.sqrt(4))
462 earrow.small = earrow(size=_base/math.sqrt(2))
463 earrow.normal = earrow(size=_base)
464 earrow.large = earrow(size=_base*math.sqrt(2))
465 earrow.Large = earrow(size=_base*math.sqrt(4))
466 earrow.LArge = earrow(size=_base*math.sqrt(8))
467 earrow.LARge = earrow(size=_base*math.sqrt(16))
468 earrow.LARGe = earrow(size=_base*math.sqrt(32))
469 earrow.LARGE = earrow(size=_base*math.sqrt(64))