Debian Codegraph @

cairo/context.c

raw symbols SCIP
1 /* -*- mode: C; c-basic-offset: 2 -*-
2 *
3 * Pycairo - Python bindings for cairo
4 *
5 * Copyright © 2003 James Henstridge, Steven Chaplin
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
14 *
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
20 *
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
25 *
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
29 */
30
31 #define PY_SSIZE_T_CLEAN
32 #include <Python.h>
33
34 #include "private.h"
35
36 PyObject *
37 PycairoContext_FromContext(cairo_t *ctx, PyTypeObject *type, PyObject *base) {
38 PyObject *o;
39
40 assert (ctx != NULL);
41
42 if (Pycairo_Check_Status (cairo_status (ctx))) {
43 cairo_destroy (ctx);
44 return NULL;
45 }
46
47 o = PycairoContext_Type.tp_alloc (type, 0);
48 if (o) {
49 ((PycairoContext *)o)->ctx = ctx;
50 Py_XINCREF(base);
51 ((PycairoContext *)o)->base = base;
52 } else {
53 cairo_destroy (ctx);
54 }
55 return o;
56 }
57
58 static void
59 pycairo_dealloc(PycairoContext *o) {
60 if (o->ctx) {
61 cairo_destroy(o->ctx);
62 o->ctx = NULL;
63 }
64 Py_CLEAR(o->base);
65 Py_TYPE(o)->tp_free(o);
66 }
67
68 static PyObject *
69 pycairo_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
70 PycairoSurface *s;
71 if (!PyArg_ParseTuple(args, "O!:Context.__new__",
72 &PycairoSurface_Type, &s))
73 return NULL;
74 return PycairoContext_FromContext (cairo_create (s->surface), type, NULL);
75 }
76
77 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 17, 6)
78 static PyObject *
79 pycairo_set_hairline (PycairoContext *o, PyObject *args) {
80 PyObject *py_hairline;
81 if (!PyArg_ParseTuple(args, "O!:Context.set_hairline",
82 &PyBool_Type, &py_hairline))
83 return NULL;
84 cairo_set_hairline (o->ctx, (py_hairline == Py_True));
85 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
86 Py_RETURN_NONE;
87 }
88
89 static PyObject *
90 pycairo_get_hairline (PycairoContext *o, PyObject *ignored) {
91 PyObject *set_hairline = cairo_get_hairline (o->ctx) ? Py_True : Py_False;
92 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
93 Py_INCREF(set_hairline);
94 return set_hairline;
95 }
96 #endif
97
98 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 15, 10)
99 static PyObject *
100 pycairo_tag_begin (PycairoContext *o, PyObject *args) {
101 const char *tag_name;
102 const char *attributes;
103
104 if (!PyArg_ParseTuple (args,
105 "eses:Context.tag_begin", "utf-8",
106 &tag_name, "utf-8", &attributes))
107 return NULL;
108
109 Py_BEGIN_ALLOW_THREADS;
110 cairo_tag_begin (o->ctx, tag_name, attributes);
111 Py_END_ALLOW_THREADS;
112
113 PyMem_Free((void *)tag_name);
114 PyMem_Free((void *)attributes);
115
116 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
117 Py_RETURN_NONE;
118 }
119
120 static PyObject *
121 pycairo_tag_end (PycairoContext *o, PyObject *args) {
122 const char *tag_name;
123
124 if (!PyArg_ParseTuple (args,
125 "es:Context.tag_end", "utf-8",
126 &tag_name))
127 return NULL;
128
129 Py_BEGIN_ALLOW_THREADS;
130 cairo_tag_end (o->ctx, tag_name);
131 Py_END_ALLOW_THREADS;
132
133 PyMem_Free((void *)tag_name);
134
135 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
136 Py_RETURN_NONE;
137 }
138 #endif
139
140 static PyObject *
141 pycairo_append_path (PycairoContext *o, PyObject *args) {
142 PycairoPath *p;
143
144 if (!PyArg_ParseTuple(args, "O!:Context.append_path",
145 &PycairoPath_Type, &p))
146 return NULL;
147
148 Py_BEGIN_ALLOW_THREADS;
149 cairo_append_path (o->ctx, p->path);
150 Py_END_ALLOW_THREADS;
151 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
152 Py_RETURN_NONE;
153 }
154
155 static PyObject *
156 pycairo_arc (PycairoContext *o, PyObject *args) {
157 double xc, yc, radius, angle1, angle2;
158
159 if (!PyArg_ParseTuple (args, "ddddd:Context.arc",
160 &xc, &yc, &radius, &angle1, &angle2))
161 return NULL;
162
163 cairo_arc (o->ctx, xc, yc, radius, angle1, angle2);
164 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
165 Py_RETURN_NONE;
166 }
167
168 static PyObject *
169 pycairo_arc_negative (PycairoContext *o, PyObject *args) {
170 double xc, yc, radius, angle1, angle2;
171
172 if (!PyArg_ParseTuple (args, "ddddd:Context.arc_negative",
173 &xc, &yc, &radius, &angle1, &angle2))
174 return NULL;
175
176 cairo_arc_negative (o->ctx, xc, yc, radius, angle1, angle2);
177 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
178 Py_RETURN_NONE;
179 }
180
181 static PyObject *
182 pycairo_clip (PycairoContext *o, PyObject *ignored) {
183 Py_BEGIN_ALLOW_THREADS;
184 cairo_clip (o->ctx);
185 Py_END_ALLOW_THREADS;
186 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
187 Py_RETURN_NONE;
188 }
189
190 static PyObject *
191 pycairo_clip_extents (PycairoContext *o, PyObject *ignored) {
192 double x1, y1, x2, y2;
193 cairo_clip_extents (o->ctx, &x1, &y1, &x2, &y2);
194 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
195 return Py_BuildValue("(dddd)", x1, y1, x2, y2);
196 }
197
198 static PyObject *
199 pycairo_clip_preserve (PycairoContext *o, PyObject *ignored) {
200 Py_BEGIN_ALLOW_THREADS;
201 cairo_clip_preserve (o->ctx);
202 Py_END_ALLOW_THREADS;
203 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
204 Py_RETURN_NONE;
205 }
206
207 static PyObject *
208 pycairo_in_clip (PycairoContext *o, PyObject *args) {
209 double x, y;
210 cairo_bool_t result;
211
212 if (!PyArg_ParseTuple (args, "dd:Context.in_clip", &x, &y))
213 return NULL;
214
215 Py_BEGIN_ALLOW_THREADS;
216 result = cairo_in_clip (o->ctx, x, y);
217 Py_END_ALLOW_THREADS;
218
219 return PyBool_FromLong(result);
220 }
221
222 static PyObject *
223 pycairo_close_path (PycairoContext *o, PyObject *ignored) {
224 Py_BEGIN_ALLOW_THREADS;
225 cairo_close_path (o->ctx);
226 Py_END_ALLOW_THREADS;
227 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
228 Py_RETURN_NONE;
229 }
230
231 static PyObject *
232 pycairo_copy_clip_rectangle_list (PycairoContext *o, PyObject *ignored) {
233 int i;
234 PyObject *rv = NULL;
235 PyObject *rect = NULL;
236 cairo_rectangle_t *r;
237
238 cairo_rectangle_list_t *rlist = cairo_copy_clip_rectangle_list (o->ctx);
239 if (rlist->status != CAIRO_STATUS_SUCCESS) {
240 Pycairo_Check_Status (rlist->status);
241 goto exit;
242 }
243
244 rv = PyList_New(rlist->num_rectangles);
245 if (rv == NULL)
246 goto exit;
247
248 for (i = 0, r = rlist->rectangles; i < rlist->num_rectangles; i++, r++) {
249 PyObject *py_rect = Py_BuildValue("(dddd)", r->x, r->y,
250 r->width, r->height);
251 if (py_rect == NULL) {
252 Py_CLEAR(rv);
253 goto exit;
254 }
255 rect = PyObject_Call((PyObject *)&PycairoRectangle_Type, py_rect, NULL);
256 Py_DECREF (py_rect);
257 if (rect == NULL) {
258 Py_CLEAR(rv);
259 goto exit;
260 }
261 PyList_SET_ITEM (rv, i, rect);
262 }
263 exit:
264 cairo_rectangle_list_destroy(rlist);
265 return rv;
266 }
267
268 static PyObject *
269 pycairo_copy_page (PycairoContext *o, PyObject *ignored) {
270 Py_BEGIN_ALLOW_THREADS;
271 cairo_copy_page (o->ctx);
272 Py_END_ALLOW_THREADS;
273 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
274 Py_RETURN_NONE;
275 }
276
277 static PyObject *
278 pycairo_copy_path (PycairoContext *o, PyObject *ignored) {
279 cairo_path_t *cp;
280 Py_BEGIN_ALLOW_THREADS;
281 cp = cairo_copy_path (o->ctx);
282 Py_END_ALLOW_THREADS;
283 return PycairoPath_FromPath (cp);
284 }
285
286 static PyObject *
287 pycairo_copy_path_flat (PycairoContext *o, PyObject *ignored) {
288 cairo_path_t *cp;
289 Py_BEGIN_ALLOW_THREADS;
290 cp = cairo_copy_path_flat (o->ctx);
291 Py_END_ALLOW_THREADS;
292 return PycairoPath_FromPath (cp);
293 }
294
295 static PyObject *
296 pycairo_curve_to (PycairoContext *o, PyObject *args) {
297 double x1, y1, x2, y2, x3, y3;
298
299 if (!PyArg_ParseTuple (args, "dddddd:Context.curve_to",
300 &x1, &y1, &x2, &y2, &x3, &y3))
301 return NULL;
302
303 cairo_curve_to (o->ctx, x1, y1, x2, y2, x3, y3);
304 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
305 Py_RETURN_NONE;
306 }
307
308 static PyObject *
309 pycairo_device_to_user(PycairoContext *o, PyObject *args) {
310 double x, y;
311
312 if (!PyArg_ParseTuple(args, "dd:Context.device_to_user", &x, &y))
313 return NULL;
314
315 cairo_device_to_user(o->ctx, &x, &y);
316 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
317 return Py_BuildValue("(dd)", x, y);
318 }
319
320 static PyObject *
321 pycairo_device_to_user_distance (PycairoContext *o, PyObject *args) {
322 double dx, dy;
323
324 if (!PyArg_ParseTuple (args, "dd:Context.device_to_user_distance",
325 &dx, &dy))
326 return NULL;
327
328 cairo_device_to_user_distance (o->ctx, &dx, &dy);
329 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
330 return Py_BuildValue("(dd)", dx, dy);
331 }
332
333 static PyObject *
334 pycairo_fill (PycairoContext *o, PyObject *ignored) {
335 Py_BEGIN_ALLOW_THREADS;
336 cairo_fill (o->ctx);
337 Py_END_ALLOW_THREADS;
338 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
339 Py_RETURN_NONE;
340 }
341
342 static PyObject *
343 pycairo_fill_extents (PycairoContext *o, PyObject *ignored) {
344 double x1, y1, x2, y2;
345 cairo_fill_extents (o->ctx, &x1, &y1, &x2, &y2);
346 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
347 return Py_BuildValue("(dddd)", x1, y1, x2, y2);
348 }
349
350 static PyObject *
351 pycairo_fill_preserve (PycairoContext *o, PyObject *ignored) {
352 Py_BEGIN_ALLOW_THREADS;
353 cairo_fill_preserve (o->ctx);
354 Py_END_ALLOW_THREADS;
355 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
356 Py_RETURN_NONE;
357 }
358
359 static PyObject *
360 pycairo_font_extents (PycairoContext *o, PyObject *ignored) {
361 cairo_font_extents_t e;
362
363 cairo_font_extents (o->ctx, &e);
364 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
365 return Py_BuildValue("(ddddd)", e.ascent, e.descent, e.height,
366 e.max_x_advance, e.max_y_advance);
367 }
368
369 static PyObject *
370 pycairo_get_antialias (PycairoContext *o, PyObject *ignored) {
371 RETURN_INT_ENUM (Antialias, cairo_get_antialias (o->ctx));
372 }
373
374 static PyObject *
375 pycairo_get_current_point (PycairoContext *o, PyObject *ignored) {
376 double x, y;
377 cairo_get_current_point (o->ctx, &x, &y);
378 return Py_BuildValue("(dd)", x, y);
379 }
380
381 static PyObject *
382 pycairo_get_dash (PycairoContext *o, PyObject *ignored) {
383 double *dashes = NULL, offset;
384 int count, i;
385 PyObject *py_dashes = NULL, *rv = NULL;
386
387 count = cairo_get_dash_count (o->ctx);
388 if (count < 0) {
389 PyErr_SetString (PyExc_RuntimeError, "invalid dash return");
390 return NULL;
391 }
392 dashes = PyMem_Malloc ((unsigned int)count * sizeof(double));
393 if (dashes == NULL)
394 return PyErr_NoMemory();
395
396 cairo_get_dash (o->ctx, dashes, &offset);
397 py_dashes = PyTuple_New(count);
398 if (py_dashes == NULL)
399 goto exit;
400
401 for (i = 0; i < count; i++) {
402 PyObject *dash = PyFloat_FromDouble(dashes[i]);
403 if (dash == NULL)
404 goto exit;
405 PyTuple_SET_ITEM (py_dashes, i, dash);
406 }
407 rv = Py_BuildValue("(Od)", py_dashes, offset);
408
409 exit:
410 PyMem_Free (dashes);
411 Py_XDECREF(py_dashes);
412 return rv;
413 }
414
415 static PyObject *
416 pycairo_get_dash_count (PycairoContext *o, PyObject *ignored) {
417 return PyLong_FromLong (cairo_get_dash_count (o->ctx));
418 }
419
420 static PyObject *
421 pycairo_get_fill_rule (PycairoContext *o, PyObject *ignored) {
422 RETURN_INT_ENUM(FillRule, cairo_get_fill_rule (o->ctx));
423 }
424
425 static PyObject *
426 pycairo_get_font_face (PycairoContext *o, PyObject *ignored) {
427 return PycairoFontFace_FromFontFace (
428 cairo_font_face_reference (cairo_get_font_face (o->ctx)));
429 }
430
431 static PyObject *
432 pycairo_get_font_matrix (PycairoContext *o, PyObject *ignored) {
433 cairo_matrix_t matrix;
434 cairo_get_font_matrix (o->ctx, &matrix);
435 return PycairoMatrix_FromMatrix (&matrix);
436 }
437
438 static PyObject *
439 pycairo_get_font_options (PycairoContext *o, PyObject *ignored) {
440 cairo_font_options_t *options = cairo_font_options_create();
441 cairo_get_font_options (o->ctx, options);
442 /* there is no reference fn */
443 return PycairoFontOptions_FromFontOptions (options);
444 }
445
446 static PyObject *
447 pycairo_get_group_target (PycairoContext *o, PyObject *ignored) {
448 cairo_surface_t *surface = cairo_get_group_target (o->ctx);
449 return PycairoSurface_FromSurface (cairo_surface_reference (surface), NULL);
450 }
451
452 static PyObject *
453 pycairo_get_line_cap (PycairoContext *o, PyObject *ignored) {
454 RETURN_INT_ENUM(LineCap, cairo_get_line_cap (o->ctx));
455 }
456
457 static PyObject *
458 pycairo_get_line_join (PycairoContext *o, PyObject *ignored) {
459 RETURN_INT_ENUM(LineJoin, cairo_get_line_join (o->ctx));
460 }
461
462 static PyObject *
463 pycairo_get_line_width (PycairoContext *o, PyObject *ignored) {
464 return PyFloat_FromDouble(cairo_get_line_width (o->ctx));
465 }
466
467 static PyObject *
468 pycairo_get_matrix (PycairoContext *o, PyObject *ignored) {
469 cairo_matrix_t matrix;
470 cairo_get_matrix (o->ctx, &matrix);
471 return PycairoMatrix_FromMatrix (&matrix);
472 }
473
474 static PyObject *
475 pycairo_get_miter_limit (PycairoContext *o, PyObject *ignored) {
476 return PyFloat_FromDouble (cairo_get_miter_limit (o->ctx));
477 }
478
479 static PyObject *
480 pycairo_get_operator (PycairoContext *o, PyObject *ignored) {
481 RETURN_INT_ENUM(Operator, cairo_get_operator (o->ctx));
482 }
483
484 static PyObject *
485 pycairo_get_scaled_font (PycairoContext *o, PyObject *ignored) {
486 return PycairoScaledFont_FromScaledFont (
487 cairo_scaled_font_reference (cairo_get_scaled_font (o->ctx)));
488 }
489
490 static PyObject *
491 pycairo_get_source (PycairoContext *o, PyObject *ignored) {
492 return PycairoPattern_FromPattern (
493 cairo_pattern_reference (cairo_get_source (o->ctx)), NULL);
494 }
495
496 static PyObject *
497 pycairo_get_target (PycairoContext *o, PyObject *ignored) {
498 return PycairoSurface_FromSurface (
499 cairo_surface_reference (cairo_get_target (o->ctx)),
500 NULL);
501 }
502
503 static PyObject *
504 pycairo_get_tolerance (PycairoContext *o, PyObject *ignored) {
505 return PyFloat_FromDouble (cairo_get_tolerance (o->ctx));
506 }
507
508 static PyObject *
509 pycairo_glyph_extents (PycairoContext *o, PyObject *args) {
510 int num_glyphs = -1;
511 cairo_glyph_t *glyphs;
512 cairo_text_extents_t extents;
513 PyObject *py_object, *ext_args, *res;
514
515 if (!PyArg_ParseTuple (args, "O|i:Context.glyph_extents",
516 &py_object, &num_glyphs))
517 return NULL;
518
519 glyphs = _PycairoGlyphs_AsGlyphs (py_object, &num_glyphs);
520 if (glyphs == NULL)
521 return NULL;
522 cairo_glyph_extents (o->ctx, glyphs, num_glyphs, &extents);
523 PyMem_Free (glyphs);
524 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
525 ext_args = Py_BuildValue ("(dddddd)", extents.x_bearing, extents.y_bearing,
526 extents.width, extents.height, extents.x_advance,
527 extents.y_advance);
528 res = PyObject_Call ((PyObject *)&PycairoTextExtents_Type, ext_args, NULL);
529 Py_DECREF (ext_args);
530 return res;
531 }
532
533 static PyObject *
534 pycairo_glyph_path (PycairoContext *o, PyObject *args) {
535 int num_glyphs = -1;
536 cairo_glyph_t *glyphs;
537 PyObject *py_object;
538
539 if (!PyArg_ParseTuple (args, "O|i:Context.glyph_path",
540 &py_object, &num_glyphs))
541 return NULL;
542
543 glyphs = _PycairoGlyphs_AsGlyphs (py_object, &num_glyphs);
544 if (glyphs == NULL)
545 return NULL;
546 cairo_glyph_path (o->ctx, glyphs, num_glyphs);
547 PyMem_Free (glyphs);
548 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
549 Py_RETURN_NONE;
550 }
551
552 static PyObject *
553 pycairo_has_current_point (PycairoContext *o, PyObject *ignored) {
554 PyObject *b = cairo_has_current_point (o->ctx) ? Py_True : Py_False;
555 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
556 Py_INCREF(b);
557 return b;
558 }
559
560 static PyObject *
561 pycairo_identity_matrix (PycairoContext *o, PyObject *ignored) {
562 cairo_identity_matrix (o->ctx);
563 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
564 Py_RETURN_NONE;
565 }
566
567 static PyObject *
568 pycairo_in_fill (PycairoContext *o, PyObject *args) {
569 double x, y;
570 PyObject *result;
571
572 if (!PyArg_ParseTuple (args, "dd:Context.in_fill", &x, &y))
573 return NULL;
574
575 result = cairo_in_fill (o->ctx, x, y) ? Py_True : Py_False;
576 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
577 Py_INCREF(result);
578 return result;
579 }
580
581 static PyObject *
582 pycairo_in_stroke (PycairoContext *o, PyObject *args) {
583 double x, y;
584 PyObject *result;
585
586 if (!PyArg_ParseTuple (args, "dd:Context.in_stroke", &x, &y))
587 return NULL;
588
589 result = cairo_in_stroke (o->ctx, x, y) ? Py_True : Py_False;
590 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
591 Py_INCREF(result);
592 return result;
593 }
594
595 static PyObject *
596 pycairo_line_to (PycairoContext *o, PyObject *args) {
597 double x, y;
598
599 if (!PyArg_ParseTuple (args, "dd:Context.line_to", &x, &y))
600 return NULL;
601
602 cairo_line_to (o->ctx, x, y);
603 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
604 Py_RETURN_NONE;
605 }
606
607 static PyObject *
608 pycairo_mask (PycairoContext *o, PyObject *args) {
609 PycairoPattern *p;
610
611 if (!PyArg_ParseTuple(args, "O!:Context.mask", &PycairoPattern_Type, &p))
612 return NULL;
613
614 Py_BEGIN_ALLOW_THREADS;
615 cairo_mask (o->ctx, p->pattern);
616 Py_END_ALLOW_THREADS;
617 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
618 Py_RETURN_NONE;
619 }
620
621 static PyObject *
622 pycairo_mask_surface (PycairoContext *o, PyObject *args) {
623 PycairoSurface *s;
624 double surface_x = 0.0, surface_y = 0.0;
625
626 if (!PyArg_ParseTuple (args, "O!|dd:Context.mask_surface",
627 &PycairoSurface_Type, &s, &surface_x, &surface_y))
628 return NULL;
629
630 Py_BEGIN_ALLOW_THREADS;
631 cairo_mask_surface (o->ctx, s->surface, surface_x, surface_y);
632 Py_END_ALLOW_THREADS;
633 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
634 Py_RETURN_NONE;
635 }
636
637 static PyObject *
638 pycairo_move_to (PycairoContext *o, PyObject *args) {
639 double x, y;
640
641 if (!PyArg_ParseTuple (args, "dd:Context.move_to", &x, &y))
642 return NULL;
643
644 cairo_move_to (o->ctx, x, y);
645 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
646 Py_RETURN_NONE;
647 }
648
649 static PyObject *
650 pycairo_new_path (PycairoContext *o, PyObject *ignored) {
651 cairo_new_path (o->ctx);
652 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
653 Py_RETURN_NONE;
654 }
655
656 static PyObject *
657 pycairo_new_sub_path (PycairoContext *o, PyObject *ignored) {
658 cairo_new_sub_path (o->ctx);
659 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
660 Py_RETURN_NONE;
661 }
662
663 static PyObject *
664 pycairo_paint (PycairoContext *o, PyObject *ignored) {
665 Py_BEGIN_ALLOW_THREADS;
666 cairo_paint (o->ctx);
667 Py_END_ALLOW_THREADS;
668 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
669 Py_RETURN_NONE;
670 }
671
672 static PyObject *
673 pycairo_paint_with_alpha (PycairoContext *o, PyObject *args) {
674 double alpha;
675
676 if (!PyArg_ParseTuple (args, "d:Context.paint_with_alpha", &alpha))
677 return NULL;
678
679 Py_BEGIN_ALLOW_THREADS;
680 cairo_paint_with_alpha (o->ctx, alpha);
681 Py_END_ALLOW_THREADS;
682 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
683 Py_RETURN_NONE;
684 }
685
686 static PyObject *
687 pycairo_path_extents (PycairoContext *o, PyObject *ignored) {
688 double x1, y1, x2, y2;
689 cairo_path_extents (o->ctx, &x1, &y1, &x2, &y2);
690 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
691 return Py_BuildValue("(dddd)", x1, y1, x2, y2);
692 }
693
694 static PyObject *
695 pycairo_pop_group (PycairoContext *o, PyObject *ignored) {
696 return PycairoPattern_FromPattern (cairo_pop_group (o->ctx), NULL);
697 }
698
699 static PyObject *
700 pycairo_pop_group_to_source (PycairoContext *o, PyObject *ignored) {
701 cairo_pop_group_to_source (o->ctx);
702 Py_RETURN_NONE;
703 }
704
705 static PyObject *
706 pycairo_push_group (PycairoContext *o, PyObject *ignored) {
707 cairo_push_group (o->ctx);
708 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
709 Py_RETURN_NONE;
710 }
711
712 static PyObject *
713 pycairo_push_group_with_content (PycairoContext *o, PyObject *args) {
714 cairo_content_t content;
715 int content_arg;
716
717 if (!PyArg_ParseTuple (args, "i:Context.push_group_with_content",
718 &content_arg))
719 return NULL;
720
721 content = (cairo_content_t)content_arg;
722
723 cairo_push_group_with_content (o->ctx, content);
724 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
725 Py_RETURN_NONE;
726 }
727
728 static PyObject *
729 pycairo_rectangle (PycairoContext *o, PyObject *args) {
730 double x, y, width, height;
731
732 if (!PyArg_ParseTuple (args, "dddd:Context.rectangle",
733 &x, &y, &width, &height))
734 return NULL;
735
736 cairo_rectangle (o->ctx, x, y, width, height);
737 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
738 Py_RETURN_NONE;
739 }
740
741 static PyObject *
742 pycairo_rel_curve_to (PycairoContext *o, PyObject *args) {
743 double dx1, dy1, dx2, dy2, dx3, dy3;
744
745 if (!PyArg_ParseTuple (args, "dddddd:Context.rel_curve_to",
746 &dx1, &dy1, &dx2, &dy2, &dx3, &dy3))
747 return NULL;
748
749 cairo_rel_curve_to (o->ctx, dx1, dy1, dx2, dy2, dx3, dy3);
750 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
751 Py_RETURN_NONE;
752 }
753
754 static PyObject *
755 pycairo_rel_line_to (PycairoContext *o, PyObject *args) {
756 double dx, dy;
757
758 if (!PyArg_ParseTuple (args, "dd:Context.rel_line_to", &dx, &dy))
759 return NULL;
760
761 cairo_rel_line_to (o->ctx, dx, dy);
762 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
763 Py_RETURN_NONE;
764 }
765
766 static PyObject *
767 pycairo_rel_move_to (PycairoContext *o, PyObject *args) {
768 double dx, dy;
769
770 if (!PyArg_ParseTuple (args, "dd:Context.rel_move_to", &dx, &dy))
771 return NULL;
772
773 cairo_rel_move_to (o->ctx, dx, dy);
774 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
775 Py_RETURN_NONE;
776 }
777
778 static PyObject *
779 pycairo_reset_clip (PycairoContext *o, PyObject *ignored) {
780 cairo_reset_clip (o->ctx);
781 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
782 Py_RETURN_NONE;
783 }
784
785 static PyObject *
786 pycairo_restore (PycairoContext *o, PyObject *ignored) {
787 cairo_restore (o->ctx);
788 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
789 Py_RETURN_NONE;
790 }
791
792 static PyObject *
793 pycairo_rotate (PycairoContext *o, PyObject *args) {
794 double angle;
795
796 if (!PyArg_ParseTuple(args, "d:Context.rotate", &angle))
797 return NULL;
798
799 cairo_rotate (o->ctx, angle);
800 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
801 Py_RETURN_NONE;
802 }
803
804 static PyObject *
805 pycairo_save (PycairoContext *o, PyObject *ignored) {
806 cairo_save (o->ctx);
807 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
808 Py_RETURN_NONE;
809 }
810
811 static PyObject *
812 pycairo_scale (PycairoContext *o, PyObject *args) {
813 double sx, sy;
814
815 if (!PyArg_ParseTuple (args, "dd:Context.scale", &sx, &sy))
816 return NULL;
817
818 cairo_scale (o->ctx, sx, sy);
819 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
820 Py_RETURN_NONE;
821 }
822
823 static PyObject *
824 pycairo_select_font_face (PycairoContext *o, PyObject *args) {
825 const char *utf8;
826 cairo_font_slant_t slant;
827 cairo_font_weight_t weight;
828 int slant_arg = CAIRO_FONT_SLANT_NORMAL;
829 int weight_arg = CAIRO_FONT_WEIGHT_NORMAL;
830
831 if (!PyArg_ParseTuple (args, "es|ii:Context.select_font_face",
832 "utf-8", &utf8, &slant_arg, &weight_arg))
833 return NULL;
834
835 slant = (cairo_font_slant_t)slant_arg;
836 weight = (cairo_font_weight_t)weight_arg;
837
838 cairo_select_font_face (o->ctx, utf8, slant, weight);
839 PyMem_Free((void *)utf8);
840 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
841 Py_RETURN_NONE;
842 }
843
844 static PyObject *
845 pycairo_set_antialias (PycairoContext *o, PyObject *args) {
846 cairo_antialias_t antialias;
847 int antialias_arg = CAIRO_ANTIALIAS_DEFAULT;
848
849 if (!PyArg_ParseTuple (args, "|i:Context.set_antialias", &antialias_arg))
850 return NULL;
851
852 antialias = (cairo_antialias_t)antialias_arg;
853
854 cairo_set_antialias (o->ctx, antialias);
855 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
856 Py_RETURN_NONE;
857 }
858
859 static PyObject *
860 pycairo_set_dash (PycairoContext *o, PyObject *args) {
861 double *dashes, offset = 0;
862 Py_ssize_t num_dashes, i;
863 PyObject *py_dashes;
864
865 if (!PyArg_ParseTuple (args, "O|d:Context.set_dash", &py_dashes, &offset))
866 return NULL;
867
868 py_dashes = PySequence_Fast (py_dashes,
869 "first argument must be a sequence");
870 if (py_dashes == NULL)
871 return NULL;
872
873 num_dashes = PySequence_Fast_GET_SIZE(py_dashes);
874 if (num_dashes > INT_MAX) {
875 Py_DECREF (py_dashes);
876 PyErr_SetString (PyExc_ValueError, "dash sequence too large");
877 return NULL;
878 }
879 dashes = PyMem_Malloc ((unsigned int)num_dashes * sizeof(double));
880 if (dashes == NULL) {
881 Py_DECREF(py_dashes);
882 return PyErr_NoMemory();
883 }
884
885 for (i = 0; i < num_dashes; i++) {
886 dashes[i] = PyFloat_AsDouble(PySequence_Fast_GET_ITEM(py_dashes, i));
887 if (PyErr_Occurred()) {
888 PyMem_Free (dashes);
889 Py_DECREF(py_dashes);
890 return NULL;
891 }
892 }
893 cairo_set_dash (o->ctx, dashes, (int)num_dashes, offset);
894 PyMem_Free (dashes);
895 Py_DECREF(py_dashes);
896 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
897 Py_RETURN_NONE;
898 }
899
900 static PyObject *
901 pycairo_set_fill_rule (PycairoContext *o, PyObject *args) {
902 cairo_fill_rule_t fill_rule;
903 int fill_rule_arg;
904
905 if (!PyArg_ParseTuple (args, "i:Context.set_fill_rule", &fill_rule_arg))
906 return NULL;
907
908 fill_rule = (cairo_fill_rule_t)fill_rule_arg;
909
910 cairo_set_fill_rule (o->ctx, fill_rule);
911 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
912 Py_RETURN_NONE;
913 }
914
915 static PyObject *
916 pycairo_set_font_face (PycairoContext *o, PyObject *obj) {
917 if (PyObject_TypeCheck(obj, &PycairoFontFace_Type))
918 cairo_set_font_face (o->ctx, ((PycairoFontFace *)obj)->font_face);
919 else if (obj == Py_None)
920 cairo_set_font_face (o->ctx, NULL);
921 else {
922 PyErr_SetString(PyExc_TypeError,
923 "Context.set_font_face() argument must be "
924 "cairo.FontFace or None");
925 return NULL;
926 }
927 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
928 Py_RETURN_NONE;
929 }
930
931 static PyObject *
932 pycairo_set_font_matrix (PycairoContext *o, PyObject *args) {
933 PycairoMatrix *matrix;
934
935 if (!PyArg_ParseTuple (args, "O!:Context.set_font_matrix",
936 &PycairoMatrix_Type, &matrix))
937 return NULL;
938
939 cairo_set_font_matrix (o->ctx, &matrix->matrix);
940 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
941 Py_RETURN_NONE;
942 }
943
944 static PyObject *
945 pycairo_set_font_options (PycairoContext *o, PyObject *args) {
946 PycairoFontOptions *options;
947
948 if (!PyArg_ParseTuple (args, "O!:Context.set_font_options",
949 &PycairoFontOptions_Type, &options))
950 return NULL;
951
952 cairo_set_font_options (o->ctx, options->font_options);
953 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
954 Py_RETURN_NONE;
955 }
956
957 static PyObject *
958 pycairo_set_font_size (PycairoContext *o, PyObject *args) {
959 double size;
960
961 if (!PyArg_ParseTuple (args, "d:Context.set_font_size", &size))
962 return NULL;
963
964 cairo_set_font_size (o->ctx, size);
965 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
966 Py_RETURN_NONE;
967 }
968
969 static PyObject *
970 pycairo_set_line_cap (PycairoContext *o, PyObject *args) {
971 cairo_line_cap_t line_cap;
972 int line_cap_arg;
973
974 if (!PyArg_ParseTuple (args, "i:Context.set_line_cap", &line_cap_arg))
975 return NULL;
976
977 line_cap = (cairo_line_cap_t)line_cap_arg;
978
979 cairo_set_line_cap (o->ctx, line_cap);
980 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
981 Py_RETURN_NONE;
982 }
983
984 static PyObject *
985 pycairo_set_line_join (PycairoContext *o, PyObject *args) {
986 cairo_line_join_t line_join;
987 int line_join_arg;
988
989 if (!PyArg_ParseTuple (args, "i:Context.set_line_join", &line_join_arg))
990 return NULL;
991
992 line_join = (cairo_line_join_t)line_join_arg;
993
994 cairo_set_line_join (o->ctx, line_join);
995 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
996 Py_RETURN_NONE;
997 }
998
999 static PyObject *
1000 pycairo_set_line_width (PycairoContext *o, PyObject *args) {
1001 double width;
1002
1003 if (!PyArg_ParseTuple (args, "d:Context.set_line_width", &width))
1004 return NULL;
1005
1006 cairo_set_line_width (o->ctx, width);
1007 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1008 Py_RETURN_NONE;
1009 }
1010
1011 static PyObject *
1012 pycairo_set_matrix (PycairoContext *o, PyObject *args) {
1013 PycairoMatrix *matrix;
1014
1015 if (!PyArg_ParseTuple (args, "O!:Context.set_matrix",
1016 &PycairoMatrix_Type, &matrix))
1017 return NULL;
1018
1019 cairo_set_matrix (o->ctx, &matrix->matrix);
1020 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1021 Py_RETURN_NONE;
1022 }
1023
1024 static PyObject *
1025 pycairo_set_miter_limit (PycairoContext *o, PyObject *args) {
1026 double limit;
1027
1028 if (!PyArg_ParseTuple (args, "d:Context.set_miter_limit", &limit))
1029 return NULL;
1030
1031 cairo_set_miter_limit (o->ctx, limit);
1032 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1033 Py_RETURN_NONE;
1034 }
1035
1036 static PyObject *
1037 pycairo_set_operator(PycairoContext *o, PyObject *args) {
1038 cairo_operator_t operator;
1039 int operator_arg;
1040
1041 if (!PyArg_ParseTuple (args, "i:Context.set_operator", &operator_arg))
1042 return NULL;
1043
1044 operator = (cairo_operator_t)operator_arg;
1045
1046 cairo_set_operator(o->ctx, operator);
1047 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1048 Py_RETURN_NONE;
1049 }
1050
1051 static PyObject *
1052 pycairo_set_scaled_font(PycairoContext *o, PyObject *args) {
1053 PycairoScaledFont *f;
1054 if (!PyArg_ParseTuple (args, "O!:Context.set_scaled_font",
1055 &PycairoScaledFont_Type, &f))
1056 return NULL;
1057
1058 cairo_set_scaled_font(o->ctx, f->scaled_font);
1059 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1060 Py_RETURN_NONE;
1061 }
1062
1063 static PyObject *
1064 pycairo_set_source (PycairoContext *o, PyObject *args) {
1065 PycairoPattern *p;
1066
1067 if (!PyArg_ParseTuple (args, "O!:Context.set_source",
1068 &PycairoPattern_Type, &p))
1069 return NULL;
1070
1071 cairo_set_source (o->ctx, p->pattern);
1072 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1073 Py_RETURN_NONE;
1074 }
1075
1076 static PyObject *
1077 pycairo_set_source_rgb (PycairoContext *o, PyObject *args) {
1078 double red, green, blue;
1079
1080 if (!PyArg_ParseTuple (args, "ddd:Context.set_source_rgb",
1081 &red, &green, &blue))
1082 return NULL;
1083
1084 cairo_set_source_rgb (o->ctx, red, green, blue);
1085 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1086 Py_RETURN_NONE;
1087 }
1088
1089 static PyObject *
1090 pycairo_set_source_rgba (PycairoContext *o, PyObject *args) {
1091 double red, green, blue;
1092 double alpha = 1.0;
1093
1094 if (!PyArg_ParseTuple (args, "ddd|d:Context.set_source_rgba",
1095 &red, &green, &blue, &alpha))
1096 return NULL;
1097
1098 cairo_set_source_rgba (o->ctx, red, green, blue, alpha);
1099 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1100 Py_RETURN_NONE;
1101 }
1102
1103 static PyObject *
1104 pycairo_set_source_surface (PycairoContext *o, PyObject *args) {
1105 PycairoSurface *surface;
1106 double x = 0.0, y = 0.0;
1107
1108 if (!PyArg_ParseTuple (args, "O!|dd:Context.set_source_surface",
1109 &PycairoSurface_Type, &surface, &x, &y))
1110 return NULL;
1111
1112 cairo_set_source_surface (o->ctx, surface->surface, x, y);
1113 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1114 Py_RETURN_NONE;
1115 }
1116
1117 static PyObject *
1118 pycairo_set_tolerance (PycairoContext *o, PyObject *args) {
1119 double tolerance;
1120 if (!PyArg_ParseTuple (args, "d:Context.set_tolerance", &tolerance))
1121 return NULL;
1122 cairo_set_tolerance (o->ctx, tolerance);
1123 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1124 Py_RETURN_NONE;
1125 }
1126
1127 static PyObject *
1128 pycairo_show_glyphs (PycairoContext *o, PyObject *args) {
1129 int num_glyphs = -1;
1130 cairo_glyph_t *glyphs;
1131 PyObject *py_object;
1132
1133 if (!PyArg_ParseTuple (args, "O|i:Context.show_glyphs",
1134 &py_object, &num_glyphs))
1135 return NULL;
1136
1137 glyphs = _PycairoGlyphs_AsGlyphs (py_object, &num_glyphs);
1138 if (glyphs == NULL)
1139 return NULL;
1140 Py_BEGIN_ALLOW_THREADS;
1141 cairo_show_glyphs (o->ctx, glyphs, num_glyphs);
1142 Py_END_ALLOW_THREADS;
1143 PyMem_Free (glyphs);
1144 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1145 Py_RETURN_NONE;
1146 }
1147
1148 static PyObject *
1149 pycairo_show_page (PycairoContext *o, PyObject *ignored) {
1150 Py_BEGIN_ALLOW_THREADS;
1151 cairo_show_page (o->ctx);
1152 Py_END_ALLOW_THREADS;
1153 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1154 Py_RETURN_NONE;
1155 }
1156
1157 static PyObject *
1158 pycairo_show_text (PycairoContext *o, PyObject *args) {
1159 const char *utf8;
1160
1161 if (!PyArg_ParseTuple (args, "es:Context.show_text", "utf-8", &utf8))
1162 return NULL;
1163
1164 Py_BEGIN_ALLOW_THREADS;
1165 cairo_show_text (o->ctx, utf8);
1166 Py_END_ALLOW_THREADS;
1167
1168 PyMem_Free((void *)utf8);
1169 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1170 Py_RETURN_NONE;
1171 }
1172
1173 static PyObject *
1174 pycairo_stroke (PycairoContext *o, PyObject *ignored) {
1175 Py_BEGIN_ALLOW_THREADS;
1176 cairo_stroke (o->ctx);
1177 Py_END_ALLOW_THREADS;
1178 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1179 Py_RETURN_NONE;
1180 }
1181
1182 static PyObject *
1183 pycairo_stroke_extents (PycairoContext *o, PyObject *ignored) {
1184 double x1, y1, x2, y2;
1185 cairo_stroke_extents (o->ctx, &x1, &y1, &x2, &y2);
1186 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1187 return Py_BuildValue("(dddd)", x1, y1, x2, y2);
1188 }
1189
1190 static PyObject *
1191 pycairo_stroke_preserve (PycairoContext *o, PyObject *ignored) {
1192 Py_BEGIN_ALLOW_THREADS;
1193 cairo_stroke_preserve (o->ctx);
1194 Py_END_ALLOW_THREADS;
1195 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1196 Py_RETURN_NONE;
1197 }
1198
1199 static PyObject *
1200 pycairo_text_extents (PycairoContext *o, PyObject *args) {
1201 cairo_text_extents_t extents;
1202 const char *utf8;
1203 PyObject *ext_args, *res;
1204
1205 if (!PyArg_ParseTuple (args, "es:Context.text_extents", "utf-8", &utf8))
1206 return NULL;
1207
1208 cairo_text_extents (o->ctx, utf8, &extents);
1209 PyMem_Free((void *)utf8);
1210 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1211 ext_args = Py_BuildValue ("(dddddd)", extents.x_bearing, extents.y_bearing,
1212 extents.width, extents.height, extents.x_advance,
1213 extents.y_advance);
1214 res = PyObject_Call ((PyObject *)&PycairoTextExtents_Type, ext_args, NULL);
1215 Py_DECREF (ext_args);
1216 return res;
1217 }
1218
1219 static PyObject *
1220 pycairo_text_path (PycairoContext *o, PyObject *args) {
1221 const char *utf8;
1222
1223 if (!PyArg_ParseTuple (args, "es:Context.text_path", "utf-8", &utf8))
1224 return NULL;
1225
1226 cairo_text_path (o->ctx, utf8);
1227 PyMem_Free((void *)utf8);
1228 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1229 Py_RETURN_NONE;
1230 }
1231
1232 static PyObject *
1233 pycairo_transform (PycairoContext *o, PyObject *args) {
1234 PycairoMatrix *matrix;
1235
1236 if (!PyArg_ParseTuple (args, "O!:Context.transform",
1237 &PycairoMatrix_Type, &matrix))
1238 return NULL;
1239
1240 cairo_transform (o->ctx, &matrix->matrix);
1241 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1242 Py_RETURN_NONE;
1243 }
1244
1245 static PyObject *
1246 pycairo_translate (PycairoContext *o, PyObject *args) {
1247 double tx, ty;
1248
1249 if (!PyArg_ParseTuple (args, "dd:Context.translate", &tx, &ty))
1250 return NULL;
1251
1252 cairo_translate (o->ctx, tx, ty);
1253 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1254 Py_RETURN_NONE;
1255 }
1256
1257 static PyObject *
1258 pycairo_user_to_device (PycairoContext *o, PyObject *args) {
1259 double x, y;
1260
1261 if (!PyArg_ParseTuple (args, "dd:Context.user_to_device", &x, &y))
1262 return NULL;
1263
1264 cairo_user_to_device (o->ctx, &x, &y);
1265 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1266 return Py_BuildValue("(dd)", x, y);
1267 }
1268
1269 static PyObject *
1270 pycairo_user_to_device_distance (PycairoContext *o, PyObject *args) {
1271 double dx, dy;
1272
1273 if (!PyArg_ParseTuple (args, "dd:Context.user_to_device_distance",
1274 &dx, &dy))
1275 return NULL;
1276
1277 cairo_user_to_device_distance (o->ctx, &dx, &dy);
1278 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR(o->ctx);
1279 return Py_BuildValue("(dd)", dx, dy);
1280 }
1281
1282 static PyObject *
1283 pycairo_show_text_glyphs (PycairoContext *o, PyObject *args) {
1284 const char *utf8 = NULL;
1285 PyObject *glyphs_arg, *glyphs_seq = NULL;
1286 PyObject *clusters_arg, *clusters_seq = NULL;
1287 cairo_text_cluster_flags_t cluster_flags;
1288 int cluster_flags_arg;
1289 PyObject *py_item;
1290 cairo_glyph_t *glyphs = NULL;
1291 cairo_text_cluster_t *clusters = NULL;
1292 Py_ssize_t i, clusters_size, glyphs_size;
1293
1294 if (!PyArg_ParseTuple (args,
1295 "esOOi:Context.show_text_glyphs",
1296 "utf-8", &utf8, &glyphs_arg, &clusters_arg, &cluster_flags_arg))
1297 return NULL;
1298
1299 cluster_flags = (cairo_text_cluster_flags_t)cluster_flags_arg;
1300
1301 glyphs_seq = PySequence_Fast (glyphs_arg, "glyphs must be a sequence");
1302 if (glyphs_seq == NULL)
1303 goto error;
1304 glyphs_size = PySequence_Fast_GET_SIZE (glyphs_seq);
1305 if (glyphs_size > INT_MAX) {
1306 PyErr_SetString (PyExc_ValueError, "glyph sequence too large");
1307 goto error;
1308 }
1309 glyphs = cairo_glyph_allocate ((int)glyphs_size);
1310 if (glyphs_size && glyphs == NULL) {
1311 PyErr_NoMemory();
1312 goto error;
1313 }
1314 for (i=0; i < glyphs_size; i++) {
1315 py_item = PySequence_Fast_GET_ITEM (glyphs_seq, i);
1316 if (py_item == 0 || _PyGlyph_AsGlyph (py_item, &(glyphs[i])) != 0)
1317 goto error;
1318 }
1319 Py_CLEAR (glyphs_seq);
1320
1321 clusters_seq = PySequence_Fast (clusters_arg, "clusters must be a sequence");
1322 if (clusters_seq == NULL)
1323 goto error;
1324 clusters_size = PySequence_Fast_GET_SIZE (clusters_seq);
1325 if (clusters_size > INT_MAX) {
1326 PyErr_SetString (PyExc_ValueError, "clusters sequence too large");
1327 goto error;
1328 }
1329 clusters = cairo_text_cluster_allocate ((int)clusters_size);
1330 if (clusters_size && clusters == NULL) {
1331 PyErr_NoMemory();
1332 goto error;
1333 }
1334 for (i=0; i < clusters_size; i++) {
1335 py_item = PySequence_Fast_GET_ITEM (clusters_seq, i);
1336 if (py_item == NULL ||
1337 _PyTextCluster_AsTextCluster (py_item, &(clusters[i])) != 0)
1338 goto error;
1339 }
1340 Py_CLEAR (clusters_seq);
1341
1342 Py_BEGIN_ALLOW_THREADS;
1343 cairo_show_text_glyphs (
1344 o->ctx, utf8, -1, glyphs, (int)glyphs_size, clusters,
1345 (int)clusters_size, cluster_flags);
1346 Py_END_ALLOW_THREADS;
1347
1348 PyMem_Free ((void *)utf8);
1349 utf8 = NULL;
1350 cairo_glyph_free (glyphs);
1351 cairo_text_cluster_free (clusters);
1352
1353 RETURN_NULL_IF_CAIRO_CONTEXT_ERROR (o->ctx);
1354 Py_RETURN_NONE;
1355 error:
1356 PyMem_Free ((void *)utf8);
1357 cairo_glyph_free (glyphs);
1358 cairo_text_cluster_free (clusters);
1359 Py_XDECREF (glyphs_seq);
1360 Py_XDECREF (clusters_seq);
1361 return NULL;
1362 }
1363
1364 static PyMethodDef pycairo_methods[] = {
1365 /* methods never exposed in a language binding:
1366 * cairo_destroy()
1367 * cairo_reference()
1368 * cairo_rectangle_list_destroy()
1369 *
1370 * cairo_status()
1371 * cairo_status_string()
1372 * - not needed since Pycairo calls Pycairo_Check_Status() to check
1373 * for errors and raise exceptions
1374 */
1375 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 17, 6)
1376 {"set_hairline", (PyCFunction)pycairo_set_hairline, METH_VARARGS},
1377 {"get_hairline", (PyCFunction)pycairo_get_hairline, METH_NOARGS},
1378 #endif
1379 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 15, 10)
1380 {"tag_begin", (PyCFunction)pycairo_tag_begin, METH_VARARGS},
1381 {"tag_end", (PyCFunction)pycairo_tag_end, METH_VARARGS},
1382 #endif
1383 {"append_path", (PyCFunction)pycairo_append_path, METH_VARARGS},
1384 {"arc", (PyCFunction)pycairo_arc, METH_VARARGS},
1385 {"arc_negative", (PyCFunction)pycairo_arc_negative, METH_VARARGS},
1386 {"clip", (PyCFunction)pycairo_clip, METH_NOARGS},
1387 {"clip_extents", (PyCFunction)pycairo_clip_extents, METH_NOARGS},
1388 {"clip_preserve", (PyCFunction)pycairo_clip_preserve, METH_NOARGS},
1389 {"close_path", (PyCFunction)pycairo_close_path, METH_NOARGS},
1390 {"copy_clip_rectangle_list", (PyCFunction)pycairo_copy_clip_rectangle_list,
1391 METH_NOARGS},
1392 {"copy_page", (PyCFunction)pycairo_copy_page, METH_NOARGS},
1393 {"copy_path", (PyCFunction)pycairo_copy_path, METH_NOARGS},
1394 {"copy_path_flat", (PyCFunction)pycairo_copy_path_flat, METH_NOARGS},
1395 {"curve_to", (PyCFunction)pycairo_curve_to, METH_VARARGS},
1396 {"device_to_user", (PyCFunction)pycairo_device_to_user, METH_VARARGS},
1397 {"device_to_user_distance", (PyCFunction)pycairo_device_to_user_distance,
1398 METH_VARARGS},
1399 {"fill", (PyCFunction)pycairo_fill, METH_NOARGS},
1400 {"fill_extents", (PyCFunction)pycairo_fill_extents, METH_NOARGS},
1401 {"fill_preserve", (PyCFunction)pycairo_fill_preserve, METH_NOARGS},
1402 {"font_extents", (PyCFunction)pycairo_font_extents, METH_NOARGS},
1403 {"get_antialias", (PyCFunction)pycairo_get_antialias, METH_NOARGS},
1404 {"get_current_point",(PyCFunction)pycairo_get_current_point,METH_NOARGS},
1405 {"get_dash", (PyCFunction)pycairo_get_dash, METH_NOARGS},
1406 {"get_dash_count", (PyCFunction)pycairo_get_dash_count, METH_NOARGS},
1407 {"get_fill_rule", (PyCFunction)pycairo_get_fill_rule, METH_NOARGS},
1408 {"get_font_face", (PyCFunction)pycairo_get_font_face, METH_NOARGS},
1409 {"get_font_matrix", (PyCFunction)pycairo_get_font_matrix, METH_NOARGS},
1410 {"get_font_options",(PyCFunction)pycairo_get_font_options, METH_NOARGS},
1411 {"get_group_target",(PyCFunction)pycairo_get_group_target, METH_NOARGS},
1412 {"get_line_cap", (PyCFunction)pycairo_get_line_cap, METH_NOARGS},
1413 {"get_line_join", (PyCFunction)pycairo_get_line_join, METH_NOARGS},
1414 {"get_line_width", (PyCFunction)pycairo_get_line_width, METH_NOARGS},
1415 {"get_matrix", (PyCFunction)pycairo_get_matrix, METH_NOARGS},
1416 {"get_miter_limit", (PyCFunction)pycairo_get_miter_limit, METH_NOARGS},
1417 {"get_operator", (PyCFunction)pycairo_get_operator, METH_NOARGS},
1418 {"get_scaled_font", (PyCFunction)pycairo_get_scaled_font, METH_NOARGS},
1419 {"get_source", (PyCFunction)pycairo_get_source, METH_NOARGS},
1420 {"get_target", (PyCFunction)pycairo_get_target, METH_NOARGS},
1421 {"get_tolerance", (PyCFunction)pycairo_get_tolerance, METH_NOARGS},
1422 {"glyph_extents", (PyCFunction)pycairo_glyph_extents, METH_VARARGS},
1423 {"glyph_path", (PyCFunction)pycairo_glyph_path, METH_VARARGS},
1424 {"has_current_point",(PyCFunction)pycairo_has_current_point, METH_NOARGS},
1425 {"identity_matrix", (PyCFunction)pycairo_identity_matrix, METH_NOARGS},
1426 {"in_clip", (PyCFunction)pycairo_in_clip, METH_VARARGS},
1427 {"in_fill", (PyCFunction)pycairo_in_fill, METH_VARARGS},
1428 {"in_stroke", (PyCFunction)pycairo_in_stroke, METH_VARARGS},
1429 {"line_to", (PyCFunction)pycairo_line_to, METH_VARARGS},
1430 {"mask", (PyCFunction)pycairo_mask, METH_VARARGS},
1431 {"mask_surface", (PyCFunction)pycairo_mask_surface, METH_VARARGS},
1432 {"move_to", (PyCFunction)pycairo_move_to, METH_VARARGS},
1433 {"new_path", (PyCFunction)pycairo_new_path, METH_NOARGS},
1434 {"new_sub_path", (PyCFunction)pycairo_new_sub_path, METH_NOARGS},
1435 {"paint", (PyCFunction)pycairo_paint, METH_NOARGS},
1436 {"paint_with_alpha",(PyCFunction)pycairo_paint_with_alpha, METH_VARARGS},
1437 {"path_extents", (PyCFunction)pycairo_path_extents, METH_NOARGS},
1438 {"pop_group", (PyCFunction)pycairo_pop_group, METH_NOARGS},
1439 {"pop_group_to_source", (PyCFunction)pycairo_pop_group_to_source,
1440 METH_NOARGS},
1441 {"push_group", (PyCFunction)pycairo_push_group, METH_NOARGS},
1442 {"push_group_with_content", (PyCFunction)pycairo_push_group_with_content,
1443 METH_VARARGS},
1444 {"rectangle", (PyCFunction)pycairo_rectangle, METH_VARARGS},
1445 {"rel_curve_to", (PyCFunction)pycairo_rel_curve_to, METH_VARARGS},
1446 {"rel_line_to", (PyCFunction)pycairo_rel_line_to, METH_VARARGS},
1447 {"rel_move_to", (PyCFunction)pycairo_rel_move_to, METH_VARARGS},
1448 {"reset_clip", (PyCFunction)pycairo_reset_clip, METH_NOARGS},
1449 {"restore", (PyCFunction)pycairo_restore, METH_NOARGS},
1450 {"rotate", (PyCFunction)pycairo_rotate, METH_VARARGS},
1451 {"save", (PyCFunction)pycairo_save, METH_NOARGS},
1452 {"scale", (PyCFunction)pycairo_scale, METH_VARARGS},
1453 {"select_font_face",(PyCFunction)pycairo_select_font_face, METH_VARARGS},
1454 {"set_antialias", (PyCFunction)pycairo_set_antialias, METH_VARARGS},
1455 {"set_dash", (PyCFunction)pycairo_set_dash, METH_VARARGS},
1456 {"set_fill_rule", (PyCFunction)pycairo_set_fill_rule, METH_VARARGS},
1457 {"set_font_face", (PyCFunction)pycairo_set_font_face, METH_O},
1458 {"set_font_matrix", (PyCFunction)pycairo_set_font_matrix, METH_VARARGS},
1459 {"set_font_options",(PyCFunction)pycairo_set_font_options, METH_VARARGS},
1460 {"set_font_size", (PyCFunction)pycairo_set_font_size, METH_VARARGS},
1461 {"set_line_cap", (PyCFunction)pycairo_set_line_cap, METH_VARARGS},
1462 {"set_line_join", (PyCFunction)pycairo_set_line_join, METH_VARARGS},
1463 {"set_line_width", (PyCFunction)pycairo_set_line_width, METH_VARARGS},
1464 {"set_matrix", (PyCFunction)pycairo_set_matrix, METH_VARARGS},
1465 {"set_miter_limit", (PyCFunction)pycairo_set_miter_limit, METH_VARARGS},
1466 {"set_operator", (PyCFunction)pycairo_set_operator, METH_VARARGS},
1467 {"set_scaled_font", (PyCFunction)pycairo_set_scaled_font, METH_VARARGS},
1468 {"set_source", (PyCFunction)pycairo_set_source, METH_VARARGS},
1469 {"set_source_rgb", (PyCFunction)pycairo_set_source_rgb, METH_VARARGS},
1470 {"set_source_rgba", (PyCFunction)pycairo_set_source_rgba, METH_VARARGS},
1471 {"set_source_surface",(PyCFunction)pycairo_set_source_surface, METH_VARARGS},
1472 {"set_tolerance", (PyCFunction)pycairo_set_tolerance, METH_VARARGS},
1473 {"show_glyphs", (PyCFunction)pycairo_show_glyphs, METH_VARARGS},
1474 {"show_page", (PyCFunction)pycairo_show_page, METH_NOARGS},
1475 {"show_text", (PyCFunction)pycairo_show_text, METH_VARARGS},
1476 {"stroke", (PyCFunction)pycairo_stroke, METH_NOARGS},
1477 {"stroke_extents", (PyCFunction)pycairo_stroke_extents, METH_NOARGS},
1478 {"stroke_preserve", (PyCFunction)pycairo_stroke_preserve, METH_NOARGS},
1479 {"text_extents", (PyCFunction)pycairo_text_extents, METH_VARARGS},
1480 {"text_path", (PyCFunction)pycairo_text_path, METH_VARARGS},
1481 {"transform", (PyCFunction)pycairo_transform, METH_VARARGS},
1482 {"translate", (PyCFunction)pycairo_translate, METH_VARARGS},
1483 {"user_to_device", (PyCFunction)pycairo_user_to_device, METH_VARARGS},
1484 {"user_to_device_distance",(PyCFunction)pycairo_user_to_device_distance,
1485 METH_VARARGS},
1486 {"show_text_glyphs",(PyCFunction)pycairo_show_text_glyphs, METH_VARARGS},
1487 {NULL, NULL, 0, NULL},
1488 };
1489
1490 static PyObject*
1491 pycairo_richcompare (PyObject *self, PyObject *other, int op)
1492 {
1493 if (Py_TYPE(self) == Py_TYPE(other))
1494 return Pycairo_richcompare (
1495 ((PycairoContext *)self)->ctx,
1496 ((PycairoContext *)other)->ctx,
1497 op);
1498 else {
1499 Py_INCREF(Py_NotImplemented);
1500 return Py_NotImplemented;
1501 }
1502 }
1503
1504 static Py_hash_t
1505 pycairo_hash (PyObject *self)
1506 {
1507 return PYCAIRO_Py_hash_t_FromVoidPtr (((PycairoContext *)self)->ctx);
1508 }
1509
1510 PyTypeObject PycairoContext_Type = {
1511 PyVarObject_HEAD_INIT(NULL, 0)
1512 "cairo.Context", /* tp_name */
1513 sizeof(PycairoContext), /* tp_basicsize */
1514 0, /* tp_itemsize */
1515 (destructor)pycairo_dealloc, /* tp_dealloc */
1516 0, /* tp_print */
1517 0, /* tp_getattr */
1518 0, /* tp_setattr */
1519 0, /* tp_compare */
1520 0, /* tp_repr */
1521 0, /* tp_as_number */
1522 0, /* tp_as_sequence */
1523 0, /* tp_as_mapping */
1524 pycairo_hash, /* tp_hash */
1525 0, /* tp_call */
1526 0, /* tp_str */
1527 0, /* tp_getattro */
1528 0, /* tp_setattro */
1529 0, /* tp_as_buffer */
1530 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/* tp_flags */
1531 0, /* tp_doc */
1532 0, /* tp_traverse */
1533 0, /* tp_clear */
1534 pycairo_richcompare, /* tp_richcompare */
1535 0, /* tp_weaklistoffset */
1536 0, /* tp_iter */
1537 0, /* tp_iternext */
1538 pycairo_methods, /* tp_methods */
1539 0, /* tp_members */
1540 0, /* tp_getset */
1541 0, /* tp_base */
1542 0, /* tp_dict */
1543 0, /* tp_descr_get */
1544 0, /* tp_descr_set */
1545 0, /* tp_dictoffset */
1546 0, /* tp_init */
1547 0, /* tp_alloc */
1548 (newfunc)pycairo_new, /* tp_new */
1549 0, /* tp_free */
1550 0, /* tp_is_gc */
1551 0, /* tp_bases */
1552 };