-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvgu_module.cc
More file actions
432 lines (356 loc) · 11.4 KB
/
vgu_module.cc
File metadata and controls
432 lines (356 loc) · 11.4 KB
1
2
3
4
5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
* Copyright (c) 2012 Dan Eicher
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library in the file COPYING;
* if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "openvg_module.h"
#include "vgu.h"
/* --- module functions --- */
static void vgu_error(VGUErrorCode error)
{
switch (error) {
case VGU_NO_ERROR:
break;
case VGU_BAD_HANDLE_ERROR:
PyErr_SetString(PyExc_TypeError, "VGU_BAD_HANDLE_ERROR");
break;
case VGU_ILLEGAL_ARGUMENT_ERROR:
PyErr_SetString(PyExc_TypeError, "VGU_ILLEGAL_ARGUMENT_ERROR");
break;
case VGU_OUT_OF_MEMORY_ERROR:
PyErr_SetString(PyExc_MemoryError, "VGU_OUT_OF_MEMORY_ERROR");
break;
case VGU_PATH_CAPABILITY_ERROR:
PyErr_SetString(PyExc_ValueError, "VGU_PATH_CAPABILITY_ERROR");
break;
case VGU_BAD_WARP_ERROR:
PyErr_SetString(PyExc_ValueError, "VGU_BAD_WARP_ERROR");
break;
default:
break;
}
}
static PyObject *
VGU_vguArc(PyObject * UNUSED(dummy), PyObject *args, PyObject *kwargs)
{
VGUErrorCode error;
PyVGPath *path;
VGfloat x, y, width, height;
VGfloat startAngle, angleExtent;
VGUArcType arcType;
const char *keywords[] = {"path", "x", "y", "width", "height",
"startAngle", "angleExtent", "arcType", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "O!ffffffi", (char **) keywords, &PyVGPath_Type, &path, &x, &y, &width, &height, &startAngle, &angleExtent, &arcType)) {
return NULL;
}
error = vguArc(path->obj, x, y, width, height, startAngle, angleExtent, arcType);
if (error) {
vgu_error(error);
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
VGU_vguLine(PyObject * UNUSED(dummy), PyObject *args, PyObject *kwargs)
{
VGUErrorCode error;
PyVGPath *path;
VGfloat x0, y0, x1, y1;
const char *keywords[] = {"path", "x0", "y0", "x1", "y1", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "O!ffff", (char **) keywords, &PyVGPath_Type, &path, &x0, &y0, &x1, &y1)) {
return NULL;
}
error = vguLine(path->obj, x0, y0, x1, y1);
if (error) {
vgu_error(error);
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
VGU_vguPolygon(PyObject * UNUSED(dummy), PyObject *args, PyObject *kwargs)
{
VGUErrorCode error;
PyVGPath *path;
VGfloat *points;
PyObject *py_list;
VGint count;
VGboolean closed;
PyObject *py_closed;
int idx;
const char *keywords[] = {"path", "points", "closed", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "O!O!O", (char **) keywords, &PyVGPath_Type, &path, &PyList_Type, &py_list, &count, &py_closed)) {
return NULL;
}
closed = (VGboolean) PyObject_IsTrue(py_closed);
count = PyList_Size(py_list);
if (count == 0 || count%2) {
PyErr_SetString(PyExc_TypeError,
"Parameter `points' must be a multiple of 2");
return NULL;
}
points = (VGfloat*)malloc(sizeof(VGfloat) * count);
for (idx = 0; idx < count; idx++) {
PyObject *element = PyList_GET_ITEM(py_list, idx);
if (!PyFloat_Check(element)) {
PyErr_SetString(PyExc_TypeError,
"Parameter `points' must be a list of floats");
free(points);
return NULL;
}
points[idx] = (VGfloat) PyFloat_AsDouble(element);
}
error = vguPolygon(path->obj, points, count/2, closed);
free(points);
if (error) {
vgu_error(error);
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
VGU_vguRoundRect(PyObject * UNUSED(dummy), PyObject *args, PyObject *kwargs)
{
VGUErrorCode error;
PyVGPath *path;
VGfloat x, y, width, height;
VGfloat arcWidth, arcHeight;
const char *keywords[] = {"path", "x", "y", "width", "height",
"arcWidth", "arcHeight", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "O!ffffff", (char **) keywords, &PyVGPath_Type, &path, &x, &y, &width, &height, &arcWidth, &arcHeight)) {
return NULL;
}
error = vguRoundRect(path->obj, x, y, width, height, arcWidth, arcHeight);
if (error) {
vgu_error(error);
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
VGU_vguEllipse(PyObject * UNUSED(dummy), PyObject *args, PyObject *kwargs)
{
VGUErrorCode error;
PyVGPath *path;
VGfloat cx, cy, width, height;
const char *keywords[] = {"path", "cx", "cy", "width", "height", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "O!ffff", (char **) keywords, &PyVGPath_Type, &path, &cx, &cy, &width, &height)) {
return NULL;
}
error = vguEllipse(path->obj, cx, cy, width, height);
if (error) {
vgu_error(error);
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
VGU_vguRect(PyObject * UNUSED(dummy), PyObject *args, PyObject *kwargs)
{
VGUErrorCode error;
PyVGPath *path;
VGfloat x, y, width, height;
const char *keywords[] = {"path", "x", "y", "width", "height", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "O!ffff", (char **) keywords, &PyVGPath_Type, &path, &x, &y, &width, &height)) {
return NULL;
}
error = vguRect(path->obj, x, y, width, height);
if (error) {
vgu_error(error);
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *
VGU_vguComputeWarpQuadToSquare(PyObject * UNUSED(dummy), PyObject *args, PyObject *kwargs)
{
VGUErrorCode error;
VGfloat sx0, sy0, sx1, sy1, sx2, sy2, sx3, sy3;
VGfloat matrix[9];
PyObject *py_list;
int idx;
const char *keywords[] = {"sx0", "sy0", "sx1", "sy1",
"sx2", "sy2", "sx3", "sy3", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "ffffffff", (char **) keywords, &sx0, &sy0, &sx1, &sy1, &sx2, &sy2, &sx3, &sy3)) {
return NULL;
}
error = vguComputeWarpQuadToSquare(sx0, sy0, sx1, sy1, sx2, sy2, sx3, sy3, matrix);
if (error) {
vgu_error(error);
return NULL;
}
py_list = PyList_New(9);
for (idx = 0; idx < 9; idx++) {
PyList_SET_ITEM(py_list, idx, PyFloat_FromDouble(matrix[idx]));
}
return Py_BuildValue((char *) "N", py_list);
}
static PyObject *
VGU_vguComputeWarpQuadToQuad(PyObject * UNUSED(dummy), PyObject *args, PyObject *kwargs)
{
VGUErrorCode error;
VGfloat dx0, dy0, dx1, dy1, dx2, dy2, dx3, dy3;
VGfloat sx0, sy0, sx1, sy1, sx2, sy2, sx3, sy3;
VGfloat matrix[9];
PyObject *py_list;
int idx;
const char *keywords[] = {"dx0", "dy0", "dx1", "dy1",
"dx2", "dy2", "dx3", "dy3",
"sx0", "sy0", "sx1", "sy1",
"sx2", "sy2", "sx3", "sy3", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "ffffffffffffffff", (char **) keywords, &dx0, &dy0, &dx1, &dy1, &dx2, &dy2, &dx3, &dy3, &sx0, &sy0, &sx1, &sy1, &sx2, &sy2, &sx3, &sy3)) {
return NULL;
}
error = vguComputeWarpQuadToQuad(dx0, dy0, dx1, dy1, dx2, dy2, dx3, dy3, sx0, sy0, sx1, sy1, sx2, sy2, sx3, sy3, matrix);
if (error) {
vgu_error(error);
return NULL;
}
py_list = PyList_New(9);
for (idx = 0; idx < 9; idx++) {
PyList_SET_ITEM(py_list, idx, PyFloat_FromDouble(matrix[idx]));
}
return Py_BuildValue((char *) "N", py_list);
}
static PyObject *
VGU_vguComputeWarpSquareToQuad(PyObject * UNUSED(dummy), PyObject *args, PyObject *kwargs)
{
VGUErrorCode error;
VGfloat dx0, dy0, dx1, dy1, dx2, dy2, dx3, dy3;
VGfloat matrix[9];
PyObject *py_list;
int idx;
const char *keywords[] = {"dx0", "dy0", "dx1", "dy1", "dx2", "dy2",
"dx3", "dy3", "matrix", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "ffffffff", (char **) keywords, &dx0, &dy0, &dx1, &dy1, &dx2, &dy2, &dx3, &dy3)) {
return NULL;
}
error = vguComputeWarpSquareToQuad(dx0, dy0, dx1, dy1, dx2, dy2, dx3, dy3, matrix);
if (error) {
vgu_error(error);
return NULL;
}
py_list = PyList_New(9);
for (idx = 0; idx < 9; idx++) {
PyList_SET_ITEM(py_list, idx, PyFloat_FromDouble(matrix[idx]));
}
return Py_BuildValue((char *) "N", py_list);
}
static PyMethodDef VGU_functions[] = {
{(char *) "arc",
(PyCFunction) VGU_vguArc,
METH_KEYWORDS|METH_VARARGS,
NULL
},
{(char *) "line",
(PyCFunction) VGU_vguLine,
METH_KEYWORDS|METH_VARARGS,
NULL
},
{(char *) "ellipse",
(PyCFunction) VGU_vguEllipse,
METH_KEYWORDS|METH_VARARGS,
NULL
},
{(char *) "polygon",
(PyCFunction) VGU_vguPolygon,
METH_KEYWORDS|METH_VARARGS,
NULL
},
{(char *) "rect",
(PyCFunction) VGU_vguRect,
METH_KEYWORDS|METH_VARARGS,
NULL
},
{(char *) "round_rect",
(PyCFunction) VGU_vguRoundRect,
METH_KEYWORDS|METH_VARARGS,
NULL
},
{(char *) "compute_warp_square_to_quad",
(PyCFunction) VGU_vguComputeWarpSquareToQuad,
METH_KEYWORDS|METH_VARARGS,
NULL
},
{(char *) "compute_warp_quad_to_sphere",
(PyCFunction) VGU_vguComputeWarpQuadToSquare,
METH_KEYWORDS|METH_VARARGS,
NULL
},
{(char *) "compute_warp_quad_to_quad",
(PyCFunction) VGU_vguComputeWarpQuadToQuad,
METH_KEYWORDS|METH_VARARGS,
NULL
},
{NULL, NULL, 0, NULL}
};
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef VGU_moduledef = {
PyModuleDef_HEAD_INIT,
"VGU",
NULL,
-1,
VGU_functions,
};
#endif
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef VGUArcType_moduledef = {
PyModuleDef_HEAD_INIT,
"OpenVG.VGUArcType",
NULL,
-1,
NULL,
};
#endif
static PyObject *
initVGU_VGUArcType(void)
{
PyObject *m;
#if PY_VERSION_HEX >= 0x03000000
m = PyModule_Create(&VGUArcType_moduledef);
#else
m = Py_InitModule3((char *) "OpenVG.VGUArcType", NULL, NULL);
#endif
if (m == NULL) {
return NULL;
}
PyModule_AddIntConstant(m, (char *) "VGU_ARC_OPEN", VGU_ARC_OPEN);
PyModule_AddIntConstant(m, (char *) "VGU_ARC_CHORD", VGU_ARC_CHORD);
PyModule_AddIntConstant(m, (char *) "VGU_ARC_PIE", VGU_ARC_PIE);
return m;
}
PyObject *
initVGU(void)
{
PyObject *m;
PyObject *submodule;
#if PY_VERSION_HEX >= 0x03000000
m = PyModule_Create(&VGU_moduledef);
#else
m = Py_InitModule3((char *) "VGU", VGU_functions, NULL);
#endif
if (m == NULL) {
return NULL;
}
submodule = initVGU_VGUArcType();
if (submodule == NULL) {
return NULL;
}
Py_INCREF(submodule);
PyModule_AddObject(m, (char *) "VGUArcType", submodule);
return m;
}