8000 SC 2005/03/10 · matplotlib/matplotlib@59df307 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59df307

Browse files
author
Steve Chaplin
committed
SC 2005/03/10
svn path=/trunk/matplotlib/; revision=1061
1 parent d6e7501 commit 59df307

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

lib/matplotlib/backends/backend_cairo.py

Lines changed: 32 additions & 18 deletions
+
ctx.line_to (x, y)
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,11 @@ class RendererCairo(RendererBase):
8989
}
9090

9191

92-
def __init__(self, matrix, dpi):
92+
def __init__(self, dpi):
9393
"""width, height - the canvas width, height. Is not necessarily
9494
the same as the surface (pixmap) width, height
9595
"""
9696
if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
97-
self.matrix = matrix
9897
self.dpi = dpi
9998
self.text_ctx = cairo.Context()
10099

@@ -105,6 +104,9 @@ def _set_pixmap(self, pixmap):
105104
def _set_width_height(self, width, height):
106105
self.width = width
107106
self.height = height
107+
self.matrix_flipy = cairo.Matrix (d=-1, ty=self.height)
108+
# use matrix_flipy for ALL rendering?
109+
# - problem with text? - will need to switch matrix_flipy off, or do a font transform?
108110

109111
def draw_arc(self, gc, rgbFace, x, y, width, height, angle1, angle2):
110112
if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
@@ -141,7 +143,6 @@ def draw_image(self, x, y, im, origin, bbox):
141143

142144
ctx = cairo.Context()
143145
ctx.set_target_surface (self.surface)
144-
ctx.set_matrix (self.matrix)
145146

146147
rows, cols, buf = im.buffer_argb32() # ARGB32, but colors still wrong
147148
X = fromstring(buf, UInt8)
@@ -178,27 +179,29 @@ def draw_line(self, gc, x1, y1, x2, y2):
178179
ctx.stroke()
179180

180181

181-
#def draw_lines(self, gc, x, y):
182182
def draw_lines(self, gc, x, y, transform=None):
183183
if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
184184

185-
# guessing what needs to be done
186185
if transform:
187186
if transform.need_nonlinear():
188187
x, y = transform.nonlinear_only_numerix(x, y)
189188
x, y = transform.numerix_x_y(x, y)
190189

191-
y = [self.height - y for y in y]
192-
points = zip(x,y)
193-
x, y = points.pop(0)
194190
ctx = gc.ctx
191+
matrix_old = ctx.matrix
192+
ctx.set_matrix (self.matrix_flipy)
193+
194+
points = izip(x,y)
195+
x, y = points.next()
195196
ctx.new_path()
196197
ctx.move_to (x, y)
197198

198199
for x,y in points:
199200
ctx.line_to (x, y)
200201
ctx.stroke()
201202

203+
ctx.set_matrix (matrix_old)
204+
202205

203206
def draw_markers(self, gc, path, x, y, transform):
204207
if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
@@ -221,6 +224,7 @@ def draw_markers(self, gc, path, x, y, transform):
221224

222225
def generate_path (path):
223226
"""trace path and return fill_rgb
227+
coords are mpl points
224228
"""
225229
for p in path:
226230
code = p[0]
@@ -266,11 +270,14 @@ def draw_polygon(self, gc, rgbFace, points):
266270
if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
267271

268272
ctx = gc.ctx
273+
matrix_old = ctx.matrix
274+
ctx.set_matrix (self.matrix_flipy)
275+
269276
ctx.new_path()
270277
x, y = points[0]
271-
ctx.move_to (x, self.height - y)
278+
ctx.move_to (x, y)
272279
for x,y in points[1:]:
273-
ctx.line_to (x, self.height - y)
280
274281
ctx.close_path()
275282

276283
if rgbFace:
@@ -279,7 +286,8 @@ def draw_polygon(self, gc, rgbFace, points):
279286
ctx.fill()
280287
ctx.restore()
281288
ctx.stroke()
282-
289+
290+
ctx.set_matrix (matrix_old)
283291

284292
def draw_rectangle(self, gc, rgbFace, x, y, width, height):
285293
if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
@@ -294,7 +302,8 @@ def draw_rectangle(self, gc, rgbFace, x, y, width, height):
294302
ctx.stroke()
295303

