| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
e |
| 6 |
n |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#define PY_SSIZE_T_CLEAN |
| 33 |
#include <Python.h> |
| 34 |
|
| 35 |
#include "private.h" |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
PyObject * |
| 41 |
PycairoSurface_FromSurface (cairo_surface_t *surface, PyObject *base) { |
| 42 |
PyTypeObject *type = NULL; |
| 43 |
PyObject *o; |
| 44 |
|
| 45 |
assert (surface != NULL); |
| 46 |
|
| 47 |
if (Pycairo_Check_Status (cairo_surface_status (surface))) { |
| 48 |
cairo_surface_destroy (surface); |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
switch (cairo_surface_get_type (surface)) { |
| 53 |
#ifdef CAIRO_HAS_IMAGE_SURFACE |
| 54 |
case CAIRO_SURFACE_TYPE_IMAGE: |
| 55 |
type = &PycairoImageSurface_Type; |
| 56 |
break; |
| 57 |
#endif |
| 58 |
#ifdef CAIRO_HAS_PDF_SURFACE |
| 59 |
case CAIRO_SURFACE_TYPE_PDF: |
| 60 |
type = &PycairoPDFSurface_Type; |
| 61 |
break; |
| 62 |
#endif |
| 63 |
#ifdef CAIRO_HAS_PS_SURFACE |
| 64 |
case CAIRO_SURFACE_TYPE_PS: |
| 65 |
type = &PycairoPSSurface_Type; |
| 66 |
break; |
| 67 |
#endif |
| 68 |
#ifdef CAIRO_HAS_RECORDING_SURFACE |
| 69 |
case CAIRO_SURFACE_TYPE_RECORDING: |
| 70 |
type = &PycairoRecordingSurface_Type; |
| 71 |
break; |
| 72 |
#endif |
| 73 |
#ifdef CAIRO_HAS_SVG_SURFACE |
| 74 |
case CAIRO_SURFACE_TYPE_SVG: |
| 75 |
type = &PycairoSVGSurface_Type; |
| 76 |
break; |
| 77 |
#endif |
| 78 |
#ifdef CAIRO_HAS_WIN32_SURFACE |
| 79 |
case CAIRO_SURFACE_TYPE_WIN32: |
| 80 |
type = &PycairoWin32Surface_Type; |
| 81 |
break; |
| 82 |
case CAIRO_SURFACE_TYPE_WIN32_PRINTING: |
| 83 |
type = &PycairoWin32PrintingSurface_Type; |
| 84 |
break; |
| 85 |
#endif |
| 86 |
#ifdef CAIRO_HAS_XCB_SURFACE |
| 87 |
case CAIRO_SURFACE_TYPE_XCB: |
| 88 |
type = &PycairoXCBSurface_Type; |
| 89 |
break; |
| 90 |
#endif |
| 91 |
#ifdef CAIRO_HAS_XLIB_SURFACE |
| 92 |
case CAIRO_SURFACE_TYPE_XLIB: |
| 93 |
type = &PycairoXlibSurface_Type; |
| 94 |
break; |
| 95 |
#endif |
| 96 |
#ifdef CAIRO_HAS_SCRIPT_SURFACE |
| 97 |
case CAIRO_SURFACE_TYPE_SCRIPT: |
| 98 |
type = &PycairoScriptSurface_Type; |
| 99 |
break; |
| 100 |
#endif |
| 101 |
#ifdef CAIRO_HAS_TEE_SURFACE |
| 102 |
case CAIRO_SURFACE_TYPE_TEE: |
| 103 |
type = &PycairoTeeSurface_Type; |
| 104 |
break; |
| 105 |
#endif |
| 106 |
default: |
| 107 |
type = &PycairoSurface_Type; |
| 108 |
break; |
| 109 |
} |
| 110 |
o = type->tp_alloc (type, 0); |
| 111 |
if (o == NULL) { |
| 112 |
cairo_surface_destroy (surface); |
| 113 |
} else { |
| 114 |
((PycairoSurface *)o)->surface = surface; |
| 115 |
Py_XINCREF(base); |
| 116 |
((PycairoSurface *)o)->base = base; |
| 117 |
} |
| 118 |
return o; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
static cairo_status_t |
| 126 |
_write_func (void *closure, const unsigned char *data, unsigned int length) { |
| 127 |
PyGILState_STATE gstate = PyGILState_Ensure(); |
| 128 |
PyObject *res = PyObject_CallMethod ((PyObject *)closure, "write", "(y#)", |
| 129 |
data, (Py_ssize_t)length); |
| 130 |
if (res == NULL) { |
| 131 |
PyErr_Clear(); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
PyGILState_Release(gstate); |
| 136 |
return CAIRO_STATUS_WRITE_ERROR; |
| 137 |
} |
| 138 |
Py_DECREF(res); |
| 139 |
PyGILState_Release(gstate); |
| 140 |
return CAIRO_STATUS_SUCCESS; |
| 141 |
} |
| 142 |
|
| 143 |
static const cairo_user_data_key_t surface_base_object_key; |
| 144 |
static const cairo_user_data_key_t surface_is_mapped_image; |
| 145 |
static const cairo_user_data_key_t surface_buffer_view_key; |
| 146 |
|
| 147 |
static void |
| 148 |
surface_dealloc (PycairoSurface *o) { |
| 149 |
if (o->surface) { |
| 150 |
if (cairo_surface_get_user_data ( |
| 151 |
o->surface, &surface_is_mapped_image) == NULL) { |
| 152 |
cairo_surface_destroy(o->surface); |
| 153 |
} |
| 154 |
o->surface = NULL; |
| 155 |
} |
| 156 |
Py_CLEAR(o->base); |
| 157 |
|
| 158 |
Py_TYPE(o)->tp_free(o); |
| 159 |
} |
| 160 |
|
| 161 |
static void |
| 162 |
_decref_destroy_func(void *user_data) { |
| 163 |
PyGILState_STATE gstate = PyGILState_Ensure(); |
| 164 |
Py_DECREF(user_data); |
| 165 |
PyGILState_Release(gstate); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
static PyObject * |
| 172 |
_surface_create_with_object(cairo_surface_t *surface, PyObject *base) { |
| 173 |
PyObject *pysurface; |
| 174 |
cairo_status_t status; |
| 175 |
|
| 176 |
pysurface = PycairoSurface_FromSurface(surface, NULL); |
| 177 |
if (pysurface == NULL) |
| 178 |
return NULL; |
| 179 |
|
| 180 |
if (base != NULL) { |
| 181 |
status = cairo_surface_set_user_data( |
| 182 |
surface, &surface_base_object_key, base, _decref_destroy_func); |
| 183 |
if (status != CAIRO_STATUS_SUCCESS) |
| 184 |
Py_DECREF(pysurface); |
| 185 |
RETURN_NULL_IF_CAIRO_ERROR(status); |
| 186 |
Py_INCREF(base); |
| 187 |
} |
| 188 |
|
| 189 |
return pysurface; |
| 190 |
} |
| 191 |
|
| 192 |
static PyObject * |
| 193 |
surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 194 |
PyErr_SetString(PyExc_TypeError, |
| 195 |
"The Surface type cannot be instantiated"); |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
|
| 199 |
static PyObject * |
| 200 |
surface_copy_page (PycairoSurface *o, PyObject *ignored) { |
| 201 |
Py_BEGIN_ALLOW_THREADS; |
| 202 |
cairo_surface_copy_page (o->surface); |
| 203 |
Py_END_ALLOW_THREADS; |
| 204 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 205 |
Py_RETURN_NONE; |
| 206 |
} |
| 207 |
|
| 208 |
static PyObject * |
| 209 |
surface_create_similar (PycairoSurface *o, PyObject *args) { |
| 210 |
cairo_content_t content; |
| 211 |
int width, height, content_arg; |
| 212 |
|
| 213 |
if (!PyArg_ParseTuple (args, "iii:Surface.create_similar", |
| 214 |
&content_arg, &width, &height)) |
| 215 |
return NULL; |
| 216 |
|
| 217 |
content = (cairo_content_t)content_arg; |
| 218 |
|
| 219 |
return PycairoSurface_FromSurface ( |
| 220 |
cairo_surface_create_similar (o->surface, content, width, height), |
| 221 |
NULL); |
| 222 |
} |
| 223 |
|
| 224 |
static PyObject * |
| 225 |
surface_finish (PycairoSurface *o, PyObject *ignored) { |
| 226 |
cairo_surface_finish (o->surface); |
| 227 |
Py_CLEAR(o->base); |
| 228 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 229 |
Py_RETURN_NONE; |
| 230 |
} |
| 231 |
|
| 232 |
static PyObject * |
| 233 |
surface_flush (PycairoSurface *o, PyObject *ignored) { |
| 234 |
Py_BEGIN_ALLOW_THREADS; |
| 235 |
cairo_surface_flush (o->surface); |
| 236 |
Py_END_ALLOW_THREADS; |
| 237 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 238 |
Py_RETURN_NONE; |
| 239 |
} |
| 240 |
|
| 241 |
static PyObject * |
| 242 |
surface_get_content (PycairoSurface *o, PyObject *ignored) { |
| 243 |
RETURN_INT_ENUM (Content, cairo_surface_get_content (o->surface)); |
| 244 |
} |
| 245 |
|
| 246 |
static PyObject * |
| 247 |
surface_get_device_offset (PycairoSurface *o, PyObject *ignored) { |
| 248 |
double x_offset, y_offset; |
| 249 |
cairo_surface_get_device_offset (o->surface, &x_offset, &y_offset); |
| 250 |
return Py_BuildValue("(dd)", x_offset, y_offset); |
| 251 |
} |
| 252 |
|
| 253 |
static PyObject * |
| 254 |
surface_get_device_scale (PycairoSurface *o, PyObject *ignored) { |
| 255 |
double x_scale, y_scale; |
| 256 |
cairo_surface_get_device_scale (o->surface, &x_scale, &y_scale); |
| 257 |
return Py_BuildValue("(dd)", x_scale, y_scale); |
| 258 |
} |
| 259 |
|
| 260 |
static PyObject * |
| 261 |
surface_get_fallback_resolution (PycairoSurface *o, PyObject *ignored) { |
| 262 |
double x_ppi, y_ppi; |
| 263 |
cairo_surface_get_fallback_resolution (o->surface, &x_ppi, &y_ppi); |
| 264 |
return Py_BuildValue("(dd)", x_ppi, y_ppi); |
| 265 |
} |
| 266 |
|
| 267 |
static PyObject * |
| 268 |
surface_get_font_options (PycairoSurface *o, PyObject *ignored) { |
| 269 |
cairo_font_options_t *options = cairo_font_options_create(); |
| 270 |
|
| 271 |
cairo_surface_get_font_options (o->surface, options); |
| 272 |
|
| 273 |
return PycairoFontOptions_FromFontOptions (options); |
| 274 |
} |
| 275 |
|
| 276 |
static PyObject * |
| 277 |
surface_get_device (PycairoSurface *o, PyObject *ignored) { |
| 278 |
cairo_device_t *device = cairo_surface_get_device (o->surface); |
| 279 |
if (device == NULL) |
| 280 |
Py_RETURN_NONE; |
| 281 |
return PycairoDevice_FromDevice (cairo_device_reference (device)); |
| 282 |
} |
| 283 |
|
| 284 |
static PyObject * |
| 285 |
surface_mark_dirty (PycairoSurface *o, PyObject *ignored) { |
| 286 |
cairo_surface_mark_dirty (o->surface); |
| 287 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 288 |
Py_RETURN_NONE; |
| 289 |
} |
| 290 |
|
| 291 |
static PyObject * |
| 292 |
surface_mark_dirty_rectangle (PycairoSurface *o, PyObject *args) { |
| 293 |
int x, y, width, height; |
| 294 |
|
| 295 |
if (!PyArg_ParseTuple(args, "iiii:Surface.mark_dirty_rectangle", |
| 296 |
&x, &y, &width, &height)) |
| 297 |
return NULL; |
| 298 |
|
| 299 |
cairo_surface_mark_dirty_rectangle (o->surface, x, y, width, height); |
| 300 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 301 |
Py_RETURN_NONE; |
| 302 |
} |
| 303 |
|
| 304 |
static PyObject * |
| 305 |
surface_set_device_offset (PycairoSurface *o, PyObject *args) { |
| 306 |
double x_offset, y_offset; |
| 307 |
|
| 308 |
if (!PyArg_ParseTuple (args, "dd:Surface.set_device_offset", |
| 309 |
&x_offset, &y_offset)) |
| 310 |
return NULL; |
| 311 |
|
| 312 |
cairo_surface_set_device_offset (o->surface, x_offset, y_offset); |
| 313 |
Py_RETURN_NONE; |
| 314 |
} |
| 315 |
|
| 316 |
static PyObject * |
| 317 |
surface_set_device_scale (PycairoSurface *o, PyObject *args) { |
| 318 |
double x_scale, y_scale; |
| 319 |
cairo_matrix_t transform; |
| 320 |
|
| 321 |
if (!PyArg_ParseTuple (args, "dd:Surface.set_device_scale", |
| 322 |
&x_scale, &y_scale)) |
| 323 |
return NULL; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
cairo_matrix_init_scale (&transform, x_scale, y_scale); |
| 329 |
RETURN_NULL_IF_CAIRO_ERROR (cairo_matrix_invert (&transform)); |
| 330 |
|
| 331 |
cairo_surface_set_device_scale (o->surface, x_scale, y_scale); |
| 332 |
Py_RETURN_NONE; |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
static PyObject * |
| 337 |
surface_set_fallback_resolution (PycairoSurface *o, PyObject *args) { |
| 338 |
double x_ppi, y_ppi; |
| 339 |
|
| 340 |
if (!PyArg_ParseTuple(args, "dd:Surface.set_fallback_resolution", |
| 341 |
&x_ppi, &y_ppi)) |
| 342 |
return NULL; |
| 343 |
cairo_surface_set_fallback_resolution (o->surface, x_ppi, y_ppi); |
| 344 |
Py_RETURN_NONE; |
| 345 |
} |
| 346 |
|
| 347 |
static PyObject * |
| 348 |
surface_create_for_rectangle (PycairoSurface *o, PyObject *args) { |
| 349 |
double x, y, width, height; |
| 350 |
cairo_surface_t *new; |
| 351 |
|
| 352 |
if (!PyArg_ParseTuple(args, "dddd:Surface.create_for_rectangle", |
| 353 |
&x, &y, &width, &height)) |
| 354 |
return NULL; |
| 355 |
|
| 356 |
Py_BEGIN_ALLOW_THREADS; |
| 357 |
new = cairo_surface_create_for_rectangle(o->surface, x, y, width, height); |
| 358 |
Py_END_ALLOW_THREADS; |
| 359 |
|
| 360 |
return PycairoSurface_FromSurface(new, NULL); |
| 361 |
} |
| 362 |
|
| 363 |
static PyObject * |
| 364 |
surface_show_page (PycairoSurface *o, PyObject *ignored) { |
| 365 |
Py_BEGIN_ALLOW_THREADS; |
| 366 |
cairo_surface_show_page (o->surface); |
| 367 |
Py_END_ALLOW_THREADS; |
| 368 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 369 |
Py_RETURN_NONE; |
| 370 |
} |
| 371 |
|
| 372 |
static PyObject * |
| 373 |
surface_create_similar_image (PycairoSurface *o, PyObject *args) { |
| 374 |
cairo_format_t format; |
| 375 |
int width, height, format_arg; |
| 376 |
cairo_surface_t *new; |
| 377 |
|
| 378 |
if (!PyArg_ParseTuple (args, "iii:Surface.create_similar_image", |
| 379 |
&format_arg, &width, &height)) |
| 380 |
return NULL; |
| 381 |
|
| 382 |
format = (cairo_format_t)format_arg; |
| 383 |
|
| 384 |
Py_BEGIN_ALLOW_THREADS; |
| 385 |
new = cairo_surface_create_similar_image (o->surface, format, width, height); |
| 386 |
Py_END_ALLOW_THREADS; |
| 387 |
|
| 388 |
return PycairoSurface_FromSurface (new, NULL); |
| 389 |
} |
| 390 |
|
| 391 |
#ifdef CAIRO_HAS_PNG_FUNCTIONS |
| 392 |
static PyObject * |
| 393 |
surface_write_to_png (PycairoSurface *o, PyObject *args) { |
| 394 |
cairo_status_t status; |
| 395 |
char *name = NULL; |
| 396 |
PyObject *file; |
| 397 |
|
| 398 |
if (!PyArg_ParseTuple (args, "O:Surface.write_to_png", &file)) |
| 399 |
return NULL; |
| 400 |
|
| 401 |
if (Pycairo_is_fspath (file)) { |
| 402 |
if (!PyArg_ParseTuple (args, "O&:Surface.write_to_png", |
| 403 |
Pycairo_fspath_converter, &name)) |
| 404 |
return NULL; |
| 405 |
Py_BEGIN_ALLOW_THREADS; |
| 406 |
status = cairo_surface_write_to_png (o->surface, name); |
| 407 |
Py_END_ALLOW_THREADS; |
| 408 |
PyMem_Free (name); |
| 409 |
} else { |
| 410 |
if (PyArg_ParseTuple (args, "O&:Surface.write_to_png", |
| 411 |
Pycairo_writer_converter, &file)) { |
| 412 |
Py_BEGIN_ALLOW_THREADS; |
| 413 |
status = cairo_surface_write_to_png_stream (o->surface, _write_func, |
| 414 |
file); |
| 415 |
Py_END_ALLOW_THREADS; |
| 416 |
} else { |
| 417 |
PyErr_Clear (); |
| 418 |
PyErr_SetString (PyExc_TypeError, |
| 419 |
"Surface.write_to_png takes one argument which must be " |
| 420 |
"a filename, file object, or a file-like object " |
| 421 |
"which has a \"write\" method (like BytesIO) taking bytes"); |
| 422 |
return NULL; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
RETURN_NULL_IF_CAIRO_ERROR (status); |
| 427 |
Py_RETURN_NONE; |
| 428 |
} |
| 429 |
#endif |
| 430 |
|
| 431 |
static void |
| 432 |
_destroy_mime_user_data_func (PyObject *user_data) { |
| 433 |
PyGILState_STATE gstate = PyGILState_Ensure(); |
| 434 |
|
| 435 |
Py_DECREF(user_data); |
| 436 |
|
| 437 |
PyGILState_Release(gstate); |
| 438 |
} |
| 439 |
|
| 440 |
static void |
| 441 |
_destroy_mime_data_func (PyObject *user_data) { |
| 442 |
cairo_surface_t *surface; |
| 443 |
Py_buffer *view; |
| 444 |
PyObject *mime_intern; |
| 445 |
|
| 446 |
PyGILState_STATE gstate = PyGILState_Ensure(); |
| 447 |
|
| 448 |
|
| 449 |
surface = PyCapsule_GetPointer(PyTuple_GET_ITEM(user_data, 0), NULL); |
| 450 |
view = PyCapsule_GetPointer(PyTuple_GET_ITEM(user_data, 1), NULL); |
| 451 |
mime_intern = PyTuple_GET_ITEM(user_data, 3); |
| 452 |
cairo_surface_set_user_data( |
| 453 |
surface, (cairo_user_data_key_t *)mime_intern, NULL, NULL); |
| 454 |
|
| 455 |
|
| 456 |
PyBuffer_Release (view); |
| 457 |
PyMem_Free (view); |
| 458 |
Py_DECREF(user_data); |
| 459 |
|
| 460 |
PyGILState_Release(gstate); |
| 461 |
} |
| 462 |
|
| 463 |
static PyObject * |
| 464 |
surface_set_mime_data (PycairoSurface *o, PyObject *args) { |
| 465 |
PyObject *obj, *user_data, *mime_intern, *surface_capsule, *view_capsule; |
| 466 |
const char *mime_type; |
| 467 |
int res; |
| 468 |
cairo_status_t status; |
| 469 |
|
| 470 |
if (!PyArg_ParseTuple(args, "sO:Surface.set_mime_data", &mime_type, &obj)) |
| 471 |
return NULL; |
| 472 |
|
| 473 |
if (obj == Py_None) { |
| 474 |
status = cairo_surface_set_mime_data ( |
| 475 |
o->surface, mime_type, NULL, 0, NULL, NULL); |
| 476 |
|
| 477 |
RETURN_NULL_IF_CAIRO_ERROR(status); |
| 478 |
Py_RETURN_NONE; |
| 479 |
} |
| 480 |
|
| 481 |
Py_buffer *view = PyMem_Malloc (sizeof (Py_buffer)); |
| 482 |
if (view == NULL) { |
| 483 |
PyErr_NoMemory (); |
| 484 |
return NULL; |
| 485 |
} |
| 486 |
|
| 487 |
res = PyObject_GetBuffer (obj, view, PyBUF_SIMPLE); |
| 488 |
if (res == -1) { |
| 489 |
PyMem_Free (view); |
| 490 |
return NULL; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
mime_intern = PyUnicode_InternFromString (mime_type); |
| 498 |
surface_capsule = PyCapsule_New(o->surface, NULL, NULL); |
| 499 |
view_capsule = PyCapsule_New(view, NULL, NULL); |
| 500 |
user_data = Py_BuildValue("(NNOO)", surface_capsule, view_capsule, obj, mime_intern); |
| 501 |
if (user_data == NULL) { |
| 502 |
PyBuffer_Release (view); |
| 503 |
PyMem_Free (view); |
| 504 |
return NULL; |
| 505 |
} |
| 506 |
|
| 507 |
status = cairo_surface_set_user_data( |
| 508 |
o->surface, (cairo_user_data_key_t *)mime_intern, user_data, |
| 509 |
(cairo_destroy_func_t)_destroy_mime_user_data_func); |
| 510 |
|
| 511 |
if (status != CAIRO_STATUS_SUCCESS) { |
| 512 |
PyBuffer_Release (view); |
| 513 |
PyMem_Free (view); |
| 514 |
Py_DECREF(user_data); |
| 515 |
Pycairo_Check_Status (status); |
| 516 |
return NULL; |
| 517 |
} |
| 518 |
|
| 519 |
Py_INCREF(user_data); |
| 520 |
status = cairo_surface_set_mime_data ( |
| 521 |
o->surface, mime_type, view->buf, (unsigned long)view->len, |
| 522 |
(cairo_destroy_func_t)_destroy_mime_data_func, user_data); |
| 523 |
|
| 524 |
if (status != CAIRO_STATUS_SUCCESS) { |
| 525 |
cairo_surface_set_user_data( |
| 526 |
o->surface, (cairo_user_data_key_t *)mime_intern, NULL, NULL); |
| 527 |
PyBuffer_Release (view); |
| 528 |
PyMem_Free (view); |
| 529 |
Py_DECREF(user_data); |
| 530 |
Pycairo_Check_Status (status); |
| 531 |
return NULL; |
| 532 |
} |
| 533 |
|
| 534 |
Py_RETURN_NONE; |
| 535 |
} |
| 536 |
|
| 537 |
static PyObject * |
| 538 |
surface_get_mime_data (PycairoSurface *o, PyObject *args) { |
| 539 |
PyObject *user_data, *obj, *mime_intern; |
| 540 |
const char *mime_type; |
| 541 |
const unsigned char *buffer; |
| 542 |
unsigned long buffer_len; |
| 543 |
|
| 544 |
if (!PyArg_ParseTuple(args, "s:Surface.get_mime_data", &mime_type)) |
| 545 |
return NULL; |
| 546 |
|
| 547 |
cairo_surface_get_mime_data (o->surface, mime_type, &buffer, &buffer_len); |
| 548 |
if (buffer == NULL) { |
| 549 |
Py_RETURN_NONE; |
| 550 |
} |
| 551 |
|
| 552 |
mime_intern = PyUnicode_InternFromString (mime_type); |
| 553 |
user_data = cairo_surface_get_user_data( |
| 554 |
o->surface, (cairo_user_data_key_t *)mime_intern); |
| 555 |
|
| 556 |
if (user_data == NULL) { |
| 557 |
|
| 558 |
return Py_BuildValue("y#", buffer, buffer_len); |
| 559 |
} else { |
| 560 |
obj = PyTuple_GET_ITEM(user_data, 2); |
| 561 |
Py_INCREF(obj); |
| 562 |
return obj; |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
static PyObject * |
| 567 |
surface_supports_mime_type (PycairoSurface *self, PyObject *args) { |
| 568 |
const char *mime_type; |
| 569 |
|
| 570 |
if (!PyArg_ParseTuple(args, "s:Surface.supports_mime_type", &mime_type)) |
| 571 |
return NULL; |
| 572 |
|
| 573 |
return PyBool_FromLong( |
| 574 |
cairo_surface_supports_mime_type(self->surface, mime_type)); |
| 575 |
} |
| 576 |
|
| 577 |
static PyObject * |
| 578 |
surface_has_show_text_glyphs (PycairoSurface *o, PyObject *ignored) { |
| 579 |
cairo_bool_t result; |
| 580 |
|
| 581 |
Py_BEGIN_ALLOW_THREADS; |
| 582 |
result = cairo_surface_has_show_text_glyphs (o->surface); |
| 583 |
Py_END_ALLOW_THREADS; |
| 584 |
|
| 585 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (o->surface); |
| 586 |
return PyBool_FromLong (result); |
| 587 |
} |
| 588 |
|
| 589 |
#ifdef CAIRO_HAS_IMAGE_SURFACE |
| 590 |
|
| 591 |
static PyObject * |
| 592 |
surface_map_to_image (PycairoSurface *self, PyObject *args) { |
| 593 |
PyObject *pyextents, *pymapped; |
| 594 |
cairo_rectangle_int_t *extents; |
| 595 |
cairo_surface_t *mapped_surface; |
| 596 |
|
| 597 |
if (!PyArg_ParseTuple(args, "O:Surface.map_to_image", &pyextents)) |
| 598 |
return NULL; |
| 599 |
|
| 600 |
if (PyObject_TypeCheck (pyextents, &PycairoRectangleInt_Type)) { |
| 601 |
extents = &(((PycairoRectangleInt *)pyextents)->rectangle_int); |
| 602 |
} else { |
| 603 |
if (pyextents == Py_None) { |
| 604 |
extents = NULL; |
| 605 |
} else { |
| 606 |
PyErr_SetString (PyExc_TypeError, |
| 607 |
"argument must be a RectangleInt or None."); |
| 608 |
return NULL; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
Py_BEGIN_ALLOW_THREADS; |
| 613 |
mapped_surface = cairo_surface_map_to_image (self->surface, extents); |
| 614 |
Py_END_ALLOW_THREADS; |
| 615 |
|
| 616 |
if (Pycairo_Check_Status (cairo_surface_status (mapped_surface))) { |
| 617 |
cairo_surface_destroy (mapped_surface); |
| 618 |
return NULL; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
cairo_surface_set_user_data ( |
| 623 |
mapped_surface, &surface_is_mapped_image, (void*)1, NULL); |
| 624 |
|
| 625 |
pymapped = PycairoMappedImageSurface_Type.tp_alloc ( |
| 626 |
&PycairoMappedImageSurface_Type, 0); |
| 627 |
|
| 628 |
if (pymapped == NULL) { |
| 629 |
Py_BEGIN_ALLOW_THREADS; |
| 630 |
cairo_surface_unmap_image (self->surface, mapped_surface); |
| 631 |
Py_END_ALLOW_THREADS; |
| 632 |
return NULL; |
| 633 |
} |
| 634 |
|
| 635 |
((PycairoSurface *)pymapped)->surface = mapped_surface; |
| 636 |
Py_XINCREF (self); |
| 637 |
((PycairoSurface *)pymapped)->base = (PyObject *)self; |
| 638 |
|
| 639 |
return pymapped; |
| 640 |
} |
| 641 |
|
| 642 |
static PyObject * |
| 643 |
surface_unmap_image (PycairoSurface *self, PyObject *args) { |
| 644 |
PycairoSurface *pymapped; |
| 645 |
cairo_surface_t *base_surface, *fake_surface; |
| 646 |
|
| 647 |
if (!PyArg_ParseTuple(args, "O!:Surface.unmap_image", |
| 648 |
&PycairoMappedImageSurface_Type, &pymapped)) |
| 649 |
return NULL; |
| 650 |
|
| 651 |
if (cairo_surface_get_user_data (pymapped->surface, |
| 652 |
&surface_is_mapped_image) == NULL) { |
| 653 |
PyErr_SetString (PyExc_RuntimeError, |
| 654 |
"MappedImageSurface was already unmapped"); |
| 655 |
return NULL; |
| 656 |
} |
| 657 |
|
| 658 |
base_surface = ((PycairoSurface *)(pymapped->base))->surface; |
| 659 |
if (base_surface != self->surface) { |
| 660 |
PyErr_SetString (PyExc_ValueError, |
| 661 |
"ImageSurface isn't mapped from this surface"); |
| 662 |
return NULL; |
| 663 |
} |
| 664 |
|
| 665 |
Py_BEGIN_ALLOW_THREADS; |
| 666 |
cairo_surface_unmap_image (self->surface, pymapped->surface); |
| 667 |
Py_END_ALLOW_THREADS; |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
fake_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0); |
| 673 |
cairo_surface_finish (fake_surface); |
| 674 |
pymapped->surface = fake_surface; |
| 675 |
|
| 676 |
Py_CLEAR(pymapped->base); |
| 677 |
|
| 678 |
Py_RETURN_NONE; |
| 679 |
} |
| 680 |
|
| 681 |
#endif |
| 682 |
|
| 683 |
static PyObject * |
| 684 |
surface_ctx_enter (PyObject *obj, PyObject *ignored) { |
| 685 |
Py_INCREF (obj); |
| 686 |
return obj; |
| 687 |
} |
| 688 |
|
| 689 |
static PyObject * |
| 690 |
surface_ctx_exit (PycairoSurface *obj, PyObject *args) { |
| 691 |
Py_BEGIN_ALLOW_THREADS; |
| 692 |
cairo_surface_finish (obj->surface); |
| 693 |
Py_END_ALLOW_THREADS; |
| 694 |
Py_RETURN_NONE; |
| 695 |
} |
| 696 |
|
| 697 |
static PyMethodDef surface_methods[] = { |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
{"__enter__", (PyCFunction)surface_ctx_enter, METH_NOARGS}, |
| 706 |
{"__exit__", (PyCFunction)surface_ctx_exit, METH_VARARGS}, |
| 707 |
{"copy_page", (PyCFunction)surface_copy_page, METH_NOARGS}, |
| 708 |
{"create_similar", (PyCFunction)surface_create_similar, METH_VARARGS}, |
| 709 |
{"create_similar_image", (PyCFunction)surface_create_similar_image, |
| 710 |
METH_VARARGS}, |
| 711 |
{"finish", (PyCFunction)surface_finish, METH_NOARGS}, |
| 712 |
{"flush", (PyCFunction)surface_flush, METH_NOARGS}, |
| 713 |
{"get_content", (PyCFunction)surface_get_content, METH_NOARGS}, |
| 714 |
{"get_device_offset",(PyCFunction)surface_get_device_offset,METH_NOARGS}, |
| 715 |
{"get_device_scale", (PyCFunction)surface_get_device_scale, METH_NOARGS}, |
| 716 |
{"get_fallback_resolution",(PyCFunction)surface_get_fallback_resolution, |
| 717 |
METH_NOARGS}, |
| 718 |
{"get_font_options",(PyCFunction)surface_get_font_options, METH_NOARGS}, |
| 719 |
{"get_device", (PyCFunction)surface_get_device, METH_NOARGS}, |
| 720 |
{"mark_dirty", (PyCFunction)surface_mark_dirty, METH_NOARGS}, |
| 721 |
{"mark_dirty_rectangle", (PyCFunction)surface_mark_dirty_rectangle, |
| 722 |
METH_VARARGS}, |
| 723 |
{"set_device_offset",(PyCFunction)surface_set_device_offset,METH_VARARGS}, |
| 724 |
{"set_device_scale", (PyCFunction)surface_set_device_scale, METH_VARARGS}, |
| 725 |
{"set_fallback_resolution",(PyCFunction)surface_set_fallback_resolution, |
| 726 |
METH_VARARGS}, |
| 727 |
{"show_page", (PyCFunction)surface_show_page, METH_NOARGS}, |
| 728 |
#ifdef CAIRO_HAS_PNG_FUNCTIONS |
| 729 |
{"write_to_png", (PyCFunction)surface_write_to_png, METH_VARARGS}, |
| 730 |
#endif |
| 731 |
{"set_mime_data", (PyCFunction)surface_set_mime_data, METH_VARARGS}, |
| 732 |
{"get_mime_data", (PyCFunction)surface_get_mime_data, METH_VARARGS}, |
| 733 |
{"supports_mime_type", (PyCFunction)surface_supports_mime_type, |
| 734 |
METH_VARARGS}, |
| 735 |
{"create_for_rectangle",(PyCFunction)surface_create_for_rectangle, |
| 736 |
METH_VARARGS}, |
| 737 |
{"has_show_text_glyphs", (PyCFunction)surface_has_show_text_glyphs, |
| 738 |
METH_NOARGS}, |
| 739 |
#ifdef CAIRO_HAS_IMAGE_SURFACE |
| 740 |
{"map_to_image", (PyCFunction)surface_map_to_image, METH_VARARGS}, |
| 741 |
{"unmap_image", (PyCFunction)surface_unmap_image, METH_VARARGS}, |
| 742 |
#endif |
| 743 |
{NULL, NULL, 0, NULL}, |
| 744 |
}; |
| 745 |
|
| 746 |
static PyObject* |
| 747 |
surface_richcompare (PyObject *self, PyObject *other, int op) |
| 748 |
{ |
| 749 |
if (Py_TYPE(self) == Py_TYPE(other)) |
| 750 |
return Pycairo_richcompare ( |
| 751 |
((PycairoSurface *)self)->surface, |
| 752 |
((PycairoSurface *)other)->surface, |
| 753 |
op); |
| 754 |
else { |
| 755 |
Py_INCREF(Py_NotImplemented); |
| 756 |
return Py_NotImplemented; |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
static Py_hash_t |
| 761 |
surface_hash (PyObject *self) |
| 762 |
{ |
| 763 |
return PYCAIRO_Py_hash_t_FromVoidPtr (((PycairoSurface *)self)->surface); |
| 764 |
} |
| 765 |
|
| 766 |
PyTypeObject PycairoSurface_Type = { |
| 767 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 768 |
"cairo.Surface", |
| 769 |
sizeof(PycairoSurface), |
| 770 |
0, |
| 771 |
(destructor)surface_dealloc, |
| 772 |
0, |
| 773 |
0, |
| 774 |
0, |
| 775 |
0, |
| 776 |
0, |
| 777 |
0, |
| 778 |
0, |
| 779 |
0, |
| 780 |
surface_hash, |
| 781 |
0, |
| 782 |
0, |
| 783 |
0, |
| 784 |
0, |
| 785 |
0, |
| 786 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 787 |
0, |
| 788 |
0, |
| 789 |
0, |
| 790 |
surface_richcompare, |
| 791 |
0, |
| 792 |
0, |
| 793 |
0, |
| 794 |
surface_methods, |
| 795 |
0, |
| 796 |
0, |
| 797 |
0, |
| 798 |
0, |
| 799 |
0, |
| 800 |
0, |
| 801 |
0, |
| 802 |
0, |
| 803 |
0, |
| 804 |
(newfunc)surface_new, |
| 805 |
0, |
| 806 |
0, |
| 807 |
0, |
| 808 |
}; |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
#ifdef CAIRO_HAS_IMAGE_SURFACE |
| 813 |
|
| 814 |
static PyObject * |
| 815 |
image_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 816 |
cairo_format_t format; |
| 817 |
int width, height, format_arg; |
| 818 |
|
| 819 |
if (!PyArg_ParseTuple (args, "iii:ImageSurface.__new__", |
| 820 |
&format_arg, &width, &height)) |
| 821 |
return NULL; |
| 822 |
|
| 823 |
format = (cairo_format_t)format_arg; |
| 824 |
|
| 825 |
return PycairoSurface_FromSurface ( |
| 826 |
cairo_image_surface_create (format, width, height), |
| 827 |
NULL); |
| 828 |
} |
| 829 |
|
| 830 |
static void |
| 831 |
_release_buffer_destroy_func (void *user_data) { |
| 832 |
Py_buffer *view = (Py_buffer *)user_data; |
| 833 |
PyGILState_STATE gstate = PyGILState_Ensure(); |
| 834 |
PyBuffer_Release (view); |
| 835 |
PyMem_Free (view); |
| 836 |
PyGILState_Release(gstate); |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
static PyObject * |
| 841 |
image_surface_create_for_data (PyTypeObject *type, PyObject *args) { |
| 842 |
cairo_surface_t *surface; |
| 843 |
cairo_format_t format; |
| 844 |
cairo_status_t status; |
| 845 |
int width, height, stride = -1, res, format_arg; |
| 846 |
PyObject *obj; |
| 847 |
|
| 848 |
if (!PyArg_ParseTuple (args, "Oiii|i:ImageSurface.create_for_data", |
| 849 |
&obj, &format_arg, &width, &height, &stride)) |
| 850 |
return NULL; |
| 851 |
|
| 852 |
format = (cairo_format_t)format_arg; |
| 853 |
|
| 854 |
if (width <= 0) { |
| 855 |
PyErr_SetString(PyExc_ValueError, "width must be positive"); |
| 856 |
return NULL; |
| 857 |
} |
| 858 |
if (height <= 0) { |
| 859 |
PyErr_SetString(PyExc_ValueError, "height must be positive"); |
| 860 |
return NULL; |
| 861 |
} |
| 862 |
|
| 863 |
if (stride < 0) { |
| 864 |
stride = cairo_format_stride_for_width (format, width); |
| 865 |
if (stride == -1){ |
| 866 |
PyErr_SetString(PyExc_ValueError, |
| 867 |
"format is invalid or the width too large"); |
| 868 |
return NULL; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
Py_buffer *view = PyMem_Malloc (sizeof (Py_buffer)); |
| 873 |
if (view == NULL) { |
| 874 |
PyErr_NoMemory (); |
| 875 |
return NULL; |
| 876 |
} |
| 877 |
|
| 878 |
res = PyObject_GetBuffer (obj, view, PyBUF_WRITABLE); |
| 879 |
if (res == -1) { |
| 880 |
PyMem_Free (view); |
| 881 |
return NULL; |
| 882 |
} |
| 883 |
|
| 884 |
if (height * stride > view->len) { |
| 885 |
PyBuffer_Release (view); |
| 886 |
PyMem_Free (view); |
| 887 |
PyErr_SetString(PyExc_TypeError, "buffer is not long enough"); |
| 888 |
return NULL; |
| 889 |
} |
| 890 |
|
| 891 |
Py_BEGIN_ALLOW_THREADS; |
| 892 |
surface = cairo_image_surface_create_for_data (view->buf, format, width, |
| 893 |
height, stride); |
| 894 |
Py_END_ALLOW_THREADS; |
| 895 |
|
| 896 |
status = cairo_surface_set_user_data( |
| 897 |
surface, &surface_buffer_view_key, view, |
| 898 |
(cairo_destroy_func_t)_release_buffer_destroy_func); |
| 899 |
if (Pycairo_Check_Status (status)) { |
| 900 |
cairo_surface_destroy (surface); |
| 901 |
PyBuffer_Release (view); |
| 902 |
PyMem_Free (view); |
| 903 |
return NULL; |
| 904 |
} |
| 905 |
|
| 906 |
return PycairoSurface_FromSurface(surface, NULL); |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
#ifdef CAIRO_HAS_PNG_FUNCTIONS |
| 911 |
static cairo_status_t |
| 912 |
_read_func (void *closure, unsigned char *data, unsigned int length) { |
| 913 |
char *buffer; |
| 914 |
int ret; |
| 915 |
Py_ssize_t str_length; |
| 916 |
cairo_status_t status = CAIRO_STATUS_READ_ERROR; |
| 917 |
PyGILState_STATE gstate = PyGILState_Ensure(); |
| 918 |
PyObject *pystr = PyObject_CallMethod ((PyObject *)closure, "read", "(i)", |
| 919 |
length); |
| 920 |
if (pystr == NULL) { |
| 921 |
PyErr_Clear(); |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
goto end; |
| 926 |
} |
| 927 |
ret = PyBytes_AsStringAndSize (pystr, &buffer, &str_length); |
| 928 |
if (ret == -1 || str_length < (Py_ssize_t)length) { |
| 929 |
PyErr_Clear(); |
| 930 |
goto end; |
| 931 |
} |
| 932 |
|
| 933 |
memcpy (data, buffer, (size_t)str_length); |
| 934 |
status = CAIRO_STATUS_SUCCESS; |
| 935 |
end: |
| 936 |
Py_XDECREF(pystr); |
| 937 |
PyGILState_Release(gstate); |
| 938 |
return status; |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
static PyObject * |
| 943 |
image_surface_create_from_png (PyTypeObject *type, PyObject *args) { |
| 944 |
cairo_surface_t *image_surface; |
| 945 |
PyObject *file; |
| 946 |
char *name; |
| 947 |
|
| 948 |
if (!PyArg_ParseTuple (args, "O:ImageSurface.create_from_png", &file)) |
| 949 |
return NULL; |
| 950 |
|
| 951 |
if (Pycairo_is_fspath (file)) { |
| 952 |
if (!PyArg_ParseTuple(args, "O&:ImageSurface.create_from_png", |
| 953 |
Pycairo_fspath_converter, &name)) |
| 954 |
return NULL; |
| 955 |
|
| 956 |
Py_BEGIN_ALLOW_THREADS; |
| 957 |
image_surface = cairo_image_surface_create_from_png (name); |
| 958 |
Py_END_ALLOW_THREADS; |
| 959 |
PyMem_Free(name); |
| 960 |
return PycairoSurface_FromSurface (image_surface, NULL); |
| 961 |
} else { |
| 962 |
if (PyArg_ParseTuple (args, "O&:ImageSurface.create_from_png", |
| 963 |
Pycairo_reader_converter, &file)) { |
| 964 |
Py_BEGIN_ALLOW_THREADS; |
| 965 |
image_surface = cairo_image_surface_create_from_png_stream ( |
| 966 |
_read_func, file); |
| 967 |
Py_END_ALLOW_THREADS; |
| 968 |
return PycairoSurface_FromSurface (image_surface, NULL); |
| 969 |
} else { |
| 970 |
PyErr_SetString(PyExc_TypeError, |
| 971 |
"ImageSurface.create_from_png argument must be a " |
| 972 |
"filename (str), file object, or an object that has a " |
| 973 |
"\"read\" method (like BytesIO) returning bytes."); |
| 974 |
return NULL; |
| 975 |
} |
| 976 |
} |
| 977 |
} |
| 978 |
#endif |
| 979 |
|
| 980 |
|
| 981 |
static PyObject * |
| 982 |
image_surface_format_stride_for_width (PyObject *self, PyObject *args) { |
| 983 |
cairo_format_t format; |
| 984 |
int width, format_arg; |
| 985 |
|
| 986 |
if (!PyArg_ParseTuple (args, "ii:format_stride_for_width", |
| 987 |
&format_arg, &width)) |
| 988 |
return NULL; |
| 989 |
|
| 990 |
format = (cairo_format_t)format_arg; |
| 991 |
|
| 992 |
return PyLong_FromLong (cairo_format_stride_for_width (format, width)); |
| 993 |
} |
| 994 |
|
| 995 |
static PyObject * |
| 996 |
image_surface_get_data (PycairoImageSurface *o, PyObject *ignored) { |
| 997 |
cairo_surface_t *surface; |
| 998 |
int height, stride; |
| 999 |
unsigned char * buffer; |
| 1000 |
|
| 1001 |
surface = o->surface; |
| 1002 |
buffer = cairo_image_surface_get_data (surface); |
| 1003 |
if (buffer == NULL) { |
| 1004 |
Py_RETURN_NONE; |
| 1005 |
} |
| 1006 |
height = cairo_image_surface_get_height (surface); |
| 1007 |
stride = cairo_image_surface_get_stride (surface); |
| 1008 |
|
| 1009 |
return buffer_proxy_create_view((PyObject *)o, buffer, height * stride, 0); |
| 1010 |
} |
| 1011 |
|
| 1012 |
static PyObject * |
| 1013 |
image_surface_get_format (PycairoImageSurface *o, PyObject *ignored) { |
| 1014 |
RETURN_INT_ENUM (Format, cairo_image_surface_get_format (o->surface)); |
| 1015 |
} |
| 1016 |
|
| 1017 |
static PyObject * |
| 1018 |
image_surface_get_height (PycairoImageSurface *o, PyObject *ignored) { |
| 1019 |
return PyLong_FromLong (cairo_image_surface_get_height (o->surface)); |
| 1020 |
} |
| 1021 |
|
| 1022 |
static PyObject * |
| 1023 |
image_surface_get_stride (PycairoImageSurface *o, PyObject *ignored) { |
| 1024 |
return PyLong_FromLong (cairo_image_surface_get_stride (o->surface)); |
| 1025 |
} |
| 1026 |
|
| 1027 |
static PyObject * |
| 1028 |
image_surface_get_width (PycairoImageSurface *o, PyObject *ignored) { |
| 1029 |
return PyLong_FromLong (cairo_image_surface_get_width (o->surface)); |
| 1030 |
} |
| 1031 |
|
| 1032 |
static PyMethodDef image_surface_methods[] = { |
| 1033 |
{"create_for_data",(PyCFunction)image_surface_create_for_data, |
| 1034 |
METH_VARARGS | METH_CLASS}, |
| 1035 |
#ifdef CAIRO_HAS_PNG_FUNCTIONS |
| 1036 |
{"create_from_png", (PyCFunction)image_surface_create_from_png, |
| 1037 |
METH_VARARGS | METH_CLASS}, |
| 1038 |
#endif |
| 1039 |
{"format_stride_for_width", |
| 1040 |
(PyCFunction)image_surface_format_stride_for_width, |
| 1041 |
METH_VARARGS | METH_STATIC}, |
| 1042 |
{"get_data", (PyCFunction)image_surface_get_data, METH_NOARGS}, |
| 1043 |
{"get_format", (PyCFunction)image_surface_get_format, METH_NOARGS}, |
| 1044 |
{"get_height", (PyCFunction)image_surface_get_height, METH_NOARGS}, |
| 1045 |
{"get_stride", (PyCFunction)image_surface_get_stride, METH_NOARGS}, |
| 1046 |
{"get_width", (PyCFunction)image_surface_get_width, METH_NOARGS}, |
| 1047 |
{NULL, NULL, 0, NULL}, |
| 1048 |
}; |
| 1049 |
|
| 1050 |
|
| 1051 |
PyTypeObject PycairoImageSurface_Type = { |
| 1052 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1053 |
"cairo.ImageSurface", |
| 1054 |
sizeof(PycairoImageSurface), |
| 1055 |
0, |
| 1056 |
0, |
| 1057 |
0, |
| 1058 |
0, |
| 1059 |
0, |
| 1060 |
0, |
| 1061 |
0, |
| 1062 |
0, |
| 1063 |
0, |
| 1064 |
0, |
| 1065 |
0, |
| 1066 |
0, |
| 1067 |
0, |
| 1068 |
0, |
| 1069 |
0, |
| 1070 |
0, |
| 1071 |
Py_TPFLAGS_DEFAULT, |
| 1072 |
0, |
| 1073 |
0, |
| 1074 |
0, |
| 1075 |
0, |
| 1076 |
0, |
| 1077 |
0, |
| 1078 |
0, |
| 1079 |
image_surface_methods, |
| 1080 |
0, |
| 1081 |
0, |
| 1082 |
&PycairoSurface_Type, |
| 1083 |
0, |
| 1084 |
0, |
| 1085 |
0, |
| 1086 |
0, |
| 1087 |
0, |
| 1088 |
0, |
| 1089 |
(newfunc)image_surface_new, |
| 1090 |
0, |
| 1091 |
0, |
| 1092 |
0, |
| 1093 |
}; |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
static PyObject * |
| 1099 |
mapped_image_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 1100 |
PyErr_SetString(PyExc_TypeError, |
| 1101 |
"The MappedImage type cannot be instantiated"); |
| 1102 |
return NULL; |
| 1103 |
} |
| 1104 |
|
| 1105 |
static void |
| 1106 |
mapped_image_surface_dealloc (PycairoImageSurface *self) { |
| 1107 |
PycairoSurface *pybasesurface = (PycairoSurface *)(self->base); |
| 1108 |
|
| 1109 |
if (cairo_surface_get_user_data ( |
| 1110 |
self->surface, &surface_is_mapped_image) != NULL) { |
| 1111 |
cairo_surface_unmap_image (pybasesurface->surface, self->surface); |
| 1112 |
self->surface = NULL; |
| 1113 |
} else { |
| 1114 |
cairo_surface_destroy(self->surface); |
| 1115 |
self->surface = NULL; |
| 1116 |
} |
| 1117 |
|
| 1118 |
Py_CLEAR (self->base); |
| 1119 |
Py_TYPE (self)->tp_free (self); |
| 1120 |
} |
| 1121 |
|
| 1122 |
static PyObject * |
| 1123 |
mapped_image_surface_finish (PycairoSurface *self, PyObject *ignored) { |
| 1124 |
PyErr_SetString(PyExc_RuntimeError, |
| 1125 |
"The MappedImage type cannot be finished, " |
| 1126 |
"use Surface.unmap_image instead"); |
| 1127 |
return NULL; |
| 1128 |
} |
| 1129 |
|
| 1130 |
static PyObject * |
| 1131 |
mapped_image_surface_ctx_exit (PycairoImageSurface *self, PyObject *args) { |
| 1132 |
PycairoSurface *pybasesurface = (PycairoSurface *)(self->base); |
| 1133 |
PyObject *subargs, *result; |
| 1134 |
|
| 1135 |
subargs = Py_BuildValue("(O)", self); |
| 1136 |
if (subargs == NULL) |
| 1137 |
return NULL; |
| 1138 |
result = surface_unmap_image (pybasesurface, subargs); |
| 1139 |
Py_DECREF (subargs); |
| 1140 |
return result; |
| 1141 |
} |
| 1142 |
|
| 1143 |
static PyMethodDef mapped_image_surface_methods[] = { |
| 1144 |
{"__exit__", (PyCFunction)mapped_image_surface_ctx_exit, METH_VARARGS}, |
| 1145 |
{"finish", (PyCFunction)mapped_image_surface_finish, METH_NOARGS}, |
| 1146 |
{NULL, NULL, 0, NULL}, |
| 1147 |
}; |
| 1148 |
|
| 1149 |
PyTypeObject PycairoMappedImageSurface_Type = { |
| 1150 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1151 |
"__cairo__.MappedImageSurface", |
| 1152 |
sizeof(PycairoImageSurface), |
| 1153 |
0, |
| 1154 |
(destructor) |
| 1155 |
mapped_image_surface_dealloc, |
| 1156 |
0, |
| 1157 |
0, |
| 1158 |
0, |
| 1159 |
0, |
| 1160 |
0, |
| 1161 |
0, |
| 1162 |
0, |
| 1163 |
0, |
| 1164 |
PyObject_HashNotImplemented, |
| 1165 |
0, |
| 1166 |
0, |
| 1167 |
0, |
| 1168 |
0, |
| 1169 |
0, |
| 1170 |
Py_TPFLAGS_DEFAULT, |
| 1171 |
0, |
| 1172 |
0, |
| 1173 |
0, |
| 1174 |
0, |
| 1175 |
0, |
| 1176 |
0, |
| 1177 |
0, |
| 1178 |
mapped_image_surface_methods, |
| 1179 |
0, |
| 1180 |
0, |
| 1181 |
&PycairoImageSurface_Type, |
| 1182 |
0, |
| 1183 |
0, |
| 1184 |
0, |
| 1185 |
0, |
| 1186 |
0, |
| 1187 |
0, |
| 1188 |
(newfunc)mapped_image_surface_new, |
| 1189 |
0, |
| 1190 |
0, |
| 1191 |
0, |
| 1192 |
}; |
| 1193 |
|
| 1194 |
|
| 1195 |
#endif |
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
#ifdef CAIRO_HAS_PDF_SURFACE |
| 1200 |
#include <cairo-pdf.h> |
| 1201 |
|
| 1202 |
static PyObject * |
| 1203 |
pdf_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 1204 |
double width_in_points, height_in_points; |
| 1205 |
PyObject *file; |
| 1206 |
cairo_surface_t *sfc; |
| 1207 |
char *name; |
| 1208 |
|
| 1209 |
if (!PyArg_ParseTuple (args, "Odd:PDFSurface.__new__", |
| 1210 |
&file, &width_in_points, &height_in_points)) |
| 1211 |
return NULL; |
| 1212 |
|
| 1213 |
if (Pycairo_is_fspath (file) || file == Py_None) { |
| 1214 |
if (!PyArg_ParseTuple (args, "O&dd:PDFSurface.__new__", |
| 1215 |
Pycairo_fspath_none_converter, &name, |
| 1216 |
&width_in_points, &height_in_points)) |
| 1217 |
return NULL; |
| 1218 |
|
| 1219 |
Py_BEGIN_ALLOW_THREADS; |
| 1220 |
sfc = cairo_pdf_surface_create (name, width_in_points, height_in_points); |
| 1221 |
Py_END_ALLOW_THREADS; |
| 1222 |
PyMem_Free(name); |
| 1223 |
return PycairoSurface_FromSurface (sfc, NULL); |
| 1224 |
} else { |
| 1225 |
if (PyArg_ParseTuple (args, "O&dd:PDFSurface.__new__", |
| 1226 |
Pycairo_writer_converter, &file, |
| 1227 |
&width_in_points, &height_in_points)) { |
| 1228 |
Py_BEGIN_ALLOW_THREADS; |
| 1229 |
sfc = cairo_pdf_surface_create_for_stream ( |
| 1230 |
_write_func, file, width_in_points, height_in_points); |
| 1231 |
Py_END_ALLOW_THREADS; |
| 1232 |
return _surface_create_with_object (sfc, file); |
| 1233 |
} else { |
| 1234 |
PyErr_Clear (); |
| 1235 |
PyErr_SetString(PyExc_TypeError, |
| 1236 |
"PDFSurface argument 1 must be " |
| 1237 |
"None, or a filename (str), or " |
| 1238 |
"a file object, or an object that has a " |
| 1239 |
"\"write\" method (like BytesIO) taking bytes."); |
| 1240 |
return NULL; |
| 1241 |
} |
| 1242 |
} |
| 1243 |
} |
| 1244 |
|
| 1245 |
static PyObject * |
| 1246 |
pdf_surface_set_size (PycairoPDFSurface *o, PyObject *args) { |
| 1247 |
double width_in_points, height_in_points; |
| 1248 |
|
| 1249 |
if (!PyArg_ParseTuple(args, "dd:PDFSurface.set_size", &width_in_points, |
| 1250 |
&height_in_points)) |
| 1251 |
return NULL; |
| 1252 |
cairo_pdf_surface_set_size (o->surface, width_in_points, |
| 1253 |
height_in_points); |
| 1254 |
Py_RETURN_NONE; |
| 1255 |
} |
| 1256 |
|
| 1257 |
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 17, 6) |
| 1258 |
static PyObject * |
| 1259 |
pdf_surface_set_custom_metadata (PycairoPDFSurface *o, PyObject *args) { |
| 1260 |
const char *name; |
| 1261 |
const char *value; |
| 1262 |
|
| 1263 |
if (!PyArg_ParseTuple (args, "sz:PDFSurface.set_custom_metadata", |
| 1264 |
&name, &value)) |
| 1265 |
return NULL; |
| 1266 |
|
| 1267 |
Py_BEGIN_ALLOW_THREADS; |
| 1268 |
cairo_pdf_surface_set_custom_metadata (o->surface, name, value); |
| 1269 |
Py_END_ALLOW_THREADS; |
| 1270 |
|
| 1271 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (o->surface); |
| 1272 |
Py_RETURN_NONE; |
| 1273 |
} |
| 1274 |
#endif |
| 1275 |
|
| 1276 |
static PyObject * |
| 1277 |
pdf_get_versions (PyObject *self, PyObject *ignored) { |
| 1278 |
PyObject *list, *num; |
| 1279 |
const cairo_pdf_version_t *versions; |
| 1280 |
int i, num_versions; |
| 1281 |
|
| 1282 |
Py_BEGIN_ALLOW_THREADS; |
| 1283 |
cairo_pdf_get_versions (&versions, &num_versions); |
| 1284 |
Py_END_ALLOW_THREADS; |
| 1285 |
|
| 1286 |
list = PyList_New (num_versions); |
| 1287 |
if (list == NULL) |
| 1288 |
return NULL; |
| 1289 |
|
| 1290 |
for (i=0; i < num_versions; i++) { |
| 1291 |
num = CREATE_INT_ENUM (PDFVersion, versions[i]); |
| 1292 |
if (num == NULL) { |
| 1293 |
Py_DECREF (list); |
| 1294 |
return NULL; |
| 1295 |
} |
| 1296 |
PyList_SET_ITEM (list, i, num); |
| 1297 |
} |
| 1298 |
|
| 1299 |
return list; |
| 1300 |
} |
| 1301 |
|
| 1302 |
static PyObject * |
| 1303 |
pdf_version_to_string (PyObject *self, PyObject *args) { |
| 1304 |
cairo_pdf_version_t version; |
| 1305 |
int version_arg; |
| 1306 |
const char *version_string; |
| 1307 |
|
| 1308 |
if (!PyArg_ParseTuple (args, "i:PDFSurface.version_to_string", &version_arg)) |
| 1309 |
return NULL; |
| 1310 |
|
| 1311 |
version = (cairo_pdf_version_t)version_arg; |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
if (version >= 0) { |
| 1320 |
Py_BEGIN_ALLOW_THREADS; |
| 1321 |
version_string = cairo_pdf_version_to_string (version); |
| 1322 |
Py_END_ALLOW_THREADS; |
| 1323 |
} else { |
| 1324 |
version_string = NULL; |
| 1325 |
} |
| 1326 |
|
| 1327 |
if (version_string == NULL) { |
| 1328 |
PyErr_SetString (PyExc_ValueError, "invalid version"); |
| 1329 |
return NULL; |
| 1330 |
} |
| 1331 |
|
| 1332 |
return PyUnicode_FromString (version_string); |
| 1333 |
} |
| 1334 |
|
| 1335 |
static PyObject * |
| 1336 |
pdf_surface_restrict_to_version (PycairoPDFSurface *o, PyObject *args) { |
| 1337 |
cairo_pdf_version_t version; |
| 1338 |
int version_arg; |
| 1339 |
|
| 1340 |
if (!PyArg_ParseTuple (args, "i:PDFSurface.restrict_to_version", |
| 1341 |
&version_arg)) |
| 1342 |
return NULL; |
| 1343 |
|
| 1344 |
version = (cairo_pdf_version_t)version_arg; |
| 1345 |
|
| 1346 |
Py_BEGIN_ALLOW_THREADS; |
| 1347 |
cairo_pdf_surface_restrict_to_version (o->surface, version); |
| 1348 |
Py_END_ALLOW_THREADS; |
| 1349 |
|
| 1350 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (o->surface); |
| 1351 |
Py_RETURN_NONE; |
| 1352 |
} |
| 1353 |
|
| 1354 |
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 15, 10) |
| 1355 |
static PyObject * |
| 1356 |
pdf_surface_set_page_label (PycairoPDFSurface *o, PyObject *args) { |
| 1357 |
const char *utf8; |
| 1358 |
|
| 1359 |
if (!PyArg_ParseTuple (args, "es:PDFSurface.set_page_label", |
| 1360 |
"utf-8", &utf8)) |
| 1361 |
return NULL; |
| 1362 |
|
| 1363 |
Py_BEGIN_ALLOW_THREADS; |
| 1364 |
cairo_pdf_surface_set_page_label (o->surface, utf8); |
| 1365 |
Py_END_ALLOW_THREADS; |
| 1366 |
|
| 1367 |
PyMem_Free((void *)utf8); |
| 1368 |
|
| 1369 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (o->surface); |
| 1370 |
Py_RETURN_NONE; |
| 1371 |
} |
| 1372 |
|
| 1373 |
static PyObject * |
| 1374 |
pdf_surface_set_metadata (PycairoPDFSurface *o, PyObject *args) { |
| 1375 |
const char *utf8; |
| 1376 |
cairo_pdf_metadata_t metadata; |
| 1377 |
int metadata_arg; |
| 1378 |
|
| 1379 |
if (!PyArg_ParseTuple (args, "ies:PDFSurface.set_metadata", |
| 1380 |
&metadata_arg, "utf-8", &utf8)) |
| 1381 |
return NULL; |
| 1382 |
|
| 1383 |
metadata = (cairo_pdf_metadata_t)metadata_arg; |
| 1384 |
|
| 1385 |
Py_BEGIN_ALLOW_THREADS; |
| 1386 |
cairo_pdf_surface_set_metadata (o->surface, metadata, utf8); |
| 1387 |
Py_END_ALLOW_THREADS; |
| 1388 |
|
| 1389 |
PyMem_Free((void *)utf8); |
| 1390 |
|
| 1391 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (o->surface); |
| 1392 |
Py_RETURN_NONE; |
| 1393 |
} |
| 1394 |
|
| 1395 |
static PyObject * |
| 1396 |
pdf_surface_set_thumbnail_size (PycairoPDFSurface *o, PyObject *args) { |
| 1397 |
int width, height; |
| 1398 |
|
| 1399 |
if (!PyArg_ParseTuple (args, "ii:PDFSurface.set_thumbnail_size", |
| 1400 |
&width, &height)) |
| 1401 |
return NULL; |
| 1402 |
|
| 1403 |
Py_BEGIN_ALLOW_THREADS; |
| 1404 |
cairo_pdf_surface_set_thumbnail_size (o->surface, width, height); |
| 1405 |
Py_END_ALLOW_THREADS; |
| 1406 |
|
| 1407 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (o->surface); |
| 1408 |
Py_RETURN_NONE; |
| 1409 |
} |
| 1410 |
|
| 1411 |
static PyObject * |
| 1412 |
pdf_surface_add_outline (PycairoPDFSurface *o, PyObject *args) { |
| 1413 |
|
| 1414 |
int parent_id, added_id; |
| 1415 |
const char *utf8; |
| 1416 |
const char *link_attribs; |
| 1417 |
cairo_pdf_outline_flags_t flags; |
| 1418 |
int flags_arg; |
| 1419 |
|
| 1420 |
if (!PyArg_ParseTuple (args, "iesesi:PDFSurface.add_outline", |
| 1421 |
&parent_id, "utf-8", &utf8, "utf-8", &link_attribs, &flags_arg)) |
| 1422 |
return NULL; |
| 1423 |
|
| 1424 |
flags = (cairo_pdf_outline_flags_t)flags_arg; |
| 1425 |
|
| 1426 |
Py_BEGIN_ALLOW_THREADS; |
| 1427 |
added_id = cairo_pdf_surface_add_outline (o->surface, parent_id, utf8, link_attribs, flags); |
| 1428 |
Py_END_ALLOW_THREADS; |
| 1429 |
|
| 1430 |
PyMem_Free((void *)utf8); |
| 1431 |
PyMem_Free((void *)link_attribs); |
| 1432 |
|
| 1433 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (o->surface); |
| 1434 |
|
| 1435 |
return PyLong_FromLong (added_id); |
| 1436 |
} |
| 1437 |
#endif |
| 1438 |
|
| 1439 |
static PyMethodDef pdf_surface_methods[] = { |
| 1440 |
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 17, 6) |
| 1441 |
{"set_custom_metadata", (PyCFunction)pdf_surface_set_custom_metadata, METH_VARARGS}, |
| 1442 |
#endif |
| 1443 |
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 15, 10) |
| 1444 |
{"set_page_label", (PyCFunction)pdf_surface_set_page_label, METH_VARARGS}, |
| 1445 |
{"set_metadata", (PyCFunction)pdf_surface_set_metadata, METH_VARARGS}, |
| 1446 |
{"set_thumbnail_size", (PyCFunction)pdf_surface_set_thumbnail_size, METH_VARARGS}, |
| 1447 |
{"add_outline", (PyCFunction)pdf_surface_add_outline, METH_VARARGS}, |
| 1448 |
#endif |
| 1449 |
{"set_size", (PyCFunction)pdf_surface_set_size, METH_VARARGS}, |
| 1450 |
{"get_versions", (PyCFunction)pdf_get_versions, METH_NOARGS | METH_STATIC}, |
| 1451 |
{"version_to_string", (PyCFunction)pdf_version_to_string, |
| 1452 |
METH_VARARGS | METH_STATIC}, |
| 1453 |
{"restrict_to_version", (PyCFunction)pdf_surface_restrict_to_version, |
| 1454 |
METH_VARARGS}, |
| 1455 |
{NULL, NULL, 0, NULL}, |
| 1456 |
}; |
| 1457 |
|
| 1458 |
PyTypeObject PycairoPDFSurface_Type = { |
| 1459 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1460 |
"cairo.PDFSurface", |
| 1461 |
sizeof(PycairoPDFSurface), |
| 1462 |
0, |
| 1463 |
0, |
| 1464 |
0, |
| 1465 |
0, |
| 1466 |
0, |
| 1467 |
0, |
| 1468 |
0, |
| 1469 |
0, |
| 1470 |
0, |
| 1471 |
0, |
| 1472 |
0, |
| 1473 |
0, |
| 1474 |
0, |
| 1475 |
0, |
| 1476 |
0, |
| 1477 |
0, |
| 1478 |
Py_TPFLAGS_DEFAULT, |
| 1479 |
0, |
| 1480 |
0, |
| 1481 |
0, |
| 1482 |
0, |
| 1483 |
0, |
| 1484 |
0, |
| 1485 |
0, |
| 1486 |
pdf_surface_methods, |
| 1487 |
0, |
| 1488 |
0, |
| 1489 |
&PycairoSurface_Type, |
| 1490 |
0, |
| 1491 |
0, |
| 1492 |
0, |
| 1493 |
0, |
| 1494 |
0, |
| 1495 |
0, |
| 1496 |
(newfunc)pdf_surface_new, |
| 1497 |
0, |
| 1498 |
0, |
| 1499 |
0, |
| 1500 |
}; |
| 1501 |
#endif |
| 1502 |
|
| 1503 |
#ifdef CAIRO_HAS_SCRIPT_SURFACE |
| 1504 |
#include <cairo-script.h> |
| 1505 |
|
| 1506 |
typedef PycairoSurface PycairoScriptSurface; |
| 1507 |
|
| 1508 |
static PyObject * |
| 1509 |
script_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 1510 |
cairo_content_t content; |
| 1511 |
int content_arg; |
| 1512 |
double width, height; |
| 1513 |
PyObject *pydevice; |
| 1514 |
|
| 1515 |
if (!PyArg_ParseTuple (args, "O!idd:ScriptSurface.__new__", |
| 1516 |
&PycairoScriptDevice_Type, &pydevice, &content_arg, &width, &height)) |
| 1517 |
return NULL; |
| 1518 |
|
| 1519 |
content = (cairo_content_t)content_arg; |
| 1520 |
|
| 1521 |
return PycairoSurface_FromSurface ( |
| 1522 |
cairo_script_surface_create ( |
| 1523 |
((PycairoDevice*)pydevice)->device, content, width, height), |
| 1524 |
NULL); |
| 1525 |
} |
| 1526 |
|
| 1527 |
static PyObject * |
| 1528 |
script_surface_create_for_target (PyTypeObject *type, PyObject *args) { |
| 1529 |
PyObject *pydevice, *target; |
| 1530 |
|
| 1531 |
if (!PyArg_ParseTuple (args, "O!O!:ScriptSurface.create_for_target", |
| 1532 |
&PycairoScriptDevice_Type, &pydevice, &PycairoSurface_Type, &target)) |
| 1533 |
return NULL; |
| 1534 |
|
| 1535 |
return PycairoSurface_FromSurface ( |
| 1536 |
cairo_script_surface_create_for_target ( |
| 1537 |
((PycairoDevice*)pydevice)->device, ((PycairoSurface*)target)->surface), |
| 1538 |
NULL); |
| 1539 |
} |
| 1540 |
|
| 1541 |
static PyMethodDef script_surface_methods[] = { |
| 1542 |
{"create_for_target", |
| 1543 |
(PyCFunction)script_surface_create_for_target, METH_VARARGS | METH_CLASS}, |
| 1544 |
{NULL, NULL, 0, NULL}, |
| 1545 |
}; |
| 1546 |
|
| 1547 |
PyTypeObject PycairoScriptSurface_Type = { |
| 1548 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1549 |
"cairo.ScriptSurface", |
| 1550 |
sizeof(PycairoScriptSurface), |
| 1551 |
0, |
| 1552 |
0, |
| 1553 |
0, |
| 1554 |
0, |
| 1555 |
0, |
| 1556 |
0, |
| 1557 |
0, |
| 1558 |
0, |
| 1559 |
0, |
| 1560 |
0, |
| 1561 |
0, |
| 1562 |
0, |
| 1563 |
0, |
| 1564 |
0, |
| 1565 |
0, |
| 1566 |
0, |
| 1567 |
Py_TPFLAGS_DEFAULT, |
| 1568 |
0, |
| 1569 |
0, |
| 1570 |
0, |
| 1571 |
0, |
| 1572 |
0, |
| 1573 |
0, |
| 1574 |
0, |
| 1575 |
script_surface_methods, |
| 1576 |
0, |
| 1577 |
0, |
| 1578 |
&PycairoSurface_Type, |
| 1579 |
0, |
| 1580 |
0, |
| 1581 |
0, |
| 1582 |
0, |
| 1583 |
0, |
| 1584 |
0, |
| 1585 |
(newfunc)script_surface_new, |
| 1586 |
0, |
| 1587 |
0, |
| 1588 |
0, |
| 1589 |
}; |
| 1590 |
|
| 1591 |
#endif |
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
#ifdef CAIRO_HAS_PS_SURFACE |
| 1596 |
#include <cairo-ps.h> |
| 1597 |
|
| 1598 |
static PyObject * |
| 1599 |
ps_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 1600 |
double width_in_points, height_in_points; |
| 1601 |
PyObject *file; |
| 1602 |
cairo_surface_t *sfc; |
| 1603 |
char *name; |
| 1604 |
|
| 1605 |
if (!PyArg_ParseTuple (args, "Odd:PSSurface.__new__", |
| 1606 |
&file, &width_in_points, &height_in_points)) |
| 1607 |
return NULL; |
| 1608 |
|
| 1609 |
if (Pycairo_is_fspath (file) || file == Py_None) { |
| 1610 |
if (!PyArg_ParseTuple (args, "O&dd:PSSurface.__new__", |
| 1611 |
Pycairo_fspath_none_converter, &name, |
| 1612 |
&width_in_points, &height_in_points)) |
| 1613 |
return NULL; |
| 1614 |
|
| 1615 |
Py_BEGIN_ALLOW_THREADS; |
| 1616 |
sfc = cairo_ps_surface_create (name, width_in_points, height_in_points); |
| 1617 |
Py_END_ALLOW_THREADS; |
| 1618 |
PyMem_Free(name); |
| 1619 |
return PycairoSurface_FromSurface (sfc, NULL); |
| 1620 |
} else { |
| 1621 |
if (PyArg_ParseTuple (args, "O&dd:PSSurface.__new__", |
| 1622 |
Pycairo_writer_converter, &file, |
| 1623 |
&width_in_points, &height_in_points)) { |
| 1624 |
Py_BEGIN_ALLOW_THREADS; |
| 1625 |
sfc = cairo_ps_surface_create_for_stream ( |
| 1626 |
_write_func, file, width_in_points, height_in_points); |
| 1627 |
Py_END_ALLOW_THREADS; |
| 1628 |
return _surface_create_with_object (sfc, file); |
| 1629 |
} else { |
| 1630 |
PyErr_Clear (); |
| 1631 |
PyErr_SetString(PyExc_TypeError, |
| 1632 |
"PSSurface argument 1 must be " |
| 1633 |
"None, or a filename (str), or " |
| 1634 |
"a file object, or an object that has a " |
| 1635 |
"\"write\" method (like BytesIO) taking bytes."); |
| 1636 |
return NULL; |
| 1637 |
} |
| 1638 |
} |
| 1639 |
} |
| 1640 |
|
| 1641 |
static PyObject * |
| 1642 |
ps_surface_dsc_begin_page_setup (PycairoPSSurface *o, PyObject *ignored) { |
| 1643 |
cairo_ps_surface_dsc_begin_page_setup (o->surface); |
| 1644 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 1645 |
Py_RETURN_NONE; |
| 1646 |
} |
| 1647 |
|
| 1648 |
static PyObject * |
| 1649 |
ps_surface_dsc_begin_setup (PycairoPSSurface *o, PyObject *ignored) { |
| 1650 |
cairo_ps_surface_dsc_begin_setup (o->surface); |
| 1651 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 1652 |
Py_RETURN_NONE; |
| 1653 |
} |
| 1654 |
|
| 1655 |
static PyObject * |
| 1656 |
ps_surface_dsc_comment (PycairoPSSurface *o, PyObject *args) { |
| 1657 |
const char *comment; |
| 1658 |
if (!PyArg_ParseTuple(args, "s:PSSurface.dsc_comment", &comment)) |
| 1659 |
return NULL; |
| 1660 |
cairo_ps_surface_dsc_comment (o->surface, comment); |
| 1661 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 1662 |
Py_RETURN_NONE; |
| 1663 |
} |
| 1664 |
|
| 1665 |
static PyObject * |
| 1666 |
ps_surface_get_eps (PycairoPSSurface *o, PyObject *ignored) { |
| 1667 |
PyObject *eps = cairo_ps_surface_get_eps (o->surface) ? Py_True : Py_False; |
| 1668 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 1669 |
Py_INCREF(eps); |
| 1670 |
return eps; |
| 1671 |
} |
| 1672 |
|
| 1673 |
|
| 1674 |
static PyObject * |
| 1675 |
ps_level_to_string (PyObject *self, PyObject *args) { |
| 1676 |
cairo_ps_level_t level; |
| 1677 |
int level_arg; |
| 1678 |
const char *s; |
| 1679 |
|
| 1680 |
if (!PyArg_ParseTuple (args, "i:PSSurface.level_to_string", &level_arg)) |
| 1681 |
return NULL; |
| 1682 |
|
| 1683 |
level = (cairo_ps_level_t)level_arg; |
| 1684 |
|
| 1685 |
s = cairo_ps_level_to_string (level); |
| 1686 |
if (s == NULL){ |
| 1687 |
PyErr_SetString(PyExc_ValueError, "level_to_string: " |
| 1688 |
"invalid level argument"); |
| 1689 |
return NULL; |
| 1690 |
} |
| 1691 |
return PyUnicode_FromString (s); |
| 1692 |
} |
| 1693 |
|
| 1694 |
static PyObject * |
| 1695 |
ps_surface_restrict_to_level (PycairoPSSurface *o, PyObject *args) { |
| 1696 |
cairo_ps_level_t level; |
| 1697 |
int level_arg; |
| 1698 |
|
| 1699 |
if (!PyArg_ParseTuple (args, "i:PSSurface.restrict_to_level", &level_arg)) |
| 1700 |
return NULL; |
| 1701 |
|
| 1702 |
level = (cairo_ps_level_t)level_arg; |
| 1703 |
|
| 1704 |
cairo_ps_surface_restrict_to_level (o->surface, level); |
| 1705 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 1706 |
Py_RETURN_NONE; |
| 1707 |
} |
| 1708 |
|
| 1709 |
static PyObject * |
| 1710 |
ps_surface_set_eps (PycairoPSSurface *o, PyObject *args) { |
| 1711 |
PyObject *py_eps; |
| 1712 |
if (!PyArg_ParseTuple(args, "O!:PSSurface.set_eps", |
| 1713 |
&PyBool_Type, &py_eps)) |
| 1714 |
return NULL; |
| 1715 |
cairo_ps_surface_set_eps (o->surface, (py_eps == Py_True)); |
| 1716 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR(o->surface); |
| 1717 |
Py_RETURN_NONE; |
| 1718 |
} |
| 1719 |
|
| 1720 |
static PyObject * |
| 1721 |
ps_surface_set_size (PycairoPSSurface *o, PyObject *args) { |
| 1722 |
double width_in_points, height_in_points; |
| 1723 |
|
| 1724 |
if (!PyArg_ParseTuple(args, "dd:PSSurface.set_size", |
| 1725 |
&width_in_points, &height_in_points)) |
| 1726 |
return NULL; |
| 1727 |
cairo_ps_surface_set_size (o->surface, width_in_points, height_in_points); |
| 1728 |
Py_RETURN_NONE; |
| 1729 |
} |
| 1730 |
|
| 1731 |
static PyObject * |
| 1732 |
ps_get_levels (PyObject *self, PyObject *ignored) { |
| 1733 |
PyObject *list, *num; |
| 1734 |
const cairo_ps_level_t *levels; |
| 1735 |
int i, num_levels; |
| 1736 |
|
| 1737 |
Py_BEGIN_ALLOW_THREADS; |
| 1738 |
cairo_ps_get_levels (&levels, &num_levels); |
| 1739 |
Py_END_ALLOW_THREADS; |
| 1740 |
|
| 1741 |
list = PyList_New (num_levels); |
| 1742 |
if (list == NULL) |
| 1743 |
return NULL; |
| 1744 |
|
| 1745 |
for (i=0; i < num_levels; i++) { |
| 1746 |
num = CREATE_INT_ENUM (PSLevel, levels[i]); |
| 1747 |
if (num == NULL) { |
| 1748 |
Py_DECREF (list); |
| 1749 |
return NULL; |
| 1750 |
} |
| 1751 |
PyList_SET_ITEM (list, i, num); |
| 1752 |
} |
| 1753 |
|
| 1754 |
return list; |
| 1755 |
} |
| 1756 |
|
| 1757 |
static PyMethodDef ps_surface_methods[] = { |
| 1758 |
{"dsc_begin_page_setup", |
| 1759 |
(PyCFunction)ps_surface_dsc_begin_page_setup, METH_NOARGS }, |
| 1760 |
{"dsc_begin_setup", (PyCFunction)ps_surface_dsc_begin_setup, METH_NOARGS }, |
| 1761 |
{"dsc_comment", (PyCFunction)ps_surface_dsc_comment, METH_VARARGS }, |
| 1762 |
{"get_eps", (PyCFunction)ps_surface_get_eps, METH_NOARGS }, |
| 1763 |
{"ps_level_to_string", (PyCFunction)ps_level_to_string, |
| 1764 |
METH_VARARGS | METH_STATIC}, |
| 1765 |
{"level_to_string", (PyCFunction)ps_level_to_string, |
| 1766 |
METH_VARARGS | METH_STATIC}, |
| 1767 |
{"restrict_to_level", (PyCFunction)ps_surface_restrict_to_level, |
| 1768 |
METH_VARARGS }, |
| 1769 |
{"set_eps", (PyCFunction)ps_surface_set_eps, METH_VARARGS }, |
| 1770 |
{"set_size", (PyCFunction)ps_surface_set_size, METH_VARARGS }, |
| 1771 |
{"get_levels", (PyCFunction)ps_get_levels, METH_NOARGS | METH_STATIC}, |
| 1772 |
{NULL, NULL, 0, NULL}, |
| 1773 |
}; |
| 1774 |
|
| 1775 |
PyTypeObject PycairoPSSurface_Type = { |
| 1776 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1777 |
"cairo.PSSurface", |
| 1778 |
sizeof(PycairoPSSurface), |
| 1779 |
0, |
| 1780 |
0, |
| 1781 |
0, |
| 1782 |
0, |
| 1783 |
0, |
| 1784 |
0, |
| 1785 |
0, |
| 1786 |
0, |
| 1787 |
0, |
| 1788 |
0, |
| 1789 |
0, |
| 1790 |
0, |
| 1791 |
0, |
| 1792 |
0, |
| 1793 |
0, |
| 1794 |
0, |
| 1795 |
Py_TPFLAGS_DEFAULT, |
| 1796 |
0, |
| 1797 |
0, |
| 1798 |
0, |
| 1799 |
0, |
| 1800 |
0, |
| 1801 |
0, |
| 1802 |
0, |
| 1803 |
ps_surface_methods, |
| 1804 |
0, |
| 1805 |
0, |
| 1806 |
&PycairoSurface_Type, |
| 1807 |
0, |
| 1808 |
0, |
| 1809 |
0, |
| 1810 |
0, |
| 1811 |
0, |
| 1812 |
0, |
| 1813 |
(newfunc)ps_surface_new, |
| 1814 |
0, |
| 1815 |
0, |
| 1816 |
0, |
| 1817 |
}; |
| 1818 |
#endif |
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
#ifdef CAIRO_HAS_RECORDING_SURFACE |
| 1823 |
|
| 1824 |
static PyObject * |
| 1825 |
recording_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 1826 |
cairo_content_t content; |
| 1827 |
cairo_rectangle_t extents, *extents_ptr; |
| 1828 |
cairo_surface_t *sfc; |
| 1829 |
int content_arg; |
| 1830 |
PyObject *extents_tuple; |
| 1831 |
|
| 1832 |
if (!PyArg_ParseTuple (args, "iO:RecordingSurface.__new__", |
| 1833 |
&content_arg, &extents_tuple)) |
| 1834 |
return NULL; |
| 1835 |
|
| 1836 |
content = (cairo_content_t)content_arg; |
| 1837 |
|
| 1838 |
if (extents_tuple == Py_None) { |
| 1839 |
extents_ptr = NULL; |
| 1840 |
} else { |
| 1841 |
if (!PyArg_ParseTuple(extents_tuple, "dddd", &extents.x, &extents.y, |
| 1842 |
&extents.width, &extents.height)) { |
| 1843 |
PyErr_SetString(PyExc_TypeError, |
| 1844 |
"RecordingSurface() argument 2 must be a " |
| 1845 |
"4-tuple of float"); |
| 1846 |
return NULL; |
| 1847 |
} |
| 1848 |
extents_ptr = &extents; |
| 1849 |
} |
| 1850 |
|
| 1851 |
Py_BEGIN_ALLOW_THREADS; |
| 1852 |
sfc = cairo_recording_surface_create (content, extents_ptr); |
| 1853 |
Py_END_ALLOW_THREADS; |
| 1854 |
return PycairoSurface_FromSurface (sfc, NULL); |
| 1855 |
} |
| 1856 |
|
| 1857 |
static PyObject * |
| 1858 |
recording_surface_ink_extents (PycairoRecordingSurface *o, PyObject *ignored) { |
| 1859 |
double x0, y0, width, height; |
| 1860 |
|
| 1861 |
cairo_recording_surface_ink_extents(o->surface, &x0, &y0, &width, &height); |
| 1862 |
|
| 1863 |
return Py_BuildValue("(dddd)", x0, y0, width, height); |
| 1864 |
} |
| 1865 |
|
| 1866 |
static PyObject * |
| 1867 |
recording_surface_get_extents (PycairoRecordingSurface *o, PyObject *ignored) { |
| 1868 |
cairo_rectangle_t extents; |
| 1869 |
cairo_bool_t result; |
| 1870 |
PyObject *rect, *args; |
| 1871 |
|
| 1872 |
Py_BEGIN_ALLOW_THREADS; |
| 1873 |
result = cairo_recording_surface_get_extents (o->surface, &extents); |
| 1874 |
Py_END_ALLOW_THREADS; |
| 1875 |
|
| 1876 |
if (!result) { |
| 1877 |
Py_RETURN_NONE; |
| 1878 |
} |
| 1879 |
|
| 1880 |
args = Py_BuildValue( |
| 1881 |
"(dddd)", extents.x, extents.y, extents.width, extents.height); |
| 1882 |
if (args == NULL) |
| 1883 |
return NULL; |
| 1884 |
rect = PyObject_Call((PyObject *)&PycairoRectangle_Type, args, NULL); |
| 1885 |
Py_DECREF (args); |
| 1886 |
return rect; |
| 1887 |
} |
| 1888 |
|
| 1889 |
static PyMethodDef recording_surface_methods[] = { |
| 1890 |
{"ink_extents", (PyCFunction)recording_surface_ink_extents, METH_NOARGS }, |
| 1891 |
{"get_extents", (PyCFunction)recording_surface_get_extents, METH_NOARGS }, |
| 1892 |
{NULL, NULL, 0, NULL}, |
| 1893 |
}; |
| 1894 |
|
| 1895 |
PyTypeObject PycairoRecordingSurface_Type = { |
| 1896 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 1897 |
"cairo.RecordingSurface", |
| 1898 |
sizeof(PycairoRecordingSurface), |
| 1899 |
0, |
| 1900 |
0, |
| 1901 |
0, |
| 1902 |
0, |
| 1903 |
0, |
| 1904 |
0, |
| 1905 |
0, |
| 1906 |
0, |
| 1907 |
0, |
| 1908 |
0, |
| 1909 |
0, |
| 1910 |
0, |
| 1911 |
0, |
| 1912 |
0, |
| 1913 |
0, |
| 1914 |
0, |
| 1915 |
Py_TPFLAGS_DEFAULT, |
| 1916 |
0, |
| 1917 |
0, |
| 1918 |
0, |
| 1919 |
0, |
| 1920 |
0, |
| 1921 |
0, |
| 1922 |
0, |
| 1923 |
recording_surface_methods, |
| 1924 |
0, |
| 1925 |
0, |
| 1926 |
&PycairoSurface_Type, |
| 1927 |
0, |
| 1928 |
0, |
| 1929 |
0, |
| 1930 |
0, |
| 1931 |
0, |
| 1932 |
0, |
| 1933 |
(newfunc)recording_surface_new, |
| 1934 |
0, |
| 1935 |
0, |
| 1936 |
0, |
| 1937 |
}; |
| 1938 |
#endif |
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
#ifdef CAIRO_HAS_SVG_SURFACE |
| 1943 |
#include <cairo-svg.h> |
| 1944 |
|
| 1945 |
static PyObject * |
| 1946 |
svg_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 1947 |
double width_in_points, height_in_points; |
| 1948 |
PyObject *file; |
| 1949 |
cairo_surface_t *sfc; |
| 1950 |
char *name; |
| 1951 |
|
| 1952 |
if (!PyArg_ParseTuple (args, "Odd:SVGSurface.__new__", |
| 1953 |
&file, &width_in_points, &height_in_points)) |
| 1954 |
return NULL; |
| 1955 |
|
| 1956 |
if (Pycairo_is_fspath (file) || file == Py_None) { |
| 1957 |
if (!PyArg_ParseTuple (args, "O&dd:SVGSurface.__new__", |
| 1958 |
Pycairo_fspath_none_converter, &name, |
| 1959 |
&width_in_points, &height_in_points)) |
| 1960 |
return NULL; |
| 1961 |
|
| 1962 |
Py_BEGIN_ALLOW_THREADS; |
| 1963 |
sfc = cairo_svg_surface_create (name, width_in_points, height_in_points); |
| 1964 |
Py_END_ALLOW_THREADS; |
| 1965 |
PyMem_Free(name); |
| 1966 |
return PycairoSurface_FromSurface (sfc, NULL); |
| 1967 |
} else { |
| 1968 |
if (PyArg_ParseTuple (args, "O&dd:SVGSurface.__new__", |
| 1969 |
Pycairo_writer_converter, &file, |
| 1970 |
&width_in_points, &height_in_points)) { |
| 1971 |
Py_BEGIN_ALLOW_THREADS; |
| 1972 |
sfc = cairo_svg_surface_create_for_stream ( |
| 1973 |
_write_func, file, width_in_points, height_in_points); |
| 1974 |
Py_END_ALLOW_THREADS; |
| 1975 |
return _surface_create_with_object (sfc, file); |
| 1976 |
} else { |
| 1977 |
PyErr_Clear (); |
| 1978 |
PyErr_SetString(PyExc_TypeError, |
| 1979 |
"SVGSurface argument 1 must be " |
| 1980 |
"None, or a filename (str), or " |
| 1981 |
"a file object, or an object that has a " |
| 1982 |
"\"write\" method (like BytesIO) taking bytes."); |
| 1983 |
return NULL; |
| 1984 |
} |
| 1985 |
} |
| 1986 |
} |
| 1987 |
|
| 1988 |
static PyObject * |
| 1989 |
svg_get_versions (PyObject *self, PyObject *ignored) { |
| 1990 |
PyObject *list, *num; |
| 1991 |
const cairo_svg_version_t *versions; |
| 1992 |
int i, num_versions; |
| 1993 |
|
| 1994 |
Py_BEGIN_ALLOW_THREADS; |
| 1995 |
cairo_svg_get_versions (&versions, &num_versions); |
| 1996 |
Py_END_ALLOW_THREADS; |
| 1997 |
|
| 1998 |
list = PyList_New (num_versions); |
| 1999 |
if (list == NULL) |
| 2000 |
return NULL; |
| 2001 |
|
| 2002 |
for (i=0; i < num_versions; i++) { |
| 2003 |
num = CREATE_INT_ENUM (SVGVersion, versions[i]); |
| 2004 |
if (num == NULL) { |
| 2005 |
Py_DECREF (list); |
| 2006 |
return NULL; |
| 2007 |
} |
| 2008 |
PyList_SET_ITEM (list, i, num); |
| 2009 |
} |
| 2010 |
|
| 2011 |
return list; |
| 2012 |
} |
| 2013 |
|
| 2014 |
static PyObject * |
| 2015 |
svg_version_to_string (PyObject *self, PyObject *args) { |
| 2016 |
cairo_svg_version_t version; |
| 2017 |
int version_arg; |
| 2018 |
const char *version_string; |
| 2019 |
|
| 2020 |
if (!PyArg_ParseTuple (args, "i:SVGSurface.version_to_string", &version_arg)) |
| 2021 |
return NULL; |
| 2022 |
|
| 2023 |
version = (cairo_svg_version_t)version_arg; |
| 2024 |
|
| 2025 |
Py_BEGIN_ALLOW_THREADS; |
| 2026 |
version_string = cairo_svg_version_to_string (version); |
| 2027 |
Py_END_ALLOW_THREADS; |
| 2028 |
|
| 2029 |
if (version_string == NULL) { |
| 2030 |
PyErr_SetString (PyExc_ValueError, "invalid version"); |
| 2031 |
return NULL; |
| 2032 |
} |
| 2033 |
|
| 2034 |
return PyUnicode_FromString (version_string); |
| 2035 |
} |
| 2036 |
|
| 2037 |
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 15, 10) |
| 2038 |
static PyObject * |
| 2039 |
svg_surface_get_document_unit (PycairoSVGSurface *o, PyObject *ignored) { |
| 2040 |
RETURN_INT_ENUM (SVGUnit, cairo_svg_surface_get_document_unit (o->surface)); |
| 2041 |
} |
| 2042 |
|
| 2043 |
static PyObject * |
| 2044 |
svg_surface_set_document_unit (PycairoSVGSurface *o, PyObject *args) { |
| 2045 |
cairo_svg_unit_t unit; |
| 2046 |
int unit_arg; |
| 2047 |
|
| 2048 |
if (!PyArg_ParseTuple (args, "i:SVGSurface.set_document_unit", &unit_arg)) |
| 2049 |
return NULL; |
| 2050 |
|
| 2051 |
unit = (cairo_svg_unit_t)unit_arg; |
| 2052 |
|
| 2053 |
Py_BEGIN_ALLOW_THREADS; |
| 2054 |
cairo_svg_surface_set_document_unit (o->surface, unit); |
| 2055 |
Py_END_ALLOW_THREADS; |
| 2056 |
|
| 2057 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (o->surface); |
| 2058 |
Py_RETURN_NONE; |
| 2059 |
} |
| 2060 |
#endif |
| 2061 |
|
| 2062 |
static PyObject * |
| 2063 |
svg_surface_restrict_to_version (PycairoSVGSurface *o, PyObject *args) { |
| 2064 |
cairo_svg_version_t version; |
| 2065 |
int version_arg; |
| 2066 |
|
| 2067 |
if (!PyArg_ParseTuple (args, "i:SVGSurface.restrict_to_version", |
| 2068 |
&version_arg)) |
| 2069 |
return NULL; |
| 2070 |
|
| 2071 |
version = (cairo_svg_version_t)version_arg; |
| 2072 |
|
| 2073 |
Py_BEGIN_ALLOW_THREADS; |
| 2074 |
cairo_svg_surface_restrict_to_version (o->surface, version); |
| 2075 |
Py_END_ALLOW_THREADS; |
| 2076 |
|
| 2077 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (o->surface); |
| 2078 |
Py_RETURN_NONE; |
| 2079 |
} |
| 2080 |
|
| 2081 |
static PyMethodDef svg_surface_methods[] = { |
| 2082 |
#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 15, 10) |
| 2083 |
{"get_document_unit", (PyCFunction)svg_surface_get_document_unit, |
| 2084 |
METH_NOARGS}, |
| 2085 |
{"set_document_unit", (PyCFunction)svg_surface_set_document_unit, |
| 2086 |
METH_VARARGS}, |
| 2087 |
#endif |
| 2088 |
{"get_versions", (PyCFunction)svg_get_versions, METH_NOARGS | METH_STATIC}, |
| 2089 |
{"version_to_string", (PyCFunction)svg_version_to_string, |
| 2090 |
METH_VARARGS | METH_STATIC}, |
| 2091 |
{"restrict_to_version", (PyCFunction)svg_surface_restrict_to_version, |
| 2092 |
METH_VARARGS}, |
| 2093 |
{NULL, NULL, 0, NULL}, |
| 2094 |
}; |
| 2095 |
|
| 2096 |
PyTypeObject PycairoSVGSurface_Type = { |
| 2097 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 2098 |
"cairo.SVGSurface", |
| 2099 |
sizeof(PycairoSVGSurface), |
| 2100 |
0, |
| 2101 |
0, |
| 2102 |
0, |
| 2103 |
0, |
| 2104 |
0, |
| 2105 |
0, |
| 2106 |
0, |
| 2107 |
0, |
| 2108 |
0, |
| 2109 |
0, |
| 2110 |
0, |
| 2111 |
0, |
| 2112 |
0, |
| 2113 |
0, |
| 2114 |
0, |
| 2115 |
0, |
| 2116 |
Py_TPFLAGS_DEFAULT, |
| 2117 |
0, |
| 2118 |
0, |
| 2119 |
0, |
| 2120 |
0, |
| 2121 |
0, |
| 2122 |
0, |
| 2123 |
0, |
| 2124 |
svg_surface_methods, |
| 2125 |
0, |
| 2126 |
0, |
| 2127 |
&PycairoSurface_Type, |
| 2128 |
0, |
| 2129 |
0, |
| 2130 |
0, |
| 2131 |
0, |
| 2132 |
0, |
| 2133 |
0, |
| 2134 |
(newfunc)svg_surface_new, |
| 2135 |
0, |
| 2136 |
0, |
| 2137 |
0, |
| 2138 |
}; |
| 2139 |
#endif |
| 2140 |
|
| 2141 |
|
| 2142 |
#ifdef CAIRO_HAS_WIN32_SURFACE |
| 2143 |
#include <cairo-win32.h> |
| 2144 |
|
| 2145 |
static int |
| 2146 |
convert_pyobject_to_hdc (PyObject *obj, HDC* addr) { |
| 2147 |
HDC temp = PyLong_AsVoidPtr(obj); |
| 2148 |
if (PyErr_Occurred() != NULL) |
| 2149 |
return 0; |
| 2150 |
*addr = temp; |
| 2151 |
return 1; |
| 2152 |
} |
| 2153 |
|
| 2154 |
|
| 2155 |
static PyObject * |
| 2156 |
win32_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 2157 |
HDC hdc; |
| 2158 |
|
| 2159 |
if (!PyArg_ParseTuple(args, "O&:Win32Surface.__new__", |
| 2160 |
convert_pyobject_to_hdc, &hdc)) |
| 2161 |
return NULL; |
| 2162 |
return PycairoSurface_FromSurface ( |
| 2163 |
cairo_win32_surface_create (hdc), NULL); |
| 2164 |
} |
| 2165 |
|
| 2166 |
static PyMethodDef win32_surface_methods[] = { |
| 2167 |
{NULL, NULL, 0, NULL}, |
| 2168 |
}; |
| 2169 |
|
| 2170 |
PyTypeObject PycairoWin32Surface_Type = { |
| 2171 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 2172 |
"cairo.Win32Surface", |
| 2173 |
sizeof(PycairoWin32Surface), |
| 2174 |
0, |
| 2175 |
0, |
| 2176 |
0, |
| 2177 |
0, |
| 2178 |
0, |
| 2179 |
0, |
| 2180 |
0, |
| 2181 |
0, |
| 2182 |
0, |
| 2183 |
0, |
| 2184 |
0, |
| 2185 |
0, |
| 2186 |
0, |
| 2187 |
0, |
| 2188 |
0, |
| 2189 |
0, |
| 2190 |
Py_TPFLAGS_DEFAULT, |
| 2191 |
0, |
| 2192 |
0, |
| 2193 |
0, |
| 2194 |
0, |
| 2195 |
0, |
| 2196 |
0, |
| 2197 |
0, |
| 2198 |
win32_surface_methods, |
| 2199 |
0, |
| 2200 |
0, |
| 2201 |
&PycairoSurface_Type, |
| 2202 |
0, |
| 2203 |
0, |
| 2204 |
0, |
| 2205 |
0, |
| 2206 |
0, |
| 2207 |
0, |
| 2208 |
(newfunc)win32_surface_new, |
| 2209 |
0, |
| 2210 |
0, |
| 2211 |
0, |
| 2212 |
}; |
| 2213 |
|
| 2214 |
|
| 2215 |
|
| 2216 |
static PyObject * |
| 2217 |
win32_printing_surface_new (PyTypeObject *type, PyObject *args, |
| 2218 |
PyObject *kwds) { |
| 2219 |
HDC hdc; |
| 2220 |
|
| 2221 |
if (!PyArg_ParseTuple(args, "O&:Win32PrintingSurface.__new__", |
| 2222 |
convert_pyobject_to_hdc, &hdc)) |
| 2223 |
return NULL; |
| 2224 |
return PycairoSurface_FromSurface ( |
| 2225 |
cairo_win32_printing_surface_create (hdc), NULL); |
| 2226 |
} |
| 2227 |
|
| 2228 |
static PyMethodDef win32_printing_surface_methods[] = { |
| 2229 |
{NULL, NULL, 0, NULL}, |
| 2230 |
}; |
| 2231 |
|
| 2232 |
PyTypeObject PycairoWin32PrintingSurface_Type = { |
| 2233 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 2234 |
"cairo.Win32PrintingSurface", |
| 2235 |
sizeof(PycairoWin32PrintingSurface), |
| 2236 |
0, |
| 2237 |
0, |
| 2238 |
0, |
| 2239 |
0, |
| 2240 |
0, |
| 2241 |
0, |
| 2242 |
0, |
| 2243 |
0, |
| 2244 |
0, |
| 2245 |
0, |
| 2246 |
0, |
| 2247 |
0, |
| 2248 |
0, |
| 2249 |
0, |
| 2250 |
0, |
| 2251 |
0, |
| 2252 |
Py_TPFLAGS_DEFAULT, |
| 2253 |
0, |
| 2254 |
0, |
| 2255 |
0, |
| 2256 |
0, |
| 2257 |
0, |
| 2258 |
0, |
| 2259 |
0, |
| 2260 |
win32_printing_surface_methods, |
| 2261 |
0, |
| 2262 |
0, |
| 2263 |
&PycairoSurface_Type, |
| 2264 |
0, |
| 2265 |
0, |
| 2266 |
0, |
| 2267 |
0, |
| 2268 |
0, |
| 2269 |
0, |
| 2270 |
(newfunc)win32_printing_surface_new, |
| 2271 |
0, |
| 2272 |
0, |
| 2273 |
0, |
| 2274 |
}; |
| 2275 |
#endif |
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
#ifdef CAIRO_HAS_XCB_SURFACE |
| 2280 |
#include <cairo-xcb.h> |
| 2281 |
|
| 2282 |
static PyObject * |
| 2283 |
xcb_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 2284 |
PyErr_SetString(PyExc_TypeError, |
| 2285 |
"Not supported"); |
| 2286 |
return NULL; |
| 2287 |
} |
| 2288 |
|
| 2289 |
static PyObject * |
| 2290 |
xcb_surface_set_size (PycairoXCBSurface *o, PyObject *args) { |
| 2291 |
int width, height; |
| 2292 |
|
| 2293 |
if (!PyArg_ParseTuple(args, "ii:XCBSurface.set_size", &width, &height)) |
| 2294 |
return NULL; |
| 2295 |
cairo_xcb_surface_set_size (o->surface, width, height); |
| 2296 |
Py_RETURN_NONE; |
| 2297 |
} |
| 2298 |
|
| 2299 |
static PyMethodDef xcb_surface_methods[] = { |
| 2300 |
{"set_size", (PyCFunction)xcb_surface_set_size, METH_VARARGS }, |
| 2301 |
{NULL, NULL, 0, NULL}, |
| 2302 |
}; |
| 2303 |
|
| 2304 |
PyTypeObject PycairoXCBSurface_Type = { |
| 2305 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 2306 |
"cairo.XCBSurface", |
| 2307 |
sizeof(PycairoXCBSurface), |
| 2308 |
0, |
| 2309 |
0, |
| 2310 |
0, |
| 2311 |
0, |
| 2312 |
0, |
| 2313 |
0, |
| 2314 |
0, |
| 2315 |
0, |
| 2316 |
0, |
| 2317 |
0, |
| 2318 |
0, |
| 2319 |
0, |
| 2320 |
0, |
| 2321 |
0, |
| 2322 |
0, |
| 2323 |
0, |
| 2324 |
Py_TPFLAGS_DEFAULT, |
| 2325 |
0, |
| 2326 |
0, |
| 2327 |
0, |
| 2328 |
0, |
| 2329 |
0, |
| 2330 |
0, |
| 2331 |
0, |
| 2332 |
xcb_surface_methods, |
| 2333 |
0, |
| 2334 |
0, |
| 2335 |
&PycairoSurface_Type, |
| 2336 |
0, |
| 2337 |
0, |
| 2338 |
0, |
| 2339 |
0, |
| 2340 |
0, |
| 2341 |
0, |
| 2342 |
(newfunc)xcb_surface_new, |
| 2343 |
0, |
| 2344 |
0, |
| 2345 |
0, |
| 2346 |
}; |
| 2347 |
#endif |
| 2348 |
|
| 2349 |
|
| 2350 |
|
| 2351 |
#ifdef CAIRO_HAS_XLIB_SURFACE |
| 2352 |
#include <cairo-xlib.h> |
| 2353 |
|
| 2354 |
static PyObject * |
| 2355 |
xlib_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 2356 |
PyErr_SetString(PyExc_TypeError, |
| 2357 |
"The XlibSurface type cannot be directly instantiated"); |
| 2358 |
return NULL; |
| 2359 |
} |
| 2360 |
|
| 2361 |
static PyObject * |
| 2362 |
xlib_surface_get_depth (PycairoXlibSurface *o, PyObject *ignored) { |
| 2363 |
return PyLong_FromLong (cairo_xlib_surface_get_depth (o->surface)); |
| 2364 |
} |
| 2365 |
|
| 2366 |
static PyObject * |
| 2367 |
xlib_surface_get_height (PycairoXlibSurface *o, PyObject *ignored) { |
| 2368 |
return PyLong_FromLong (cairo_xlib_surface_get_height (o->surface)); |
| 2369 |
} |
| 2370 |
|
| 2371 |
static PyObject * |
| 2372 |
xlib_surface_get_width (PycairoXlibSurface *o, PyObject *ignored) { |
| 2373 |
return PyLong_FromLong (cairo_xlib_surface_get_width (o->surface)); |
| 2374 |
} |
| 2375 |
|
| 2376 |
static PyMethodDef xlib_surface_methods[] = { |
| 2377 |
{"get_depth", (PyCFunction)xlib_surface_get_depth, METH_NOARGS }, |
| 2378 |
{"get_height",(PyCFunction)xlib_surface_get_height, METH_NOARGS }, |
| 2379 |
{"get_width", (PyCFunction)xlib_surface_get_width, METH_NOARGS }, |
| 2380 |
{NULL, NULL, 0, NULL}, |
| 2381 |
}; |
| 2382 |
|
| 2383 |
PyTypeObject PycairoXlibSurface_Type = { |
| 2384 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 2385 |
"cairo.XlibSurface", |
| 2386 |
sizeof(PycairoXlibSurface), |
| 2387 |
0, |
| 2388 |
0, |
| 2389 |
0, |
| 2390 |
0, |
| 2391 |
0, |
| 2392 |
0, |
| 2393 |
0, |
| 2394 |
0, |
| 2395 |
0, |
| 2396 |
0, |
| 2397 |
0, |
| 2398 |
0, |
| 2399 |
0, |
| 2400 |
0, |
| 2401 |
0, |
| 2402 |
0, |
| 2403 |
Py_TPFLAGS_DEFAULT, |
| 2404 |
0, |
| 2405 |
0, |
| 2406 |
0, |
| 2407 |
0, |
| 2408 |
0, |
| 2409 |
0, |
| 2410 |
0, |
| 2411 |
xlib_surface_methods, |
| 2412 |
0, |
| 2413 |
0, |
| 2414 |
&PycairoSurface_Type, |
| 2415 |
0, |
| 2416 |
0, |
| 2417 |
0, |
| 2418 |
0, |
| 2419 |
0, |
| 2420 |
0, |
| 2421 |
(newfunc)xlib_surface_new, |
| 2422 |
0, |
| 2423 |
0, |
| 2424 |
0, |
| 2425 |
}; |
| 2426 |
#endif |
| 2427 |
|
| 2428 |
#ifdef CAIRO_HAS_TEE_SURFACE |
| 2429 |
#include <cairo-tee.h> |
| 2430 |
|
| 2431 |
static PyObject * |
| 2432 |
tee_surface_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 2433 |
PyObject *pysurface; |
| 2434 |
|
| 2435 |
if (!PyArg_ParseTuple (args, "O!:TeeSurface.__new__", |
| 2436 |
&PycairoSurface_Type, &pysurface)) |
| 2437 |
return NULL; |
| 2438 |
|
| 2439 |
return PycairoSurface_FromSurface ( |
| 2440 |
cairo_tee_surface_create(((PycairoSurface*)pysurface)->surface), |
| 2441 |
NULL); |
| 2442 |
} |
| 2443 |
|
| 2444 |
static PyObject * |
| 2445 |
tee_surface_add (PycairoTeeSurface *obj, PyObject *args) { |
| 2446 |
PyObject *pysurface; |
| 2447 |
|
| 2448 |
if (!PyArg_ParseTuple(args, "O!:TeeSurface.add", |
| 2449 |
&PycairoSurface_Type, &pysurface)) |
| 2450 |
return NULL; |
| 2451 |
|
| 2452 |
cairo_tee_surface_add (obj->surface, ((PycairoSurface*)pysurface)->surface); |
| 2453 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (obj->surface); |
| 2454 |
|
| 2455 |
Py_RETURN_NONE; |
| 2456 |
} |
| 2457 |
|
| 2458 |
static PyObject * |
| 2459 |
tee_surface_remove (PycairoTeeSurface *obj, PyObject *args) { |
| 2460 |
PyObject *pysurface; |
| 2461 |
|
| 2462 |
if (!PyArg_ParseTuple(args, "O!:TeeSurface.remove", |
| 2463 |
&PycairoSurface_Type, &pysurface)) |
| 2464 |
return NULL; |
| 2465 |
|
| 2466 |
cairo_tee_surface_remove (obj->surface, ((PycairoSurface*)pysurface)->surface); |
| 2467 |
RETURN_NULL_IF_CAIRO_SURFACE_ERROR (obj->surface); |
| 2468 |
|
| 2469 |
Py_RETURN_NONE; |
| 2470 |
} |
| 2471 |
|
| 2472 |
static PyObject * |
| 2473 |
tee_surface_index (PycairoTeeSurface *obj, PyObject *args) { |
| 2474 |
unsigned int index; |
| 2475 |
|
| 2476 |
if (!PyArg_ParseTuple(args, "I:TeeSurface.index", &index)) |
| 2477 |
return NULL; |
| 2478 |
|
| 2479 |
return PycairoSurface_FromSurface ( |
| 2480 |
cairo_surface_reference (cairo_tee_surface_index (obj->surface, index)), |
| 2481 |
NULL); |
| 2482 |
} |
| 2483 |
|
| 2484 |
static PyMethodDef tee_surface_methods[] = { |
| 2485 |
{"add", (PyCFunction)tee_surface_add, METH_VARARGS }, |
| 2486 |
{"remove", (PyCFunction)tee_surface_remove, METH_VARARGS }, |
| 2487 |
{"index", (PyCFunction)tee_surface_index, METH_VARARGS }, |
| 2488 |
{NULL, NULL, 0, NULL}, |
| 2489 |
}; |
| 2490 |
|
| 2491 |
PyTypeObject PycairoTeeSurface_Type = { |
| 2492 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 2493 |
"cairo.TeeSurface", |
| 2494 |
sizeof(PycairoTeeSurface), |
| 2495 |
0, |
| 2496 |
0, |
| 2497 |
0, |
| 2498 |
0, |
| 2499 |
0, |
| 2500 |
0, |
| 2501 |
0, |
| 2502 |
0, |
| 2503 |
0, |
| 2504 |
0, |
| 2505 |
0, |
| 2506 |
0, |
| 2507 |
0, |
| 2508 |
0, |
| 2509 |
0, |
| 2510 |
0, |
| 2511 |
Py_TPFLAGS_DEFAULT, |
| 2512 |
0, |
| 2513 |
0, |
| 2514 |
0, |
| 2515 |
0, |
| 2516 |
0, |
| 2517 |
0, |
| 2518 |
0, |
| 2519 |
tee_surface_methods, |
| 2520 |
0, |
| 2521 |
0, |
| 2522 |
&PycairoSurface_Type, |
| 2523 |
0, |
| 2524 |
0, |
| 2525 |
0, |
| 2526 |
0, |
| 2527 |
0, |
| 2528 |
0, |
| 2529 |
(newfunc)tee_surface_new, |
| 2530 |
0, |
| 2531 |
0, |
| 2532 |
0, |
| 2533 |
}; |
| 2534 |
#endif |