|
27 | 27 | "is installed") |
28 | 28 | else: |
29 | 29 | HAS_CAIRO_CFFI = False |
| 30 | + if cairo.version_info < (1, 11, 0): |
| 31 | + # Introduced create_for_data for Py3. |
| 32 | + raise ImportError( |
| 33 | + "cairo {} is installed; cairo>=1.11.0 is required" |
| 34 | + .format(cairo.version)) |
30 | 35 | else: |
31 | 36 | HAS_CAIRO_CFFI = True |
32 | 37 |
|
33 | | -if cairo.version_info < (1, 4, 0): |
34 | | - raise ImportError("cairo {} is installed; " |
35 | | - "cairo>=1.4.0 is required".format(cairo.version)) |
36 | 38 | backend_version = cairo.version |
37 | 39 |
|
38 | | -from matplotlib import cbook |
| 40 | +from .. import cbook |
39 | 41 | from matplotlib.backend_bases import ( |
40 | 42 | _Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase, |
41 | 43 | RendererBase) |
@@ -65,6 +67,23 @@ def _premultiplied_argb32_to_unmultiplied_rgba8888(buf): |
65 | 67 | return rgba |
66 | 68 |
|
67 | 69 |
|
| 70 | +if HAS_CAIRO_CFFI: |
| 71 | + # Convert a pycairo context to a cairocffi one. |
| 72 | + def _to_context(ctx): |
| 73 | + if not isinstance(ctx, cairo.Context): |
| 74 | + ctx = cairo.Context._from_pointer( |
| 75 | + cairo.ffi.cast( |
| 76 | + 'cairo_t **', |
| 77 | + id(ctx) + object.__basicsize__)[0], |
| 78 | + incref=True) |
| 79 | + return ctx |
| 80 | +else: |
| 81 | + # Pass-through a pycairo context. |
| 82 | + def _to_context(ctx): |
| 83 | + return ctx |
| 84 | + |
| 85 | + |
| 86 | +@cbook.deprecated("3.0") |
68 | 87 | class ArrayWrapper: |
69 | 88 | """Thin wrapper around numpy ndarray to expose the interface |
70 | 89 | expected by cairocffi. Basically replicates the |
@@ -348,21 +367,9 @@ def draw_image(self, gc, x, y, im): |
348 | 367 | im = im[:, :, (2, 1, 0, 3)] |
349 | 368 | else: |
350 | 369 | im = im[:, :, (3, 0, 1, 2)] |
351 | | - if HAS_CAIRO_CFFI: |
352 | | - # cairocffi tries to use the buffer_info from array.array |
353 | | - # that we replicate in ArrayWrapper and alternatively falls back |
354 | | - # on ctypes to get a pointer to the numpy array. This works |
355 | | - # correctly on a numpy array in python3 but not 2.7. We replicate |
356 | | - # the array.array functionality here to get cross version support. |
357 | | - imbuffer = ArrayWrapper(im.ravel()) |
358 | | - else: |
359 | | - # py2cairo uses PyObject_AsWriteBuffer to get a pointer to the |
360 | | - # numpy array; this works correctly on a regular numpy array but |
361 | | - # not on a memory view. |
362 | | - imbuffer = im.ravel() |
363 | 370 | surface = cairo.ImageSurface.create_for_data( |
364 | | - imbuffer, cairo.FORMAT_ARGB32, |
365 | | - im.shape[1], im.shape[0], im.shape[1]*4) |
| 371 | + im.ravel().data, cairo.FORMAT_ARGB32, |
| 372 | + im.shape[1], im.shape[0], im.shape[1] * 4) |
366 | 373 | ctx = gc.ctx |
367 | 374 | y = self.height - y - im.shape[0] |
368 | 375 |
|
|
0 commit comments