Added TODO and DEVELOPMENT.
[faces-project.git] / faces / charting / renderer.py
blob2d44861983fc3d71fd43b21d566b644c4872a6db
1 ############################################################################
2 # Copyright (C) 2005 by Reithinger GmbH
3 # mreithinger@web.de
5 # This file is part of faces.
6 #
7 # faces is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # faces is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the
19 # Free Software Foundation, Inc.,
20 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ############################################################################
23 """
24 Patched Renderers of matplotlib
25 """
27 import matplotlib.numerix as numerix
28 import matplotlib.transforms as mtrans
29 import matplotlib.backends.backend_agg as agg
30 import math
33 class PatchedRendererAgg(agg.RendererAgg):
34 def __init__(self, *args, **kwargs):
35 agg.RendererAgg.__init__(self, *args, **kwargs)
36 self.old_draw_lines = self.draw_lines
37 self.draw_lines = self.patched_draw_lines
38 self.dumy_trans = mtrans.identity_transform()
41 def patched_draw_lines(self, gc, xs, ys, trans=None):
42 self.old_draw_lines(gc, xs, ys, trans or self.dumy_trans)
45 def draw_line(self, gc, x1, y1, x2, y2):
46 """
47 x and y are equal length arrays, draw lines connecting each
48 point in x, y
49 """
50 x = numerix.array([x1,x2], typecode=numerix.Float)
51 y = numerix.array([y1,y2], typecode=numerix.Float)
52 self.patched_draw_lines(gc, x, y)
55 def draw_text(self, gc, x, y, s, prop, angle, ismath):
56 if ismath:
57 return self.draw_mathtext(gc, x, y, s, prop, angle)
59 font = self._get_agg_font(prop)
60 if font is None: return None
61 if len(s)==1 and ord(s)>127:
62 font.load_char(ord(s))
63 else:
64 font.set_text(s, angle)
65 font.draw_glyphs_to_bitmap()
67 decent = font.get_descent() / 64.0
68 #print x, y, int(x), int(y)
70 self._renderer.draw_text(font, int(x), int(y + decent), gc)
73 class PatchedFigureCanvasAgg(agg.FigureCanvasAgg):
74 def get_renderer(self):
75 l,b,w,h = self.figure.bbox.get_bounds()
76 key = w, h, self.figure.dpi.get()
77 try: self._lastKey, self.renderer
78 except AttributeError: need_new_renderer = True
79 else: need_new_renderer = (self._lastKey != key)
81 if need_new_renderer:
82 self.renderer = PatchedRendererAgg(w, h, self.figure.dpi)
83 self._lastKey = key
85 return self.renderer
88 class SpeedupGraphicsContext(agg.GraphicsContextBase):
89 def set_alpha(self, alpha):
90 # workaround for alpha bug in agg (needed for WidgetAxes.speed_up)
91 # If normal alpha would be set, the object is to light by factor alpha
92 # in speed_up cache.
93 agg.GraphicsContextBase.set_alpha(self, math.sqrt(alpha))
96 class SpeedupRenderer(PatchedRendererAgg):
97 def new_gc(self):
98 """
99 Return an instance of a GraphicsContextBase
101 return SpeedupGraphicsContext()