Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, xx=1, yx=0, xy=0, yy=1, x0=0, y0=0):
self._pointer = ffi.new('cairo_matrix_t *')
cairo.cairo_matrix_init(self._pointer, xx, yx, xy, yy, x0, y0)
:copyright: Copyright 2013-2019 by Simon Sapin
:license: BSD, see LICENSE for details.
"""
import ctypes
import io
import os
import sys
import weakref
from . import _check_status, _keepref, cairo, constants, ffi
from .fonts import FontOptions, _encode_string
SURFACE_TARGET_KEY = ffi.new('cairo_user_data_key_t *')
def _make_read_func(file_obj):
"""Return a CFFI callback that reads from a file-like object."""
@ffi.callback("cairo_read_func_t", error=constants.STATUS_READ_ERROR)
def read_func(_closure, data, length):
string = file_obj.read(length)
if len(string) < length: # EOF too early
return constants.STATUS_READ_ERROR
ffi.buffer(data, length)[:len(string)] = string
return constants.STATUS_SUCCESS
return read_func
def _make_write_func(file_obj):
"""Return a CFFI callback that writes to a file-like object."""
def _rectangle(
libfunction,
cr,
x,
y,
width,
height,
radius,
corners=(True, True, True, True)
):
corners_vals = cairocffi.ffi.new("cairo_bool_t[4]")
for i, c in enumerate(corners):
corners_vals[i] = c
return libfunction(
cr._pointer,
x,
y,
width,
height,
radius,
corners_vals
)
raise TypeError('Expected a cairo.Context, got %r' % pycairo_context)
# On CPython, id() gives the memory address of a Python object.
# pycairo implements Context as a C struct:
# typedef struct {
# PyObject_HEAD
# cairo_t *ctx;
# PyObject *base;
# } PycairoContext;
# Still on CPython, object.__basicsize__ is the size of PyObject_HEAD,
# ie. the offset to the ctx field.
# ffi.cast() converts the integer address to a cairo_t** pointer.
# [0] dereferences that pointer, ie. read the ctx field.
# The result is a cairo_t* pointer that cairocffi can use.
return cairocffi.Context._from_pointer(
cairocffi.ffi.cast('cairo_t **',
id(pycairo_context) + object.__basicsize__)[0],
incref=True)
def voidp(ptr):
"""Convert a SIP void* type to a ffi cdata"""
if isinstance(ptr, ffi.CData):
return ptr
return ffi.cast('void *', int(ptr))
def _UNSAFE_cairocffi_context_to_pycairo(ctx):
import cairocffi
capi = get_capi()
ptr = ctx._pointer
cairocffi.cairo.cairo_reference(ptr)
ptr = int(cairocffi.ffi.cast('uintptr_t', ptr))
_ctx = capi.Context_FromContext(ptr, capi.Context_Type, None)
return _ctx
def get_radial_circles(self):
"""Return this radial gradient’s endpoint circles,
each specified as a center coordinate and a radius.
:returns: A ``(cx0, cy0, radius0, cx1, cy1, radius1)`` tuple of floats.
"""
circles = ffi.new('double[6]')
_check_status(cairo.cairo_pattern_get_radial_circles(
self._pointer, circles + 0, circles + 1, circles + 2,
circles + 3, circles + 4, circles + 5))
return tuple(circles)
Caution: the associated MIME data will be discarded
if you draw on the surface afterwards.
Use this method with care.
:param mime_type: The MIME type of the image data.
:type mime_type: ASCII string
:param data: The image data to attach to the surface.
:type data: bytes
*New in cairo 1.10.*
"""
mime_type = ffi.new('char[]', mime_type.encode('utf8'))
if data is None:
_check_status(cairo.cairo_surface_set_mime_data(
self._pointer, mime_type, ffi.NULL, 0, ffi.NULL, ffi.NULL))
else:
# TODO: avoid making a copy here if possible.
length = len(data)
data = ffi.new('unsigned char[]', data)
keep_alive = KeepAlive(data, mime_type)
_check_status(cairo.cairo_surface_set_mime_data(
self._pointer, mime_type, data, length,
*keep_alive.closure))
keep_alive.save() # Only on success