| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
n |
| 6 |
|
| 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 |
#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; |
| 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 |
|
| 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 |
|
| 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, |
| 231 |
(binaryfunc)0, |
| 232 |
(binaryfunc)matrix_operator_multiply, |
| 233 |
(binaryfunc)0, |
| 234 |
(binaryfunc)0, |
| 235 |
(ternaryfunc)0, |
| 236 |
(unaryfunc)0, |
| 237 |
(unaryfunc)0, |
| 238 |
(unaryfunc)0, |
| 239 |
(inquiry)0, |
| 240 |
(unaryfunc)0, |
| 241 |
(binaryfunc)0, |
| 242 |
(binaryfunc)0, |
| 243 |
(binaryfunc)0, |
| 244 |
(binaryfunc)0, |
| 245 |
(binaryfunc)0, |
| 246 |
(unaryfunc)0, |
| 247 |
0, |
| 248 |
(unaryfunc)0, |
| 249 |
0, |
| 250 |
0, |
| 251 |
0, |
| 252 |
0, |
| 253 |
0, |
| 254 |
0, |
| 255 |
0, |
| 256 |
0, |
| 257 |
0, |
| 258 |
0, |
| 259 |
(binaryfunc)0, |
| 260 |
0, |
| 261 |
0, |
| 262 |
0, |
| 263 |
(unaryfunc)0, |
| 264 |
}; |
| 265 |
|
| 266 |
static PySequenceMethods matrix_as_sequence = { |
| 267 |
0, |
| 268 |
0, |
| 269 |
0, |
| 270 |
(ssizeargfunc)matrix_item, |
| 271 |
0, |
| 272 |
0, |
| 273 |
0, |
| 274 |
0, |
| 275 |
}; |
| 276 |
|
| 277 |
static PyMethodDef matrix_methods[] = { |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 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", |
| 317 |
sizeof(PycairoMatrix), |
| 318 |
0, |
| 319 |
(destructor)matrix_dealloc, |
| 320 |
0, |
| 321 |
0, |
| 322 |
0, |
| 323 |
0, |
| 324 |
(reprfunc)matrix_repr, |
| 325 |
&matrix_as_number, |
| 326 |
&matrix_as_sequence, |
| 327 |
0, |
| 328 |
0, |
| 329 |
0, |
| 330 |
0, |
| 331 |
0, |
| 332 |
0, |
| 333 |
0, |
| 334 |
Py_TPFLAGS_DEFAULT, |
| 335 |
NULL, |
| 336 |
0, |
| 337 |
0, |
| 338 |
(richcmpfunc)matrix_richcmp, |
| 339 |
0, |
| 340 |
0, |
| 341 |
0, |
| 342 |
matrix_methods, |
| 343 |
matrix_tp_members, |
| 344 |
0, |
| 345 |
0, |
| 346 |
0, |
| 347 |
0, |
| 348 |
0, |
| 349 |
0, |
| 350 |
0, |
| 351 |
0, |
| 352 |
(newfunc)matrix_new, |
| 353 |
0, |
| 354 |
0, |
| 355 |
0, |
| 356 |
}; |