296304

297-
def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
305+
def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
306+
# Note: x,y are device/display coords, not user-coords, unlike other draw_* methods
298307
if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
299308

300309
if ismath:
@@ -375,6 +384,7 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
375384
def flipy(self):
376385
if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
377386
return True
387+
#return False # tried - all draw objects ok except text (and images?) which comes out mirrored!
378388

379389

380390
def get_canvas_width_height(self):
416426
if DEBUG: print 'backend_cairo.RendererCairo.%s()' % _fn_name()
417427
gc = GraphicsContextCairo (renderer=self)
418428
gc.ctx.set_target_surface (self.surface)
419-
gc.ctx.set_matrix (self.matrix)
420429
return gc
421430

422431

@@ -443,12 +452,17 @@ def __init__(self, renderer):
443452
GraphicsContextBase.__init__(self)
444453
self.renderer = renderer
445454
self.ctx = cairo.Context()
446-
455+
# default is 0.1, raise value to increase performance
456+
# (and lower quality)
457+
self.ctx.set_tolerance(0.5)
447458

448459
def set_alpha(self, alpha):
449460
self._alpha = alpha
450461
self.ctx.set_alpha(alpha)
451462

463+
#def set_antialiased(self, b):
464+
# enable/disable anti-aliasing is not (yet) supported by Cairo
465+
452466

453467
def set_capstyle(self, cs):
454468
if cs in ('butt', 'round', 'projecting'):
@@ -487,8 +501,8 @@ def set_dashes(self, offset, dashes):
487501
if dashes == None:
488502
self.ctx.set_dash([], 0) # switch dashes off
489503
else:
490-
dashes_pixels = self.renderer.points_to_pixels(asarray(dashes))
491-
self.ctx.set_dash(dashes_pixels, offset)
504+
self.ctx.set_dash(self.renderer.points_to_pixels(asarray(dashes)),
505+
offset)
492506

493507

494508
def set_foreground(self, fg, isRGB=None):
@@ -574,7 +588,7 @@ def _save_png (figure, fileObject):
574588
ctx = cairo.Context()
575589
ctx.set_target_png (fileObject, cairo.FORMAT_ARGB32, width, height)
576590

577-
renderer = RendererCairo (ctx.matrix, figure.dpi)
591+
renderer = RendererCairo (figure.dpi)
578592
renderer._set_width_height(width, height)
579593
renderer.surface = ctx.target_surface
580594
figure.draw(renderer)
@@ -612,7 +626,7 @@ def _save_ps_pdf (figure, fileObject, ext, orientation):
612626
# TODO:
613627
# add portrait/landscape checkbox to FileChooser
614628

615-
renderer = RendererCairo (ctx.matrix, figure.dpi)
629+
renderer = RendererCairo (figure.dpi)
616630
renderer._set_width_height(width, height)
617631
renderer.surface = ctx.target_surface
618632
figure.draw(renderer)

lib/matplotlib/backends/backend_gdk.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ def rgb_to_gdk_color(self, rgb):
404404
return color
405405

406406

407+
#def set_antialiased(self, b):
408+
# anti-aliasing is not supported by GDK
409+
407410
def set_capstyle(self, cs):
408411
GraphicsContextBase.set_capstyle(self, cs)
409412
self.gdkGC.cap_style = self._capd[self._capstyle]

lib/matplotlib/backends/backend_gtkcairo.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
except AttributeError:
1313
backend_version = 'PyGTK(%d.%d.%d),PyCairo ??' % gtk.pygtk_version
1414
else:
15-
16-
1715
backend_version = 'PyGTK(%d.%d.%d),PyCairo(%d.%d.%d)' % (gtk.pygtk_version + cairo.version_info)
1816

1917

@@ -34,5 +32,4 @@ class FigureCanvasGTKCairo(FigureCanvasGTK):
3432
def _renderer_init(self):
3533
"""Override to use Cairo rather than GDK renderer"""
3634
if DEBUG: print 'backend_gtkcairo.%s()' % fn_name()
37-
matrix = cairo.Matrix ()
38-
self._renderer = RendererCairo (matrix, self.figure.dpi)
35+
self._renderer = RendererCairo (self.figure.dpi)

0 commit comments

Comments
 (0)
0