3 Python wrapper for librfxswf- primitive objects (implementation)
5 Part of the swftools package.
7 Copyright (c) 2003 Matthias Kramm <kramm@quiss.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
25 #include "../rfxswf.h"
27 #include "./pyutils.h"
28 #include "primitives.h"
30 //----------------------------------------------------------------------------
36 PyObject* f_Color(PyObject* self, PyObject* args, PyObject* kwargs)
38 static char *kwlist[] = {"r", "g", "b", "a", NULL};
40 int r=0,g=0,b=0,a=255;
41 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii|i", kwlist, &r,&g,&b,&a))
43 color = PyObject_New(ColorObject, &ColorClass);
44 mylog("+%08x(%d) color_new(%d,%d,%d,%d)\n", (int)color, color->ob_refcnt, r,g,b,a);
49 return (PyObject*)color;
51 static PyObject* color_getattr(PyObject * self, char* a)
53 ColorObject*color = (ColorObject*)self;
55 return Py_BuildValue("r", color->rgba.r);
56 } else if(!strcmp(a, "g")) {
57 return Py_BuildValue("g", color->rgba.g);
58 } else if(!strcmp(a, "b")) {
59 return Py_BuildValue("b", color->rgba.b);
60 } else if(!strcmp(a, "a")) {
61 return Py_BuildValue("a", color->rgba.a);
65 static int color_setattr(PyObject * self, char* attr, PyObject* o)
67 ColorObject*color = (ColorObject*)self;
68 if(!strcmp(attr, "r")) {
69 if (!PyArg_Parse(o, "d", &color->rgba.r)) goto err;
71 } else if(!strcmp(attr, "g")) {
72 if (!PyArg_Parse(o, "d", &color->rgba.g)) goto err;
74 } else if(!strcmp(attr, "b")) {
75 if (!PyArg_Parse(o, "d", &color->rgba.b)) goto err;
77 } else if(!strcmp(attr, "a")) {
78 if (!PyArg_Parse(o, "d", &color->rgba.a)) goto err;
82 mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, attr, o);
85 RGBA color_getRGBA(PyObject*self)
87 ColorObject*color = 0;
88 if (!PyArg_Parse(self, "O!", &ColorClass, &color)) {
90 memset(&dummy, 0, sizeof(dummy));
91 mylog("Error: wrong type for function color_getRGBA");
96 void color_dealloc(PyObject* self)
98 mylog("-%08x(%d) color_dealloc\n", (int)self, self->ob_refcnt);
101 PyTypeObject ColorClass =
103 PyObject_HEAD_INIT(NULL)
106 tp_basicsize: sizeof(ColorObject),
108 tp_dealloc: color_dealloc,
110 tp_getattr: color_getattr,
111 tp_setattr: color_setattr,
113 //----------------------------------------------------------------------------
119 PyObject* f_BBox(PyObject* self, PyObject* args, PyObject* kwargs)
121 static char *kwlist[] = {"xmin", "ymin", "xmax", "ymax", NULL};
123 float xmin,ymin,xmax,ymax;
125 if (!PyArg_ParseTuple(args, "ffff", &xmin, &ymin, &xmax, &ymax))
128 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ffff", kwlist, &xmin, &ymin, &xmax, &ymax))
132 box.xmin = (int)(xmin*20);
133 box.ymin = (int)(ymin*20);
134 box.xmax = (int)(xmax*20);
135 box.ymax = (int)(ymax*20);
136 mylog("+%08x(%d) bbox_new(%d,%d,%d,%d)\n", (int)self, self?self->ob_refcnt:0, box.xmin, box.ymin, box.xmax,box.ymax);
137 bbox = PyObject_New(BBoxObject, &BBoxClass);
139 return (PyObject*)bbox;
141 static PyObject* bbox_getattr(PyObject * self, char* a)
143 BBoxObject*bbox = (BBoxObject*)self;
144 if(!strcmp(a, "xmin")) {
145 return Py_BuildValue("f", bbox->bbox.xmin/20.0);
146 } else if(!strcmp(a, "ymin")) {
147 return Py_BuildValue("f", bbox->bbox.ymin/20.0);
148 } else if(!strcmp(a, "xmax")) {
149 return Py_BuildValue("f", bbox->bbox.xmax/20.0);
150 } else if(!strcmp(a, "ymax")) {
151 return Py_BuildValue("f", bbox->bbox.ymax/20.0);
155 static int bbox_setattr(PyObject * self, char* a, PyObject* o)
157 BBoxObject*bbox= (BBoxObject*)self;
158 if(!strcmp(a, "xmin")) {
160 if (!PyArg_Parse(o, "i", &xmin)) goto err;
161 bbox->bbox.xmin = (int)(xmin*20);
163 } else if(!strcmp(a, "ymin")) {
165 if (!PyArg_Parse(o, "i", &ymin)) goto err;
166 bbox->bbox.ymin = (int)(ymin*20);
168 } else if(!strcmp(a, "xmax")) {
170 if (!PyArg_Parse(o, "i", &xmax)) goto err;
171 bbox->bbox.xmax = (int)(xmax*20);
173 } else if(!strcmp(a, "ymax")) {
175 if (!PyArg_Parse(o, "i", &ymax)) goto err;
176 bbox->bbox.ymax = (int)(ymax*20);
180 mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, a, o);
183 void bbox_dealloc(PyObject* self)
185 mylog("-%08x(%d) bbox_dealloc\n", (int)self, self->ob_refcnt);
188 SRECT bbox_getSRECT(PyObject*self)
191 if (!PyArg_Parse(self, "O!", &BBoxClass, &bbox)) {
193 memset(&dummy, 0, sizeof(dummy));
194 mylog("Error: wrong type for function color_getRGBA");
199 PyTypeObject BBoxClass =
201 PyObject_HEAD_INIT(NULL)
204 tp_basicsize: sizeof(BBoxObject),
206 tp_dealloc: bbox_dealloc,
208 tp_getattr: bbox_getattr,
209 tp_setattr: bbox_setattr,
211 SRECT bbox_getBBox(PyObject*self);
212 //----------------------------------------------------------------------------
218 PyObject* f_Matrix(PyObject* _self, PyObject* args, PyObject* kwargs)
220 PyObject*self = (PyObject*)PyObject_New(MatrixObject, &MatrixClass);
221 MatrixObject*matrix = (MatrixObject*)self;
222 mylog("+%08x(%d) f_Matrix", self, self->ob_refcnt);
223 static char *kwlist[] = {"x", "y", "scale", "rotate", NULL};
224 float x=0,y=0,scale=1.0,rotate=0;
225 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ffff", kwlist, &x,&y,&scale,&rotate))
227 mylog(" %08x(%d) f_Matrix: x=%f y=%f scale=%f rotate=%f", self, self->ob_refcnt, x,y,scale,rotate);
228 swf_GetMatrix(0, &matrix->matrix);
229 matrix->matrix.tx = (int)(x*20);
230 matrix->matrix.ty = (int)(y*20);
231 matrix->matrix.sx = (int)(scale*65536);
232 matrix->matrix.sy = (int)(scale*65536);
236 static PyObject* matrix_getattr(PyObject * self, char* a)
238 PY_ASSERT_TYPE(self,&MatrixClass);
241 static int matrix_setattr(PyObject * self, char* a, PyObject* o)
243 PY_ASSERT_TYPE(self,&MatrixClass);
246 MATRIX matrix_getMatrix(PyObject*self)
248 mylog(" %08x(%d) matrix_getMatrix", self, self->ob_refcnt);
249 PY_ASSERT_TYPE(self,&MatrixClass);
250 MatrixObject*matrix = (MatrixObject*)self;
251 return matrix->matrix;
253 void matrix_dealloc(PyObject* self)
255 mylog("-%08x(%d) matrix_dealloc", self, self->ob_refcnt);
258 PyTypeObject MatrixClass =
260 PyObject_HEAD_INIT(NULL)
263 tp_basicsize: sizeof(MatrixObject),
265 tp_dealloc: matrix_dealloc,
267 tp_getattr: matrix_getattr,
268 tp_setattr: matrix_setattr,
274 tp_hash: 0, // dict(x)
278 //----------------------------------------------------------------------------
284 PyObject* f_ColorTransform(PyObject* self, PyObject* args, PyObject* kwargs)
288 static PyObject* colortransform_getattr(PyObject * self, char* a)
292 static int colortransform_setattr(PyObject * self, char* a, PyObject* o)
296 CXFORM colortransform_getCXForm(PyObject*self)
298 CXFormObject*cxform= 0;
299 if (!PyArg_Parse(self, "O!", &CXFormClass, &cxform)) {
301 memset(&dummy, 0, sizeof(dummy));
302 mylog("Error: wrong type for function color_getRGBA");
305 return cxform->cxform;
307 void colortransform_dealloc(PyObject* self)
309 mylog("-%08x(%d) colortransform_dealloc", self, self->ob_refcnt);
312 PyTypeObject CXFormClass =
314 PyObject_HEAD_INIT(NULL)
316 tp_name: "ColorTransform",
317 tp_basicsize: sizeof(CXFormObject),
319 tp_dealloc: colortransform_dealloc,
321 tp_getattr: colortransform_getattr,
322 tp_setattr: colortransform_setattr,
324 //----------------------------------------------------------------------------
330 PyObject* f_Gradient(PyObject* self, PyObject* args, PyObject* kwargs)
334 static PyObject* gradient_getattr(PyObject * self, char* a)
338 static int gradient_setattr(PyObject * self, char* a, PyObject* o)
342 GRADIENT gradient_getGradient(PyObject*self)
344 GradientObject*gradient = 0;
345 if (!PyArg_Parse(self, "O!", &gradient, &gradient)) {
347 memset(&dummy, 0, sizeof(dummy));
348 mylog("Error: wrong type for function color_getRGBA");
351 return gradient->gradient;
353 void gradient_dealloc(PyObject* self)
355 mylog("-%08x(%d) gradient_dealloc", self, self->ob_refcnt);
358 PyTypeObject GradientClass =
360 PyObject_HEAD_INIT(NULL)
363 tp_basicsize: sizeof(GradientObject),
365 tp_dealloc: gradient_dealloc,
367 tp_getattr: gradient_getattr,
368 tp_setattr: gradient_setattr,
370 //----------------------------------------------------------------------------
372 static PyMethodDef primitive_methods[] =
374 {"Color", (PyCFunction)f_Color, METH_KEYWORDS, "Create a new color object."},
375 {"Gradient", (PyCFunction)f_Gradient, METH_KEYWORDS, "Create a new gradient object."},
376 {"ColorTransform", (PyCFunction)f_ColorTransform, METH_KEYWORDS, "Create a new colortransform object."},
377 {"Matrix", (PyCFunction)f_Matrix, METH_KEYWORDS, "Create a new matrix object."},
378 {"BBox", (PyCFunction)f_BBox, METH_KEYWORDS, "Create a new bounding box object."},
379 {NULL, NULL, 0, NULL}
382 PyMethodDef* primitive_getMethods()
384 GradientClass.ob_type = &PyType_Type;
385 CXFormClass.ob_type = &PyType_Type;
386 BBoxClass.ob_type = &PyType_Type;
387 MatrixClass.ob_type = &PyType_Type;
388 return primitive_methods;