8000 Fix Cairo alpha-blending. · certik/matplotlib@1aa9c22 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1aa9c22

Browse files
committed
Fix Cairo alpha-blending.
svn path=/branches/transforms/; revision=4527
1 parent 06527d8 commit 1aa9c22

File tree

2 files changed

+39
-39
lines changed

2 files changed

+39
-39
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 21 additions & 23 deletions
212
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RendererBase:
2727
2828
The following methods *should* be implemented in the backend for
2929
optimization reasons:
30-
30+
3131
draw_markers
3232
draw_path_collection
3333
draw_quad_mesh
@@ -73,7 +73,7 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
7373
self.draw_path(gc, marker_path,
7474
marker_trans + transforms.Affine2D().translate(x, y),
7575
rgbFace)
76-
76+
7777
def draw_path_collection(self, master_transform, cliprect, clippath,
7878
clippath_trans, paths, all_transforms, offsets,
7979
offsetTrans, facecolors, edgecolors, linewidths,
@@ -126,12 +126,12 @@ def draw_quad_mesh(self, master_transform, cliprect, clippath,
126126
else:
127127
edgecolors = facecolors
128128
linewidths = npy.array([1.0], npy.float_)
129-
129+
130130
return self.draw_path_collection(
131131
master_transform, cliprect, clippath, clippath_trans,
132132
paths, [], offsets, offsetTrans, facecolors, edgecolors,
133133
linewidths, [], [antialiased])
134-
134+
135135
def _iter_collection_raw_paths(self, master_transform, paths, all_transforms):
136136
"""
137137
This is a helper method (along with _iter_collection) to make
@@ -203,20 +203,20 @@ def _iter_collection(self, path_ids, cliprect, clippath, clippath_trans,
203203
return
204204
if Noffsets:
205205
toffsets = offsetTrans.transform(offsets)
206-
206+
207207
gc = self.new_gc()
208208

209209
gc.set_clip_rectangle(cliprect)
210210
if clippath is not None:
211211
clippath = transforms.TransformedPath(clippath, clippath_trans)
212
gc.set_clip_path(clippath)
213-
213+
214214
if Nfacecolors == 0:
215215
rgbFace = None
216216

217217
if Nedgecolors == 0:
218218
gc.set_linewidth(0.0)
219-
219+
220220
xo, yo = 0, 0
221221
for i in xrange(N):
222222
path_id = path_ids[i % Npaths]
@@ -233,7 +233,7 @@ def _iter_collection(self, path_ids, cliprect, clippath, clippath_trans,
233233
gc.set_antialiased(antialiaseds[i % Naa])
234234

235235
yield xo, yo, path_id, gc, rgbFace
236-
236+
237237
def get_image_magnification(self):
238238
"""
239239
Get the factor by which to magnify images passed to draw_image.
@@ -282,7 +282,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False):
282282
your text.
283283
"""
284284
raise NotImplementedError
285-
285+
286286
def flipy(self):
287287
"""return true if y small numbers are top for renderer
288288
Is used for drawing text (text.py) and images (image.py) only
@@ -334,7 +334,7 @@ def start_rasterizing(self):
334334

335335
def stop_rasterizing(self):
336336
pass
337-
337+
338338

339339
class GraphicsContextBase:
340340
"""An abstract base class that provides color, line styles, etc...
@@ -380,8 +380,6 @@ def get_alpha(self):
380380
Return the alpha value used for blending - not supported on
381381
all backends
382382
"""
383-
if len(self._rgb) == 4:
384-
return self._rgb[3]
385383
return self._alpha
386384

387385
def get_antialiased(self):
@@ -795,7 +793,7 @@ def sort_artists(artists):
795793
# can't delete the artist
796794
while h:
797795
print "Removing",h
798-
if h.remove():
796+
if h.remove():
799797
self.draw_idle()
800798
break
801799
parent = None
@@ -804,7 +802,7 @@ def sort_artists(artists):
804802
parent = p
805803
break
806804
h = parent
807-
805+
808806
def onHilite(self, ev):
809807
"""
810808
Mouse event processor which highlights the artists
@@ -980,7 +978,7 @@ def get_width_height(self):
980978
# a) otherwise we'd have cyclical imports, since all of these
981979
# classes inherit from FigureCanvasBase
982980
# b) so we don't import a bunch of stuff the user may never use
983-
981+
984982
def print_emf(self, *args, **kwargs):
985983
from backends.backend_emf import FigureCanvasEMF # lazy import
986984
emf = self.switch_backends(FigureCanvasEMF)
@@ -990,7 +988,7 @@ def print_eps(self, *args, **kwargs):
990988
from backends.backend_ps import FigureCanvasPS # lazy import
991989
ps = self.switch_backends(FigureCanvasPS)
992990
return ps.print_eps(*args, **kwargs)
993-
991+
994992
def print_pdf(self, *args, **kwargs):
995993
from backends.backend_pdf import FigureCanvasPdf # lazy import
996994
pdf = self.switch_backends(FigureCanvasPdf)
@@ -1000,7 +998,7 @@ def print_png(self, *args, **kwargs):
1000998
from backends.backend_agg import FigureCanvasAgg # lazy import
1001999
agg = self.switch_backends(FigureCanvasAgg)
10021000
return agg.print_png(*args, **kwargs)
1003-< 8000 /span>
1001+
10041002
def print_ps(self, *args, **kwargs):
10051003
from backends.backend_ps import FigureCanvasPS # lazy import
10061004
ps = self.switch_backends(FigureCanvasPS)
@@ -1016,12 +1014,12 @@ def print_svg(self, *args, **kwargs):
10161014
from backends.backend_svg import FigureCanvasSVG # lazy import
10171015
svg = self.switch_backends(FigureCanvasSVG)
10181016
return svg.print_svg(*args, **kwargs)
1019-
1017+
10201018
def print_svgz(self, *args, **kwargs):
10211019
from backends.backend_svg import FigureCanvasSVG # lazy import
10221020
svg = self.switch_backends(FigureCanvasSVG)
10231021
return svg.print_svgz(*args, **kwargs)
1024-
1022+
10251023
def get_supported_filetypes(self):
10261024
return self.filetypes
10271025

@@ -1031,7 +1029,7 @@ def get_supported_filetypes_grouped(self):
10311029
groupings.setdefault(name, []).append(ext)
10321030
groupings[name].sort()
10331031
return groupings
1034-
1032+
10351033
def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
10361034
orientation='portrait', format=None, **kwargs):
10371035
"""
@@ -1069,7 +1067,7 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
10691067

10701068
if dpi is None:
10711069
dpi = rcParams['savefig.dpi']
1072-
1070+
10731071
origDPI = self.figure.dpi
10741072
origfacecolor = self.figure.get_facecolor()
10751073
origedgecolor = self.figure.get_edgecolor()
@@ -1092,12 +1090,12 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
10921090
self.figure.set_edgecolor(origedgecolor)
10931091
self.figure.set_canvas(self)
10941092
self.figure.canvas.draw()
1095-
1093+
10961094
return result
10971095

10981096
def get_default_filetype(self):
10991097
raise NotImplementedError
1100-
1098+
11011099
def set_window_title(self, title):
11021100
"""
11031101
Set the title text of the window containing the figure. Note that

lib/matplotlib/backends/backend_cairo.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def __init__(self, dpi):
9494
def set_ctx_from_surface (self, surface):
9595
self.ctx = cairo.Context (surface)
9696
self.ctx.save() # restore, save - when call new_gc()
97-
97+
9898

9999
def set_width_height(self, width, height):
100100
self.width = width
@@ -111,12 +111,12 @@ def _fill_and_stroke (self, ctx, fill_c, alpha):
111111
if len(fill_c) == 3:
112112
ctx.set_source_rgba (fill_c[0], fill_c[1], fill_c[2], alpha)
113113
else:
114-
ctx.set_source_rgba (*fill_c)
114+
ctx.set_source_rgba (fill_c[0], fill_c[1], fill_c[2], alpha*fill_c[3])
115115
ctx.fill_preserve()
116116
ctx.restore()
117117
ctx.stroke()
118118

119-
119+
120120
#@staticmethod
121121
def convert_path(ctx, tpath):
122122
for points, code in tpath.iter_segments():
@@ -134,7 +134,7 @@ def convert_path(ctx, tpath):
134134
ctx.close_path()
135135
convert_path = staticmethod(convert_path)
136136

137-
137+
138138
def draw_path(self, gc, path, transform, rgbFace=None):
139139
if len(path.vertices) > 18980:
140140
raise ValueError("The Cairo backend can not draw paths longer than 18980 points.")
@@ -143,7 +143,7 @@ def draw_path(self, gc, path, transform, rgbFace=None):
143143
transform = transform + \
144144
Affine2D().scale(1.0, -1.0).translate(0, self.height)
145145
tpath = transform.transform_path(path)
146-
146+
147147
ctx.new_path()
148148
self.convert_path(ctx, tpath)
149149

@@ -203,11 +203,11 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
203203
ctx.translate(x, y)
204204
if angle:
205205
ctx.rotate (-angle * npy.pi / 180)
206-
206+
207207
for font, fontsize, s, ox, oy in glyphs:
208208
ctx.new_path()
209209
ctx.move_to(ox, oy)
210-
210+
211211
fontProp = ttfFontProperty(font)
212212
ctx.save()
213213
ctx.select_font_face (fontProp.name,
@@ -228,7 +228,7 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle):
228228

229229
ctx.restore()
230230

231-
231+
232232
def flipy(self):
233233
if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name())
234234
return True
@@ -281,8 +281,8 @@ def new_gc(self):
281281
def points_to_pixels(self, points):
282282
if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name())
283283
return points/72.0 * self.dpi
284-
285-
284+
285+
286286
class GraphicsContextCairo(GraphicsContextBase):
287287
_joind = {
288288
'bevel' : cairo.LINE_JOIN_BEVEL,
@@ -323,6 +323,8 @@ def set_capstyle(self, cs):
323323

324324
def set_clip_rectangle(self, rectangle):
325325
self._cliprect = rectangle
326+
if rectangle is None:
327+
return
326328

327329
x,y,w,h = rectangle.bounds
328330
# pixel-aligned clip-regions are faster
@@ -345,7 +347,7 @@ def set_clip_path(self, path):
345347
RendererCairo.convert_path(ctx, tpath)
346348
ctx.clip()
347349

348-
350+
349351
def set_dashes(self, offset, dashes):
350352
self._dashes = offset, dashes
351353
if dashes == None:
@@ -382,7 +384,7 @@ def set_linewidth(self, w):
382384
self._linewidth = w
383385
self.ctx.set_line_width (self.renderer.points_to_pixels(w))
384386

385-
387+
386388
def new_figure_manager(num, *args, **kwargs): # called by backends/__init__.py
387389
"""
388390
Create a new figure manager instance
@@ -406,7 +408,7 @@ def print_png(self, fobj, *args, **kwargs):
406408

407409
self.figure.draw (renderer)
408410
surface.write_to_png (fobj)
409-
411+
410412
def print_pdf(self, fobj, *args, **kwargs):
411413
return self._save(fobj, 'pdf', *args, **kwargs)
412414

@@ -418,16 +420,16 @@ def print_svg(self, fobj, *args, **kwargs):
418420

419421
def print_svgz(self, fobj, *args, **kwargs):
420422
return self._save(fobj, 'svgz', *args, **kwargs)
421-
423+
422424
def get_default_filetype(self):
423425
return rcParams['cairo.format']
424-
426+
425427
def _save (self, fo, format, **kwargs):
426428
# save PDF/PS/SVG
427429
orientation = kwargs.get('orientation', 'portrait')
428430

429431
dpi = 72
430-
self.figure.dpi.set (dpi)
432+
self.figure.dpi = dpi
431433
w_in, h_in = self.figure.get_size_inches()
432434
width_in_points, height_in_points = w_in * dpi, h_in * dpi
433435

0 commit comments

Comments
 (0)
0