| 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 |
|
| 34 |
#include "private.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
PyObject * |
| 42 |
PycairoPath_FromPath (cairo_path_t *path) { |
| 43 |
PyObject *o; |
| 44 |
|
| 45 |
assert (path != NULL); |
| 46 |
|
| 47 |
if (Pycairo_Check_Status (path->status)) { |
| 48 |
cairo_path_destroy (path); |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
|
| 52 |
o = PycairoPath_Type.tp_alloc (&PycairoPath_Type, 0); |
| 53 |
if (o) |
| 54 |
((PycairoPath *)o)->path = path; |
| 55 |
else |
| 56 |
cairo_path_destroy (path); |
| 57 |
return o; |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
path_dealloc(PycairoPath *p) { |
| 62 |
if (p->path) { |
| 63 |
cairo_path_destroy(p->path); |
| 64 |
p->path = NULL; |
| 65 |
} |
| 66 |
Py_TYPE(p)->tp_free(p); |
| 67 |
} |
| 68 |
|
| 69 |
static PyObject * |
| 70 |
path_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 71 |
return type->tp_alloc(type, 0); |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
static int |
| 76 |
path_init(PycairoPath *p, PyObject *args, PyObject *kwds) { |
| 77 |
PyErr_SetString(PyExc_TypeError, "The Path type cannot be instantiated, " |
| 78 |
"use Context.copy_path()"); |
| 79 |
return -1; |
| 80 |
} |
| 81 |
|
| 82 |
static PyObject * |
| 83 |
path_str(PycairoPath *p) { |
| 84 |
PyObject *s, *pieces = NULL, *result = NULL; |
| 85 |
cairo_path_t *path = p->path; |
| 86 |
cairo_path_data_t *data; |
| 87 |
int i, ret; |
| 88 |
char buf[80]; |
| 89 |
|
| 90 |
pieces = PyList_New(0); |
| 91 |
if (pieces == NULL) |
| 92 |
goto Done; |
| 93 |
|
| 94 |
|
| 95 |
for (i=0; i < path->num_data; i += path->data[i].header.length) { |
| 96 |
data = &path->data[i]; |
| 97 |
switch (data->header.type) { |
| 98 |
|
| 99 |
case CAIRO_PATH_MOVE_TO: |
| 100 |
PyOS_snprintf(buf, sizeof(buf), "move_to %f %f", |
| 101 |
data[1].point.x, data[1].point.y); |
| 102 |
s = PyUnicode_FromString (buf); |
| 103 |
if (!s) |
| 104 |
goto Done; |
| 105 |
ret = PyList_Append(pieces, s); |
| 106 |
Py_DECREF(s); |
| 107 |
if (ret < 0) |
| 108 |
goto Done; |
| 109 |
break; |
| 110 |
|
| 111 |
case CAIRO_PATH_LINE_TO: |
| 112 |
PyOS_snprintf(buf, sizeof(buf), "line_to %f %f", |
| 113 |
data[1].point.x, data[1].point.y); |
| 114 |
s = PyUnicode_FromString (buf); |
| 115 |
if (!s) |
| 116 |
goto Done; |
| 117 |
ret = PyList_Append(pieces, s); |
| 118 |
Py_DECREF(s); |
| 119 |
if (ret < 0) |
| 120 |
goto Done; |
| 121 |
break; |
| 122 |
|
| 123 |
case CAIRO_PATH_CURVE_TO: |
| 124 |
PyOS_snprintf(buf, sizeof(buf), "curve_to %f %f %f %f %f %f", |
| 125 |
data[1].point.x, data[1].point.y, |
| 126 |
data[2].point.x, data[2].point.y, |
| 127 |
data[3].point.x, data[3].point.y); |
| 128 |
s = PyUnicode_FromString (buf); |
| 129 |
if (!s) |
| 130 |
goto Done; |
| 131 |
ret = PyList_Append(pieces, s); |
| 132 |
Py_DECREF(s); |
| 133 |
if (ret < 0) |
| 134 |
goto Done; |
| 135 |
break; |
| 136 |
|
| 137 |
case CAIRO_PATH_CLOSE_PATH: |
| 138 |
s = PyUnicode_FromString ("close path"); |
| 139 |
if (!s) |
| 140 |
goto Done; |
| 141 |
ret = PyList_Append(pieces, s); |
| 142 |
Py_DECREF(s); |
| 143 |
if (ret < 0) |
| 144 |
goto Done; |
| 145 |
break; |
| 146 |
default: |
| 147 |
PyErr_SetString(PyExc_RuntimeError, "unknown CAIRO_PATH type"); |
| 148 |
goto Done; |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
s = PyUnicode_FromString ("\n"); |
| 154 |
if (s == NULL) |
| 155 |
goto Done; |
| 156 |
result = PyUnicode_Join (s, pieces); |
| 157 |
Py_DECREF(s); |
| 158 |
|
| 159 |
Done: |
| 160 |
Py_XDECREF(pieces); |
| 161 |
return result; |
| 162 |
} |
| 163 |
|
| 164 |
static PyObject * path_iter(PyObject *seq); |
| 165 |
|
| 166 |
|
| 167 |
static PyObject* |
| 168 |
path_richcompare (PyObject *self, PyObject *other, int op) |
| 169 |
{ |
| 170 |
if (Py_TYPE(self) == Py_TYPE(other)) |
| 171 |
return Pycairo_richcompare ( |
| 172 |
((PycairoPath *)self)->path, |
| 173 |
((PycairoPath *)other)->path, |
| 174 |
op); |
| 175 |
else { |
| 176 |
Py_INCREF(Py_NotImplemented); |
| 177 |
return Py_NotImplemented; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
static Py_hash_t |
| 182 |
path_hash (PyObject *self) |
| 183 |
{ |
| 184 |
return PYCAIRO_Py_hash_t_FromVoidPtr (((PycairoPath *)self)->path); |
| 185 |
} |
| 186 |
|
| 187 |
PyTypeObject PycairoPath_Type = { |
| 188 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 189 |
"cairo.Path", |
| 190 |
sizeof(PycairoPath), |
| 191 |
0, |
| 192 |
(destructor)path_dealloc, |
| 193 |
0, |
| 194 |
0, |
| 195 |
0, |
| 196 |
0, |
| 197 |
0, |
| 198 |
0, |
| 199 |
0, |
| 200 |
0, |
| 201 |
path_hash, |
| 202 |
0, |
| 203 |
(reprfunc)path_str, |
| 204 |
0, |
| 205 |
0, |
| 206 |
0, |
| 207 |
Py_TPFLAGS_DEFAULT, |
| 208 |
0, |
| 209 |
0, |
| 210 |
0, |
| 211 |
path_richcompare, |
| 212 |
0, |
| 213 |
(getiterfunc)path_iter, |
| 214 |
0, |
| 215 |
0, |
| 216 |
0, |
| 217 |
0, |
| 218 |
0, |
| 219 |
0, |
| 220 |
0, |
| 221 |
0, |
| 222 |
0, |
| 223 |
(initproc)path_init, |
| 224 |
0, |
| 225 |
(newfunc)path_new, |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
typedef struct { |
| 231 |
PyObject_HEAD |
| 232 |
int index; |
| 233 |
PycairoPath *pypath; |
| 234 |
} PycairoPathiter; |
| 235 |
|
| 236 |
PyTypeObject PycairoPathiter_Type; |
| 237 |
|
| 238 |
|
| 239 |
static void |
| 240 |
pathiter_dealloc(PycairoPathiter *it) { |
| 241 |
Py_XDECREF(it->pypath); |
| 242 |
PyObject_Del(it); |
| 243 |
} |
| 244 |
|
| 245 |
static PyObject * |
| 246 |
path_iter(PyObject *pypath) { |
| 247 |
PycairoPathiter *it; |
| 248 |
|
| 249 |
if (!PyObject_TypeCheck (pypath, &PycairoPath_Type)) { |
| 250 |
PyErr_BadInternalCall(); |
| 251 |
return NULL; |
| 252 |
} |
| 253 |
it = PyObject_New(PycairoPathiter, &PycairoPathiter_Type); |
| 254 |
if (it == NULL) |
| 255 |
return NULL; |
| 256 |
|
| 257 |
it->index = 0; |
| 258 |
Py_INCREF(pypath); |
| 259 |
it->pypath = (PycairoPath *)pypath; |
| 260 |
return (PyObject *) it; |
| 261 |
} |
| 262 |
|
| 263 |
static PyObject * |
| 264 |
pathiter_next(PycairoPathiter *it) { |
| 265 |
PycairoPath *pypath; |
| 266 |
cairo_path_t *path; |
| 267 |
|
| 268 |
assert(it != NULL); |
| 269 |
pypath = it->pypath; |
| 270 |
if (pypath == NULL) |
| 271 |
return NULL; |
| 272 |
assert (PyObject_TypeCheck (pypath, &PycairoPath_Type)); |
| 273 |
path = pypath->path; |
| 274 |
|
| 275 |
|
| 276 |
if (it->index < path->num_data) { |
| 277 |
cairo_path_data_t *data = &path->data[it->index]; |
| 278 |
cairo_path_data_type_t type = data->header.type; |
| 279 |
|
| 280 |
it->index += data[0].header.length; |
| 281 |
|
| 282 |
switch (type) { |
| 283 |
case CAIRO_PATH_MOVE_TO: |
| 284 |
case CAIRO_PATH_LINE_TO: |
| 285 |
return Py_BuildValue("(i(dd))", (int)type, |
| 286 |
data[1].point.x, data[1].point.y); |
| 287 |
case CAIRO_PATH_CURVE_TO: |
| 288 |
return Py_BuildValue("(i(dddddd))", (int)type, |
| 289 |
data[1].point.x, data[1].point.y, |
| 290 |
data[2].point.x, data[2].point.y, |
| 291 |
data[3].point.x, data[3].point.y); |
| 292 |
case CAIRO_PATH_CLOSE_PATH: |
| 293 |
return Py_BuildValue("i()", (int)type); |
| 294 |
default: |
| 295 |
PyErr_SetString(PyExc_RuntimeError, "unknown CAIRO_PATH type"); |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
Py_DECREF(pypath); |
| 302 |
it->pypath = NULL; |
| 303 |
return NULL; |
| 304 |
} |
| 305 |
|
| 306 |
PyTypeObject PycairoPathiter_Type = { |
| 307 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 308 |
"cairo.Pathiter", |
| 309 |
sizeof(PycairoPathiter), |
| 310 |
0, |
| 311 |
(destructor)pathiter_dealloc, |
| 312 |
0, |
| 313 |
0, |
| 314 |
0, |
| 315 |
0, |
| 316 |
0, |
| 317 |
0, |
| 318 |
0, |
| 319 |
0, |
| 320 |
0, |
| 321 |
0, |
| 322 |
0, |
| 323 |
0, |
| 324 |
0, |
| 325 |
0, |
| 326 |
Py_TPFLAGS_DEFAULT, |
| 327 |
0, |
| 328 |
0, |
| 329 |
0, |
| 330 |
0, |
| 331 |
0, |
| 332 |
0, |
| 333 |
(iternextfunc)pathiter_next, |
| 334 |
0, |
| 335 |
}; |