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_Color2(U8 r, U8 g, U8 b, U8 a)
38 ColorObject* color = PyObject_New(ColorObject, &ColorClass);
43 return (PyObject*)color;
45 PyObject* f_Color(PyObject* self, PyObject* args, PyObject* kwargs)
47 static char *kwlist[] = {"r", "g", "b", "a", NULL};
49 int r=0,g=0,b=0,a=255;
50 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii|i", kwlist, &r,&g,&b,&a)) {
54 static char *kwlist[] = {"col", "alpha", NULL};
55 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", kwlist, &s, &mya))
58 sscanf(s, "%02x%02x%02x%02x",&r,&g,&b,&a);
60 color = PyObject_New(ColorObject, &ColorClass);
61 mylog("+%08x(%d) color_new(%d,%d,%d,%d)\n", (int)color, color->ob_refcnt, r,g,b,a);
62 return f_Color2(r,g,b,a);
64 static PyObject* color_getattr(PyObject * self, char* a)
66 ColorObject*color = (ColorObject*)self;
68 return Py_BuildValue("i", color->rgba.r);
69 } else if(!strcmp(a, "g")) {
70 return Py_BuildValue("i", color->rgba.g);
71 } else if(!strcmp(a, "b")) {
72 return Py_BuildValue("i", color->rgba.b);
73 } else if(!strcmp(a, "a")) {
74 return Py_BuildValue("i", color->rgba.a);
75 } else if(!strcmp(a, "alpha")) {
76 return Py_BuildValue("i", color->rgba.a);
77 } else if(!strcmp(a, "rgb")) {
79 sprintf(text, "%02x%02x%02x", color->rgba.r, color->rgba.g, color->rgba.b);
80 return PyString_FromString(text);
81 } else if(!strcmp(a, "rgba")) {
83 sprintf(text, "%02x%02x%02x%02x", color->rgba.r, color->rgba.g, color->rgba.b, color->rgba.a);
84 return PyString_FromString(text);
86 return PY_ERROR("bad attribute");
88 static int color_setattr(PyObject * self, char* attr, PyObject* o)
90 ColorObject*color = (ColorObject*)self;
91 if(!strcmp(attr, "r")) {
92 if (!PyArg_Parse(o, "d", &color->rgba.r)) goto err;
94 } else if(!strcmp(attr, "g")) {
95 if (!PyArg_Parse(o, "d", &color->rgba.g)) goto err;
97 } else if(!strcmp(attr, "b")) {
98 if (!PyArg_Parse(o, "d", &color->rgba.b)) goto err;
100 } else if(!strcmp(attr, "a")) {
101 if (!PyArg_Parse(o, "d", &color->rgba.a)) goto err;
105 mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, attr, o);
108 RGBA color_getRGBA(PyObject*self)
110 ColorObject*color = 0;
111 if (!PyArg_Parse(self, "O!", &ColorClass, &color)) {
113 memset(&dummy, 0, sizeof(dummy));
114 mylog("Error: wrong type for function color_getRGBA");
119 void color_dealloc(PyObject* self)
121 mylog("-%08x(%d) color_dealloc\n", (int)self, self->ob_refcnt);
124 PyTypeObject ColorClass =
126 PyObject_HEAD_INIT(NULL)
129 tp_basicsize: sizeof(ColorObject),
131 tp_dealloc: color_dealloc,
133 tp_getattr: color_getattr,
134 tp_setattr: color_setattr,
136 //----------------------------------------------------------------------------
141 //void swf_ExpandRect(SRECT*src, SPOINT add);
142 //void swf_ExpandRect2(SRECT*src, SRECT*add);
144 PyObject* f_BBox(PyObject* self, PyObject* args, PyObject* kwargs)
146 static char *kwlist[] = {"xmin", "ymin", "xmax", "ymax", NULL};
148 float xmin,ymin,xmax,ymax;
150 if (!PyArg_ParseTuple(args, "ffff", &xmin, &ymin, &xmax, &ymax))
153 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ffff", kwlist, &xmin, &ymin, &xmax, &ymax))
157 box.xmin = (int)(xmin*20);
158 box.ymin = (int)(ymin*20);
159 box.xmax = (int)(xmax*20);
160 box.ymax = (int)(ymax*20);
161 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);
162 bbox = PyObject_New(BBoxObject, &BBoxClass);
164 return (PyObject*)bbox;
166 PyObject* f_BBox2(SRECT box)
169 bbox = PyObject_New(BBoxObject, &BBoxClass);
171 return (PyObject*)bbox;
173 static PyObject* bbox_getattr(PyObject * self, char* a)
175 BBoxObject*bbox = (BBoxObject*)self;
176 if(!strcmp(a, "xmin")) {
177 return Py_BuildValue("f", bbox->bbox.xmin/20.0);
178 } else if(!strcmp(a, "ymin")) {
179 return Py_BuildValue("f", bbox->bbox.ymin/20.0);
180 } else if(!strcmp(a, "xmax")) {
181 return Py_BuildValue("f", bbox->bbox.xmax/20.0);
182 } else if(!strcmp(a, "ymax")) {
183 return Py_BuildValue("f", bbox->bbox.ymax/20.0);
187 static int bbox_setattr(PyObject * self, char* a, PyObject* o)
189 BBoxObject*bbox= (BBoxObject*)self;
190 if(!strcmp(a, "xmin")) {
192 if (!PyArg_Parse(o, "f", &xmin)) goto err;
193 bbox->bbox.xmin = (int)(xmin*20);
195 } else if(!strcmp(a, "ymin")) {
197 if (!PyArg_Parse(o, "f", &ymin)) goto err;
198 bbox->bbox.ymin = (int)(ymin*20);
200 } else if(!strcmp(a, "xmax")) {
202 if (!PyArg_Parse(o, "f", &xmax)) goto err;
203 bbox->bbox.xmax = (int)(xmax*20);
205 } else if(!strcmp(a, "ymax")) {
207 if (!PyArg_Parse(o, "f", &ymax)) goto err;
208 bbox->bbox.ymax = (int)(ymax*20);
212 mylog("swf_setattr %08x(%d) %s = ? (%08x)\n", (int)self, self->ob_refcnt, a, o);
215 void bbox_dealloc(PyObject* self)
217 mylog("-%08x(%d) bbox_dealloc\n", (int)self, self->ob_refcnt);
220 SRECT bbox_getSRECT(PyObject*self)
223 if (!PyArg_Parse(self, "O!", &BBoxClass, &bbox)) {
225 memset(&dummy, 0, sizeof(dummy));
226 mylog("Error: wrong type for function color_getRGBA");
231 PyTypeObject BBoxClass =
233 PyObject_HEAD_INIT(NULL)
236 tp_basicsize: sizeof(BBoxObject),
238 tp_dealloc: bbox_dealloc,
240 tp_getattr: bbox_getattr,
241 tp_setattr: bbox_setattr,
243 SRECT bbox_getBBox(PyObject*self);
244 //----------------------------------------------------------------------------
250 PyObject* f_Matrix2(MATRIX* m)
252 PyObject*self = (PyObject*)PyObject_New(MatrixObject, &MatrixClass);
253 MatrixObject*matrix = (MatrixObject*)self;
258 PyObject* f_Matrix(PyObject* _self, PyObject* args, PyObject* kwargs)
260 PyObject*self = (PyObject*)PyObject_New(MatrixObject, &MatrixClass);
261 MatrixObject*matrix = (MatrixObject*)self;
262 mylog("+%08x(%d) f_Matrix", self, self->ob_refcnt);
263 static char *kwlist[] = {"x", "y", "scale", "rotate", "pivotx", "pivoty", NULL};
264 float x=0,y=0,scale=1.0,rotate=0,pivotx=0,pivoty=0;
265 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ffffff", kwlist, &x,&y,&scale,&rotate,&pivotx,&pivoty))
267 mylog(" %08x(%d) f_Matrix: x=%f y=%f scale=%f rotate=%f", self, self->ob_refcnt, x,y,scale,rotate);
268 swf_GetMatrix(0, &matrix->matrix);
270 matrix->matrix.tx = (int)(x*20);
271 matrix->matrix.ty = (int)(y*20);
274 matrix->matrix.sx = (int)(scale*65536);
275 matrix->matrix.sy = (int)(scale*65536);
277 matrix->matrix.sx = (int)(scale*cos(rotate)*65536);
278 matrix->matrix.sy = (int)(scale*cos(rotate)*65536);
279 matrix->matrix.r0 = (int)(scale*sin(rotate)*65536);
280 matrix->matrix.r1 = (int)(-scale*sin(rotate)*65536);
282 if(pivotx || pivoty) {
284 p.x = (int)(pivotx*20);
285 p.y = (int)(pivoty*20);
286 p = swf_TurnPoint(p, &matrix->matrix);
287 matrix->matrix.tx += matrix->matrix.tx-p.x;
288 matrix->matrix.ty += matrix->matrix.ty-p.y;
294 static PyObject* matrix_getattr(PyObject * self, char* a)
296 PY_ASSERT_TYPE(self,&MatrixClass);
299 static int matrix_setattr(PyObject * self, char* a, PyObject* o)
301 PY_ASSERT_TYPE(self,&MatrixClass);
304 MATRIX matrix_getMatrix(PyObject*self)
306 mylog(" %08x(%d) matrix_getMatrix", self, self->ob_refcnt);
307 PY_ASSERT_TYPE(self,&MatrixClass);
308 MatrixObject*matrix = (MatrixObject*)self;
309 return matrix->matrix;
311 void matrix_dealloc(PyObject* self)
313 mylog("-%08x(%d) matrix_dealloc", self, self->ob_refcnt);
316 //SPOINT swf_TurnPoint(SPOINT p, MATRIX* m);
317 //SRECT swf_TurnRect(SRECT r, MATRIX* m);
318 PyTypeObject MatrixClass =
320 PyObject_HEAD_INIT(NULL)
323 tp_basicsize: sizeof(MatrixObject),
325 tp_dealloc: matrix_dealloc,
327 tp_getattr: matrix_getattr,
328 tp_setattr: matrix_setattr,
334 tp_hash: 0, // dict(x)
338 //----------------------------------------------------------------------------
344 PyObject* f_ColorTransform(PyObject* self, PyObject* args, PyObject* kwargs)
348 static PyObject* colortransform_getattr(PyObject * self, char* a)
352 static int colortransform_setattr(PyObject * self, char* a, PyObject* o)
356 CXFORM colortransform_getCXForm(PyObject*self)
358 CXFormObject*cxform= 0;
359 if (!PyArg_Parse(self, "O!", &CXFormClass, &cxform)) {
361 memset(&dummy, 0, sizeof(dummy));
362 mylog("Error: wrong type for function color_getRGBA");
365 return cxform->cxform;
367 void colortransform_dealloc(PyObject* self)
369 mylog("-%08x(%d) colortransform_dealloc", self, self->ob_refcnt);
372 PyTypeObject CXFormClass =
374 PyObject_HEAD_INIT(NULL)
376 tp_name: "ColorTransform",
377 tp_basicsize: sizeof(CXFormObject),
379 tp_dealloc: colortransform_dealloc,
381 tp_getattr: colortransform_getattr,
382 tp_setattr: colortransform_setattr,
384 //----------------------------------------------------------------------------
390 PyObject* f_Gradient(PyObject* self, PyObject* args, PyObject* kwargs)
394 static PyObject* gradient_getattr(PyObject * self, char* a)
398 static int gradient_setattr(PyObject * self, char* a, PyObject* o)
402 GRADIENT gradient_getGradient(PyObject*self)
404 GradientObject*gradient = 0;
405 if (!PyArg_Parse(self, "O!", &gradient, &gradient)) {
407 memset(&dummy, 0, sizeof(dummy));
408 mylog("Error: wrong type for function color_getRGBA");
411 return gradient->gradient;
413 void gradient_dealloc(PyObject* self)
415 mylog("-%08x(%d) gradient_dealloc", self, self->ob_refcnt);
418 PyTypeObject GradientClass =
420 PyObject_HEAD_INIT(NULL)
423 tp_basicsize: sizeof(GradientObject),
425 tp_dealloc: gradient_dealloc,
427 tp_getattr: gradient_getattr,
428 tp_setattr: gradient_setattr,
430 //----------------------------------------------------------------------------
437 PyObject* f_LineStyle2(RGBA color, int width)
439 LineStyleObject* self = PyObject_New(LineStyleObject, &LineStyleClass);
440 self->ls.color = color;
441 self->ls.width = width;
442 return (PyObject*)self;
444 PyObject* f_LineStyle3(LINESTYLE ls)
446 LineStyleObject* self = PyObject_New(LineStyleObject, &LineStyleClass);
448 return (PyObject*)self;
450 PyObject* f_LineStyle(PyObject* _self, PyObject* args, PyObject* kwargs)
452 static char *kwlist[] = {"line", "color", NULL};
456 if (!PyArg_ParseTuple(args, "fO!", &linewidth, &ColorClass, &color))
459 return f_LineStyle2(color_getRGBA(color), (int)(linewidth*20));
461 LINESTYLE linestyle_getLineStyle(PyObject*_self)
463 LineStyleObject* self = (LineStyleObject*)_self;
466 static PyObject* linestyle_getattr(PyObject * _self, char* a)
468 LineStyleObject*self = (LineStyleObject*)_self;
469 if(!strcmp(a, "width")) {
470 return Py_BuildValue("i", self->ls.width);
471 } else if(!strcmp(a, "color")) {
472 return f_Color2(self->ls.color.r, self->ls.color.g, self->ls.color.b, self->ls.color.a);
476 static int linestyle_setattr(PyObject * _self, char* a, PyObject* o)
478 LineStyleObject*self = (LineStyleObject*)_self;
479 if(!strcmp(a, "color")) {
480 self->ls.color = color_getRGBA(o);
485 static LINESTYLE linestyle_getlinestyle(PyObject*_self)
487 LineStyleObject*self = (LineStyleObject*)_self;
490 static void linestyle_dealloc(PyObject* self)
492 mylog("-%08x(%d) linestyle_dealloc", self, self->ob_refcnt);
495 static int linestyle_print(PyObject * _self, FILE *fi, int flags) //flags&Py_PRINT_RAW
497 LineStyleObject* self = (LineStyleObject*)_self;
498 fprintf(fi, "line-%d-%02x%02x%02x%02x", self->ls.width, self->ls.color.r, self->ls.color.g, self->ls.color.b, self->ls.color.a);
501 PyTypeObject LineStyleClass =
503 PyObject_HEAD_INIT(NULL)
505 tp_name: "linestyle",
506 tp_basicsize: sizeof(LineStyleObject),
508 tp_dealloc: linestyle_dealloc,
509 tp_print: linestyle_print,
510 tp_getattr: linestyle_getattr,
511 tp_setattr: linestyle_setattr,
513 //----------------------------------------------------------------------------
520 PyObject* f_FillStyle2(FILLSTYLE fs)
522 FillStyleObject* self = PyObject_New(FillStyleObject, &FillStyleClass);
524 return (PyObject*)self;
526 PyObject* f_SolidFillStyle2(RGBA color)
528 FillStyleObject* self = PyObject_New(FillStyleObject, &FillStyleClass);
529 self->fs.type = FILL_SOLID;
530 self->fs.color = color;
531 return (PyObject*)self;
533 PyObject* f_SolidFillStyle(PyObject* _self, PyObject* args, PyObject* kwargs)
535 static char *kwlist[] = {"color", NULL};
538 if (!PyArg_ParseTuple(args, "O!", &ColorClass, &color))
541 return f_SolidFillStyle2(color_getRGBA(color));
543 FILLSTYLE fillstyle_getFillStyle(PyObject*_self)
545 FillStyleObject* self = (FillStyleObject*)_self;
548 static void fillstyle_dealloc(PyObject* self)
550 mylog("-%08x(%d) linestyle_dealloc", self, self->ob_refcnt);
553 static int fillstyle_print(PyObject * _self, FILE *fi, int flags) //flags&Py_PRINT_RAW
555 FillStyleObject* self = (FillStyleObject*)_self;
556 if(self->fs.type == FILL_SOLID)
557 fprintf(fi, "fill-solid(%02x%02x%02x%02x)", self->fs.color.r, self->fs.color.g, self->fs.color.b, self->fs.color.a);
559 fprintf(fi, "fill-%02x", self->fs.type);
562 PyObject* fillstyle_issolid(PyObject*_self, PyObject*args)
564 FillStyleObject* self = (FillStyleObject*)_self;
565 int b = self->fs.type == FILL_SOLID;
566 return PyInt_FromLong(b);
568 static PyMethodDef FillStyleMethods[] =
570 /* Module functions */
571 {"isSolid", fillstyle_issolid, METH_VARARGS, "Queries whether this is a solid fill"},
574 static PyObject* fillstyle_getattr(PyObject * _self, char* a)
576 FillStyleObject* self = (FillStyleObject*)_self;
577 if(!strcmp(a, "color")) {
578 return f_Color2(self->fs.color.r, self->fs.color.g, self->fs.color.b, self->fs.color.a);
580 return Py_FindMethod(FillStyleMethods, _self, a);
582 static int fillstyle_setattr(PyObject * _self, char* a, PyObject* o)
584 FillStyleObject*self = (FillStyleObject*)_self;
585 if(!strcmp(a, "color")) {
586 self->fs.color = color_getRGBA(o);
592 PyTypeObject FillStyleClass =
594 PyObject_HEAD_INIT(NULL)
596 tp_name: "fillstyle",
597 tp_basicsize: sizeof(FillStyleObject),
599 tp_dealloc: fillstyle_dealloc,
600 tp_print: fillstyle_print,
601 tp_getattr: fillstyle_getattr,
602 tp_setattr: fillstyle_setattr,
604 //----------------------------------------------------------------------------
605 static PyMethodDef primitive_methods[] =
607 {"Color", (PyCFunction)f_Color, METH_KEYWORDS, "Create a new color object."},
608 {"Gradient", (PyCFunction)f_Gradient, METH_KEYWORDS, "Create a new gradient object."},
609 {"ColorTransform", (PyCFunction)f_ColorTransform, METH_KEYWORDS, "Create a new colortransform object."},
610 {"Matrix", (PyCFunction)f_Matrix, METH_KEYWORDS, "Create a new matrix object."},
611 {"BBox", (PyCFunction)f_BBox, METH_KEYWORDS, "Create a new bounding box object."},
612 {"SolidFillStyle", (PyCFunction)f_SolidFillStyle, METH_KEYWORDS, "Creates a new solid fill style."},
613 {"LineStyle", (PyCFunction)f_SolidFillStyle, METH_KEYWORDS, "Creates a new line style."},
614 {NULL, NULL, 0, NULL}
617 PyMethodDef* primitive_getMethods()
619 GradientClass.ob_type = &PyType_Type;
620 CXFormClass.ob_type = &PyType_Type;
621 BBoxClass.ob_type = &PyType_Type;
622 MatrixClass.ob_type = &PyType_Type;
623 return primitive_methods;