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