Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9fac26f

Browse files
author
Steve Chaplin
committed
SC 29/10/2004
svn path=/trunk/matplotlib/; revision=630
1 parent 9566c8f commit 9fac26f

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

lib/matplotlib/backends/backend_cairo.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,19 @@ def get_text_width_height(self, s, prop, ismath):
140140
return w, h
141141

142142

143-
def draw_arc(self, gcEdge, rgbFace, x, y, width, height, angle1, angle2):
143+
def draw_arc(self, gc, rgbFace, x, y, width, height, angle1, angle2):
144144
"""
145145
Draw an arc centered at x,y with width and height and angles
146146
from 0.0 to 360.0.
147-
148-
If rgbFace is not None, fill the arc with it. gcEdge
149-
is a GraphicsContext instance
147+
If rgbFace is not None, fill the arc with it.
150148
"""
151149
if Debug: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
152150
# cairo draws circular arcs (width=height) only?
153151
# could curve_to() and draw a spline instead?
154-
ctx = gcEdge.ctx
155-
y = self.height - y
156-
radius = width / 2
157-
ctx.arc (x, y, radius, angle1 * pi/180.0, angle2 * pi/180.0)
152+
radius = (height + width) / 4
153+
ctx = gc.ctx
154+
ctx.new_path()
155+
ctx.arc (x, self.height - y, radius, angle1 * pi/180.0, angle2 * pi/180.0)
158156

159157
if rgbFace:
160158
ctx.save()
@@ -188,12 +186,10 @@ def draw_line(self, gc, x1, y1, x2, y2):
188186
"""
189187
if Debug: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
190188
ctx = gc.ctx
191-
y1 = self.height - y1
192-
y2 = self.height - y2
193-
ctx.move_to (x1, y1); ctx.line_to (x2, y2)
189+
ctx.new_path()
190+
ctx.move_to (x1, self.height - y1)
191+
ctx.line_to (x2, self.height - y2)
194192
ctx.stroke()
195-
# should call new_path() so this line is not part of future paths?
196-
# or save()/restore() wrapper ?
197193

198194

199195
def draw_lines(self, gc, x, y):
@@ -202,44 +198,40 @@ def draw_lines(self, gc, x, y):
202198
point in x, y
203199
"""
204200
if Debug: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
205-
ctx = gc.ctx
206201
y = [self.height - y for y in y]
207202
points = zip(x,y)
208203
x, y = points.pop(0)
204+
ctx = gc.ctx
205+
ctx.new_path()
209206
ctx.move_to (x, y)
210207

211208
for x,y in points:
212209
ctx.line_to (x, y)
213210
ctx.stroke()
214211

215-
#ctx.new_path() # testing
216-
# should call new_path() so these lines are not part of future paths?
217-
218212

219213
def draw_point(self, gc, x, y):
220214
"""
221215
Draw a single point at x,y
222216
"""
223217
if Debug: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
224218
# render by drawing a 0.5 radius circle
225-
y = self.height - y
226-
gc.ctx.arc (x, y, 0.5, 0, 2*pi)
219+
gc.ctx.new_path()
220+
gc.ctx.arc (x, self.height - y, 0.5, 0, 2*pi)
227221
gc.ctx.fill()
228222

229223

230-
def draw_polygon(self, gcEdge, rgbFace, points):
224+
def draw_polygon(self, gc, rgbFace, points):
231225
"""
232226
Draw a polygon. points is a len vertices tuple, each element
233227
giving the x,y coords a vertex.
234-
235-
If rgbFace is not None, fill the rectangle with it. gcEdge
236-
is a GraphicsContext instance
228+
If rgbFace is not None, fill the rectangle with it.
237229
"""
238230
if Debug: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
239-
ctx = gcEdge.ctx
240231
points = [(x, (self.height-y)) for x,y in points]
241232

242-
ctx.new_path() # not required?
233+
ctx = gc.ctx
234+
ctx.new_path()
243235
x, y = points.pop(0)
244236
ctx.move_to (x, y)
245237
for x,y in points:
@@ -254,18 +246,17 @@ def draw_polygon(self, gcEdge, rgbFace, points):
254246
ctx.stroke()
255247

256248

257-
def draw_rectangle(self, gcEdge, rgbFace, x, y, width, height):
249+
def draw_rectangle(self, gc, rgbFace, x, y, width, height):
258250
"""
259251
Draw a non-filled rectangle at x,y (lower left) with width and height,
260252
using the GraphicsContext gcEdge.
261253
Draw a filled rectangle within it of color rgbFace, if rgbFace is not
262254
None.
263255
"""
264256
if Debug: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
265-
ctx = gcEdge.ctx
266-
y = self.height - y - height
267-
268-
ctx.rectangle (x, y, width, height)
257+
ctx = gc.ctx
258+
ctx.new_path()
259+
ctx.rectangle (x, self.height - y - height, width, height)
269260
if rgbFace:
270261
ctx.save()
271262
ctx.set_rgb_color (*rgbFace)
@@ -288,17 +279,17 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
288279
else:
289280
# see also get_text_width_height()
290281
# text is looking too small - size, scale problem?
282+
ctx.new_path()
283+
ctx.move_to (x, y)
291284
ctx.select_font (prop.get_name(),
292285
self.fontangles [prop.get_style()],
293286
self.fontweights[prop.get_weight()])
294287
scale = self.get_text_scale()
295288
size = prop.get_size_in_points()
296-
ctx.move_to (x, y)
297289
if angle: # rotated text looks awful!
298290
ctx.rotate (-angle * pi / 180)
299291
ctx.scale_font (scale*size)
300292
ctx.show_text (s)
301-
# new_path() ?
302293

303294

304295
def flipy(self):
@@ -385,9 +376,14 @@ def set_clip_rectangle(self, rectangle):
385376
ctx = self.ctx
386377
ctx.new_path()
387378
ctx.rectangle (x, self.renderer.height - h - y, w, h)
379+
380+
#ctx.save() # uncomment to view the clip rectangle
381+
#ctx.set_rgb_color(1,0,0)
382+
#ctx.set_line_width(6)
383+
#ctx.stroke()
384+
#ctx.restore()
385+
388386
ctx.clip ()
389-
ctx.new_path() # prevents the rectangle from being drawn by next ctx.stroke()/fill()
390-
# is better to do new_path() (or save(),restore()) at start of EVERY draw_*() op ?
391387

392388

393389
def set_dashes(self, offset, dashes):

lib/matplotlib/backends/backend_gtkcairo.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
1616
show, draw_if_interactive,\
1717
error_msg, NavigationToolbar, PIXELS_PER_INCH, backend_version
1818

19+
import gobject
20+
1921
try:
2022
import cairo
2123
import cairo.gtk
@@ -173,3 +175,10 @@ def print_figure(self, filename, dpi=150, facecolor='w', edgecolor='w'):
173175
filename, msg))
174176
else:
175177
error_msg('Could not save figure\n%s' % msg)
178+
179+
180+
def raise_msg_to_str(msg): # used by print_figure
181+
"""msg is a return arg from a raise. Join with new lines"""
182+
if not is_string_like(msg):
183+
msg = '\n'.join(map(str, msg))
184+
return msg

0 commit comments

Comments
 (0)