| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
r |
| 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 |
cairo_glyph_t * |
| 42 |
_PycairoGlyphs_AsGlyphs (PyObject *py_object, int *num_glyphs) |
| 43 |
{ |
| 44 |
Py_ssize_t length, i; |
| 45 |
cairo_glyph_t *glyphs = NULL, *glyph; |
| 46 |
PyObject *py_glyphs, *py_seq = NULL; |
| 47 |
|
| 48 |
py_glyphs = PySequence_Fast (py_object, "glyphs must be a sequence"); |
| 49 |
if (py_glyphs == NULL) |
| 50 |
return NULL; |
| 51 |
|
| 52 |
length = PySequence_Fast_GET_SIZE (py_glyphs); |
| 53 |
if (length > INT_MAX) { |
| 54 |
Py_DECREF (py_glyphs); |
| 55 |
PyErr_SetString (PyExc_ValueError, "sequence too large"); |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
if (*num_glyphs < 0 || *num_glyphs > length) |
| 60 |
*num_glyphs = (int)length; |
| 61 |
|
| 62 |
glyphs = PyMem_Malloc ((unsigned int)*num_glyphs * sizeof(cairo_glyph_t)); |
| 63 |
if (glyphs == NULL) { |
| 64 |
PyErr_NoMemory(); |
| 65 |
goto error; |
| 66 |
} |
| 67 |
for (i = 0, glyph = glyphs; i < *num_glyphs; i++, glyph++) { |
| 68 |
long index; |
| 69 |
PyObject *py_item = PySequence_Fast_GET_ITEM (py_glyphs, i); |
| 70 |
py_seq = PySequence_Fast (py_item, "glyph items must be a sequence"); |
| 71 |
if (py_seq == NULL) |
| 72 |
goto error; |
| 73 |
if (PySequence_Fast_GET_SIZE (py_seq) != 3) { |
| 74 |
PyErr_SetString (PyExc_ValueError, |
| 75 |
"each glyph item must be an (i,x,y) sequence"); |
| 76 |
goto error; |
| 77 |
} |
| 78 |
index = PyLong_AsLong (PySequence_Fast_GET_ITEM (py_seq, 0)); |
| 79 |
if (PyErr_Occurred ()) |
| 80 |
goto error; |
| 81 |
glyph->index = (unsigned long)index; |
| 82 |
glyph->x = PyFloat_AsDouble (PySequence_Fast_GET_ITEM (py_seq, 1)); |
| 83 |
glyph->y = PyFloat_AsDouble (PySequence_Fast_GET_ITEM (py_seq, 2)); |
| 84 |
if (PyErr_Occurred()) |
| 85 |
goto error; |
| 86 |
Py_DECREF (py_seq); |
| 87 |
} |
| 88 |
|
| 89 |
Py_DECREF (py_glyphs); |
| 90 |
return glyphs; |
| 91 |
|
| 92 |
error: |
| 93 |
Py_DECREF (py_glyphs); |
| 94 |
Py_XDECREF (py_seq); |
| 95 |
PyMem_Free (glyphs); |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
int |
| 101 |
_PyGlyph_AsGlyph (PyObject *pyobj, cairo_glyph_t *glyph) { |
| 102 |
long index; |
| 103 |
|
| 104 |
if (!PyObject_TypeCheck (pyobj, &PycairoGlyph_Type)) { |
| 105 |
PyErr_SetString (PyExc_TypeError, "item must be of type cairo.Glyph"); |
| 106 |
return -1; |
| 107 |
} |
| 108 |
|
| 109 |
index = PyLong_AsLong (PySequence_Fast_GET_ITEM (pyobj, 0)); |
| 110 |
if (PyErr_Occurred ()) |
| 111 |
return -1; |
| 112 |
if (index < 0) { |
| 113 |
PyErr_SetString (PyExc_ValueError, "negative index"); |
| 114 |
return -1; |
| 115 |
} |
| 116 |
glyph->index = (unsigned long) index; |
| 117 |
glyph->x = PyFloat_AsDouble (PySequence_Fast_GET_ITEM (pyobj, 1)); |
| 118 |
glyph->y = PyFloat_AsDouble (PySequence_Fast_GET_ITEM (pyobj, 2)); |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
static char *KWDS[] = {"index", "x", "y", NULL}; |
| 123 |
|
| 124 |
static PyObject * |
| 125 |
glyph_new (PyTypeObject *type, PyObject *args, PyObject *kwds) { |
| 126 |
double x, y; |
| 127 |
unsigned long index; |
| 128 |
PyObject *pyindex, *tuple_args, *result; |
| 129 |
|
| 130 |
if (!PyArg_ParseTupleAndKeywords (args, kwds, "Odd:Glyph.__new__", |
| 131 |
KWDS, &pyindex, &x, &y)) |
| 132 |
return NULL; |
| 133 |
|
| 134 |
if (_conv_pyobject_to_ulong (pyindex, &index) < 0) |
| 135 |
return NULL; |
| 136 |
|
| 137 |
tuple_args = Py_BuildValue ("((kdd))", index, x, y); |
| 138 |
if (tuple_args == NULL) |
| 139 |
return NULL; |
| 140 |
result = PyTuple_Type.tp_new (type, tuple_args, NULL); |
| 141 |
Py_DECREF (tuple_args); |
| 142 |
return result; |
| 143 |
} |
| 144 |
|
| 145 |
static PyObject* |
| 146 |
glyph_repr(PyObject *self) { |
| 147 |
PyObject *format, *result; |
| 148 |
|
| 149 |
format = PyUnicode_FromString ( |
| 150 |
"cairo.Glyph(index=%r, x=%r, y=%r)"); |
| 151 |
if (format == NULL) |
| 152 |
return NULL; |
| 153 |
result = PyUnicode_Format (format, self); |
| 154 |
Py_DECREF (format); |
| 155 |
return result; |
| 156 |
} |
| 157 |
|
| 158 |
static PyObject * |
| 159 |
glyph_get_index(PyObject *self, void *closure) |
| 160 |
{ |
| 161 |
PyObject *obj = PyTuple_GetItem (self, 0); |
| 162 |
Py_XINCREF (obj); |
| 163 |
return obj; |
| 164 |
} |
| 165 |
|
| 166 |
static PyObject * |
| 167 |
glyph_get_x(PyObject *self, void *closure) |
| 168 |
{ |
| 169 |
PyObject *obj = PyTuple_GetItem (self, 1); |
| 170 |
Py_XINCREF (obj); |
| 171 |
return obj; |
| 172 |
} |
| 173 |
|
| 174 |
static PyObject * |
| 175 |
glyph_get_y(PyObject *self, void *closure) |
| 176 |
{ |
| 177 |
PyObject *obj = PyTuple_GetItem (self, 2); |
| 178 |
Py_XINCREF (obj); |
| 179 |
return obj; |
| 180 |
} |
| 181 |
|
| 182 |
static PyGetSetDef glyph_getset[] = { |
| 183 |
{"index", (getter)glyph_get_index}, |
| 184 |
{"x", (getter)glyph_get_x}, |
| 185 |
{"y", (getter)glyph_get_y}, |
| 186 |
{NULL,}, |
| 187 |
}; |
| 188 |
|
| 189 |
|
| 190 |
PyTypeObject PycairoGlyph_Type = { |
| 191 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 192 |
"cairo.Glyph", |
| 193 |
sizeof(PycairGlyph), |
| 194 |
0, |
| 195 |
0, |
| 196 |
0, |
| 197 |
0, |
| 198 |
0, |
| 199 |
0, |
| 200 |
(reprfunc)glyph_repr, |
| 201 |
0, |
| 202 |
0, |
| 203 |
0, |
| 204 |
0, |
| 205 |
0, |
| 206 |
0, |
| 207 |
0, |
| 208 |
0, |
| 209 |
0, |
| 210 |
Py_TPFLAGS_DEFAULT, |
| 211 |
0, |
| 212 |
0, |
| 213 |
0, |
| 214 |
0, |
| 215 |
0, |
| 216 |
0, |
| 217 |
0, |
| 218 |
0, |
| 219 |
0, |
| 220 |
glyph_getset, |
| 221 |
0, |
| 222 |
0, |
| 223 |
0, |
| 224 |
0, |
| 225 |
0, |
| 226 |
0, |
| 227 |
0, |
| 228 |
(newfunc)glyph_new, |
| 229 |
0, |
| 230 |
0, |
| 231 |
0, |
| 232 |
}; |