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

Skip to content

Commit c998849

Browse files
authored
Merge pull request #10092 from anntzer/cairo-cleanup
Minor cleanups to the cairo backend.
2 parents 6e67239 + 4493b15 commit c998849

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

lib/matplotlib/backends/backend_cairo.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,9 @@ def draw_image(self, gc, x, y, im):
232232
# the array.array functionality here to get cross version support.
233233
imbuffer = ArrayWrapper(im.flatten())
234234
else:
235-
# py2cairo uses PyObject_AsWriteBuffer
236-
# to get a pointer to the numpy array this works correctly
237-
# on a regular numpy array but not on a memory view.
238-
# At the time of writing the latest release version of
239-
# py3cairo still does not support create_for_data
235+
# pycairo uses PyObject_AsWriteBuffer to get a pointer to the
236+
# numpy array; this works correctly on a regular numpy array but
237+
# not on a py2 memoryview.
240238
imbuffer = im.flatten()
241239
surface = cairo.ImageSurface.create_for_data(
242240
imbuffer, cairo.FORMAT_ARGB32,
@@ -277,7 +275,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
277275
if not isinstance(s, six.text_type):
278276
s = six.text_type(s)
279277
else:
280-
if not six.PY3 and isinstance(s, six.text_type):
278+
if six.PY2 and isinstance(s, six.text_type):
281279
s = s.encode("utf-8")
282280

283281
ctx.show_text(s)
@@ -328,8 +326,8 @@ def get_canvas_width_height(self):
328326

329327
def get_text_width_height_descent(self, s, prop, ismath):
330328
if ismath:
331-
width, height, descent, fonts, used_characters = self.mathtext_parser.parse(
332-
s, self.dpi, prop)
329+
width, height, descent, fonts, used_characters = \
330+
self.mathtext_parser.parse(s, self.dpi, prop)
333331
return width, height, descent
334332

335333
ctx = self.text_ctx
@@ -354,7 +352,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
354352

355353
def new_gc(self):
356354
self.gc.ctx.save()
357-
self.gc._alpha = 1.0
355+
self.gc._alpha = 1
358356
self.gc._forced_alpha = False # if True, _alpha overrides A from RGBA
359357
return self.gc
360358

@@ -391,8 +389,9 @@ def set_alpha(self, alpha):
391389
else:
392390
self.ctx.set_source_rgba(rgb[0], rgb[1], rgb[2], rgb[3])
393391

394-
#def set_antialiased(self, b):
395-
# enable/disable anti-aliasing is not (yet) supported by Cairo
392+
# def set_antialiased(self, b):
393+
# cairo has many antialiasing modes, we need to pick one for True and
394+
# one for False.
396395

397396
def set_capstyle(self, cs):
398397
if cs in ('butt', 'round', 'projecting'):
@@ -404,9 +403,7 @@ def set_capstyle(self, cs):
404403
def set_clip_rectangle(self, rectangle):
405404
if not rectangle:
406405
return
407-
x, y, w, h = rectangle.bounds
408-
# pixel-aligned clip-regions are faster
409-
x,y,w,h = np.round(x), np.round(y), np.round(w), np.round(h)
406+
x, y, w, h = np.round(rectangle.bounds)
410407
ctx = self.ctx
411408
ctx.new_path()
412409
ctx.rectangle(x, self.renderer.height - h - y, w, h)
@@ -522,14 +519,9 @@ def _save(self, fo, fmt, **kwargs):
522519
ctx = renderer.gc.ctx
523520

524521
if orientation == 'landscape':
525-
ctx.rotate(np.pi/2)
522+
ctx.rotate(np.pi / 2)
526523
ctx.translate(0, -height_in_points)
527-
# cairo/src/cairo_ps_surface.c
528-
# '%%Orientation: Portrait' is always written to the file header
529-
# '%%Orientation: Landscape' would possibly cause problems
530-
# since some printers would rotate again ?
531-
# TODO:
532-
# add portrait/landscape checkbox to FileChooser
524+
# Perhaps add an '%%Orientation: Landscape' comment?
533525

534526
self.figure.draw(renderer)
535527

0 commit comments

Comments
 (0)