Debian Codegraph @

cairo/glyph.c

raw symbols SCIP
1 /* -*- mode: C; c-basic-offset: 2 -*-
2 *
3 * Pycairo - Python bindings for cairo
4 *
5 * Copyright © 2017 Christoph Reiter
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 /* read a Python sequence of (i,x,y) sequences
37 * return cairo_glyph_t *
38 * num_glyphs
39 * must call PyMem_Free(glyphs) when finished using the glyphs
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 /* 0 on success */
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", /* tp_name */
193 sizeof(PycairGlyph), /* tp_basicsize */
194 0, /* tp_itemsize */
195 0, /* tp_dealloc */
196 0, /* tp_print */
197 0, /* tp_getattr */
198 0, /* tp_setattr */
199 0, /* tp_compare */
200 (reprfunc)glyph_repr, /* tp_repr */
201 0, /* tp_as_number */
202 0, /* tp_as_sequence */
203 0, /* tp_as_mapping */
204 0, /* tp_hash */
205 0, /* tp_call */
206 0, /* tp_str */
207 0, /* tp_getattro */
208 0, /* tp_setattro */
209 0, /* tp_as_buffer */
210 Py_TPFLAGS_DEFAULT, /* tp_flags */
211 0, /* tp_doc */
212 0, /* tp_traverse */
213 0, /* tp_clear */
214 0, /* tp_richcompare */
215 0, /* tp_weaklistoffset */
216 0, /* tp_iter */
217 0, /* tp_iternext */
218 0, /* tp_methods */
219 0, /* tp_members */
220 glyph_getset, /* tp_getset */
221 0, /* tp_base */
222 0, /* tp_dict */
223 0, /* tp_descr_get */
224 0, /* tp_descr_set */
225 0, /* tp_dictoffset */
226 0, /* tp_init */
227 0, /* tp_alloc */
228 (newfunc)glyph_new, /* tp_new */
229 0, /* tp_free */
230 0, /* tp_is_gc */
231 0, /* tp_bases */
232 };