1 ############################################################################
2 # Copyright (C) 2005 by Reithinger GmbH
5 # This file is part of faces.
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 ############################################################################
24 Patched Renderers of matplotlib
27 import matplotlib
.numerix
as numerix
28 import matplotlib
.transforms
as mtrans
29 import matplotlib
.backends
.backend_agg
as agg
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
):
47 x and y are equal length arrays, draw lines connecting each
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
):
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
))
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
)
82 self
.renderer
= PatchedRendererAgg(w
, h
, self
.figure
.dpi
)
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
93 agg
.GraphicsContextBase
.set_alpha(self
, math
.sqrt(alpha
))
96 class SpeedupRenderer(PatchedRendererAgg
):
99 Return an instance of a GraphicsContextBase
101 return SpeedupGraphicsContext()