Debian Codegraph @

cairo/matrix.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 #include "structmember.h"
34
35 #include "private.h"
36
37
38 PyObject *
39 PycairoMatrix_FromMatrix (const cairo_matrix_t *matrix) {
40 PyObject *o;
41 assert (matrix != NULL);
42 o = PycairoMatrix_Type.tp_alloc (&PycairoMatrix_Type, 0);
43 if (o != NULL)
44 ((PycairoMatrix *)o)->matrix = *matrix; /* copy struct */
45 return o;
46 }
47
48 static void
49 matrix_dealloc (PycairoMatrix *o) {
50 Py_TYPE(o)->tp_free(o);
51 }
52
53 static PyObject *
54 matrix_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
55 cairo_matrix_t mx;
56 static char *kwlist[] = { "xx", "yx", "xy", "yy", "x0", "y0", NULL };
57 double xx = 1.0, yx = 0.0, xy = 0.0, yy = 1.0, x0 = 0.0, y0 = 0.0;
58
59 if (!PyArg_ParseTupleAndKeywords(args, kwds,
60 "|dddddd:Matrix.__init__", kwlist,
61 &xx, &yx, &xy, &yy, &x0, &y0))
62 return NULL;
63
64 cairo_matrix_init (&mx, xx, yx, xy, yy, x0, y0);
65 return PycairoMatrix_FromMatrix (&mx);
66 }
67
68 static PyObject *
69 matrix_init_rotate (PyTypeObject *type, PyObject *args) {
70 cairo_matrix_t matrix;
71 double radians;
72
73 if (!PyArg_ParseTuple(args, "d:Matrix.init_rotate", &radians))
74 return NULL;
75
76 cairo_matrix_init_rotate (&matrix, radians);
77 return PycairoMatrix_FromMatrix (&matrix);
78 }
79
80 static PyObject *
81 matrix_invert (PycairoMatrix *o, PyObject *ignored) {
82 if (Pycairo_Check_Status (cairo_matrix_invert (&o->matrix)))
83 return NULL;
84 Py_RETURN_NONE;
85 }
86
87 /* cairo_matrix_multiply */
88 static PyObject *
89 matrix_multiply (PycairoMatrix *o, PyObject *args) {
90 PycairoMatrix *mx2;
91 cairo_matrix_t result;
92
93 if (!PyArg_ParseTuple(args, "O!:Matrix.multiply",
94 &PycairoMatrix_Type, &mx2))
95 return NULL;
96
97 cairo_matrix_multiply (&result, &o->matrix, &mx2->matrix);
98 return PycairoMatrix_FromMatrix (&result);
99 }
100
101 /* standard matrix multiply, for use by '*' operator */
102 static PyObject *
103 matrix_operator_multiply (PycairoMatrix *o, PycairoMatrix *o2) {
104 cairo_matrix_t result;
105 if (PyObject_IsInstance((PyObject *)o2, (PyObject *)&PycairoMatrix_Type) <= 0) {
106 PyErr_SetString(PyExc_TypeError, "matrix can only multiply another matrix");
107 return NULL;
108 }
109 cairo_matrix_multiply (&result, &o->matrix, &o2->matrix);
110 return PycairoMatrix_FromMatrix (&result);
111 }
112
113 static PyObject *
114 matrix_repr (PycairoMatrix *o) {
115 char buf[256];
116
117 PyOS_snprintf(buf, sizeof(buf), "cairo.Matrix(%g, %g, %g, %g, %g, %g)",
118 o->matrix.xx, o->matrix.yx,
119 o->matrix.xy, o->matrix.yy,
120 o->matrix.x0, o->matrix.y0);
121 return PyUnicode_FromString (buf);
122 }
123
124 static PyObject *
125 matrix_richcmp (PycairoMatrix *m1, PycairoMatrix *m2, int op) {
126 int equal;
127 PyObject *ret;
128 cairo_matrix_t *mx1 = &m1->matrix;
129 cairo_matrix_t *mx2 = &m2->matrix;
130
131 if (op != Py_EQ && op != Py_NE) {
132 PyErr_SetString(PyExc_TypeError, "Only support testing for == or !=");
133 return NULL;
134 }
135
136 if (!PyObject_TypeCheck(m2, &PycairoMatrix_Type)) {
137 Py_INCREF(Py_NotImplemented);
138 return Py_NotImplemented;
139 }
140
141 equal = mx1->xx == mx2->xx && mx1->yx == mx2->yx &&
142 mx1->xy == mx2->xy && mx1->yy == mx2->yy &&
143 mx1->x0 == mx2->x0 && mx1->y0 == mx2->y0;
144
145 if (op == Py_EQ)
146 ret = equal ? Py_True : Py_False;
147 else
148 ret = equal ? Py_False : Py_True;
149 Py_INCREF(ret);
150 return ret;
151 }
152
153 static PyObject *
154 matrix_rotate (PycairoMatrix *o, PyObject *args) {
155 double radians;
156
157 if (!PyArg_ParseTuple(args, "d:Matrix.rotate", &radians))
158 return NULL;
159
160 cairo_matrix_rotate (&o->matrix, radians);
161 Py_RETURN_NONE;
162 }
163
164 static PyObject *
165 matrix_scale (PycairoMatrix *o, PyObject *args) {
166 double sx, sy;
167
168 if (!PyArg_ParseTuple(args, "dd:Matrix.scale", &sx, &sy))
169 return NULL;
170
171 cairo_matrix_scale (&o->matrix, sx, sy);
172 Py_RETURN_NONE;
173 }
174
175 static PyObject *
176 matrix_transform_distance (PycairoMatrix *o, PyObject *args) {
177 double dx, dy;
178
179 if (!PyArg_ParseTuple(args, "dd:Matrix.transform_distance", &dx, &dy))
180 return NULL;
181
182 cairo_matrix_transform_distance (&o->matrix, &dx, &dy);
183 return Py_BuildValue("(dd)", dx, dy);
184 }
185
186 static PyObject *
187 matrix_transform_point (PycairoMatrix *o, PyObject *args) {
188 double x, y;
189
190 if (!PyArg_ParseTuple(args, "dd:Matrix.transform_point", &x, &y))
191 return NULL;
192
193 cairo_matrix_transform_point (&o->matrix, &x, &y);
194 return Py_BuildValue("(dd)", x, y);
195 }
196
197 static PyObject *
198 matrix_translate (PycairoMatrix *o, PyObject *args) {
199 double tx, ty;
200
201 if (!PyArg_ParseTuple(args, "dd:Matrix.translate", &tx, &ty))
202 return NULL;
203
204 cairo_matrix_translate (&o->matrix, tx, ty);
205 Py_RETURN_NONE;
206 }
207
208 static PyObject *
209 matrix_item (PycairoMatrix *o, Py_ssize_t i) {
210 switch (i) {
211 case 0:
212 return Py_BuildValue("d", o->matrix.xx);
213 case 1:
214 return Py_BuildValue("d", o->matrix.yx);
215 case 2:
216 return Py_BuildValue("d", o->matrix.xy);
217 case 3:
218 return Py_BuildValue("d", o->matrix.yy);
219 case 4:
220 return Py_BuildValue("d", o->matrix.x0);
221 case 5:
222 return Py_BuildValue("d", o->matrix.y0);
223 default:
224 PyErr_SetString(PyExc_IndexError, "Matrix index out of range");
225 return NULL;
226 }
227 }
228
229 static PyNumberMethods matrix_as_number = {
230 (binaryfunc)0, /*nb_add*/
231 (binaryfunc)0, /*nb_subtract*/
232 (binaryfunc)matrix_operator_multiply, /*nb_multiply*/
233 (binaryfunc)0, /*nb_remainder*/
234 (binaryfunc)0, /*nb_divmod*/
235 (ternaryfunc)0, /*nb_power*/
236 (unaryfunc)0, /*nb_negative*/
237 (unaryfunc)0, /*nb_positive*/
238 (unaryfunc)0, /*nb_absolute*/
239 (inquiry)0, /*py2:nb_nonzero/py3:nb_bool*/
240 (unaryfunc)0, /*nb_invert*/
241 (binaryfunc)0, /*nb_lshift*/
242 (binaryfunc)0, /*nb_rshift*/
243 (binaryfunc)0, /*nb_and*/
244 (binaryfunc)0, /*nb_xor*/
245 (binaryfunc)0, /*nb_or*/
246 (unaryfunc)0, /*nb_int*/
247 0, /*py2:nb_long/py3:nb_reserved*/
248 (unaryfunc)0, /*nb_float*/
249 0, /*nb_inplace_add*/
250 0, /*nb_inplace_subtract*/
251 0, /*nb_inplace_multiply*/
252 0, /*nb_inplace_remainder*/
253 0, /*nb_inplace_power*/
254 0, /*nb_inplace_lshift*/
255 0, /*nb_inplace_rshift*/
256 0, /*nb_inplace_and*/
257 0, /*nb_inplace_xor*/
258 0, /*nb_inplace_or*/
259 (binaryfunc)0, /* nb_floor_divide */
260 0, /* nb_true_divide */
261 0, /* nb_inplace_floor_divide */
262 0, /* nb_inplace_true_divide */
263 (unaryfunc)0, /* nb_index */
264 };
265
266 static PySequenceMethods matrix_as_sequence = {
267 0, /* sq_length */
268 0, /* sq_concat */
269 0, /* sq_repeat */
270 (ssizeargfunc)matrix_item, /* sq_item */
271 0, /* sq_slice */
272 0, /* sq_ass_item */
273 0, /* sq_ass_slice */
274 0, /* sq_contains */
275 };
276
277 static PyMethodDef matrix_methods[] = {
278 /* Do not need to wrap all cairo_matrix_init_*() functions
279 * C API Matrix constructors Python equivalents
280 * cairo_matrix_init() cairo.Matrix(xx,yx,xy,yy,x0,y0)
281 * cairo_matrix_init_rotate() cairo.Matrix.init_rotate(radians)
282
283 * cairo_matrix_init_identity() cairo.Matrix()
284 * cairo_matrix_init_translate() cairo.Matrix(x0=x0,y0=y0)
285 * cairo_matrix_init_scale() cairo.Matrix(xx=xx,yy=yy)
286 */
287 {"init_rotate", (PyCFunction)matrix_init_rotate, METH_VARARGS | METH_CLASS },
288 {"invert", (PyCFunction)matrix_invert, METH_NOARGS },
289 {"multiply", (PyCFunction)matrix_multiply, METH_VARARGS },
290 {"rotate", (PyCFunction)matrix_rotate, METH_VARARGS },
291 {"scale", (PyCFunction)matrix_scale, METH_VARARGS },
292 {"transform_distance",(PyCFunction)matrix_transform_distance, METH_VARARGS },
293 {"transform_point", (PyCFunction)matrix_transform_point, METH_VARARGS },
294 {"translate", (PyCFunction)matrix_translate, METH_VARARGS },
295 {NULL, NULL, 0, NULL},
296 };
297
298 static PyMemberDef matrix_tp_members[] = {
299 {"xx", T_DOUBLE, sizeof(PyObject) + sizeof(double) * 0, 0,
300 "xx component of the affine transformation"},
301 {"yx", T_DOUBLE, sizeof(PyObject) + sizeof(double) * 1, 0,
302 "yx component of the affine transformation"},
303 {"xy", T_DOUBLE, sizeof(PyObject) + sizeof(double) * 2, 0,
304 "xy component of the affine transformation"},
305 {"yy", T_DOUBLE, sizeof(PyObject) + sizeof(double) * 3, 0,
306 "yy component of the affine transformation"},
307 {"x0", T_DOUBLE, sizeof(PyObject) + sizeof(double) * 4, 0,
308 "X translation component of the affine transformation"},
309 {"y0", T_DOUBLE, sizeof(PyObject) + sizeof(double) * 5, 0,
310 "Y translation component of the affine transformation"},
311 {NULL}
312 };
313
314 PyTypeObject PycairoMatrix_Type = {
315 PyVarObject_HEAD_INIT(NULL, 0)
316 "cairo.Matrix", /* tp_name */
317 sizeof(PycairoMatrix), /* tp_basicsize */
318 0, /* tp_itemsize */
319 (destructor)matrix_dealloc, /* tp_dealloc */
320 0, /* tp_print */
321 0, /* tp_getattr */
322 0, /* tp_setattr */
323 0, /* tp_compare */
324 (reprfunc)matrix_repr, /* tp_repr */
325 &matrix_as_number, /* tp_as_number */
326 &matrix_as_sequence, /* tp_as_sequence */
327 0, /* tp_as_mapping */
328 0, /* tp_hash */
329 0, /* tp_call */
330 0, /* tp_str */
331 0, /* tp_getattro */
332 0, /* tp_setattro */
333 0, /* tp_as_buffer */
334 Py_TPFLAGS_DEFAULT, /* tp_flags */
335 NULL, /* tp_doc */
336 0, /* tp_traverse */
337 0, /* tp_clear */
338 (richcmpfunc)matrix_richcmp, /* tp_richcompare */
339 0, /* tp_weaklistoffset */
340 0, /* tp_iter */
341 0, /* tp_iternext */
342 matrix_methods, /* tp_methods */
343 matrix_tp_members, /* tp_members */
344 0, /* tp_getset */
345 0, /* tp_base */
346 0, /* tp_dict */
347 0, /* tp_descr_get */
348 0, /* tp_descr_set */
349 0, /* tp_dictoffset */
350 0, /* tp_init */
351 0, /* tp_alloc */
352 (newfunc)matrix_new, /* tp_new */
353 0, /* tp_free */
354 0, /* tp_is_gc */
355 0, /* tp_bases */
356 };