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);
297 MatrixObject*matrix = (MatrixObject*)self;
298 if(!strcmp(a, "entries")) {
299 return Py_BuildValue("(ffffff)",
300 matrix->matrix.sx/65536.0,
301 matrix->matrix.r0/65536.0,
302 matrix->matrix.r1/65536.0,
303 matrix->matrix.sy/65536.0,
304 matrix->matrix.tx/20.0,
305 matrix->matrix.ty/20.0
310 static int matrix_setattr(PyObject * self, char* a, PyObject* o)
312 PY_ASSERT_TYPE(self,&MatrixClass);
315 MATRIX matrix_getMatrix(PyObject*self)
317 mylog(" %08x(%d) matrix_getMatrix", self, self->ob_refcnt);
318 PY_ASSERT_TYPE(self,&MatrixClass);
319 MatrixObject*matrix = (MatrixObject*)self;
320 return matrix->matrix;
322 void matrix_dealloc(PyObject* self)
324 mylog("-%08x(%d) matrix_dealloc", self, self->ob_refcnt);
327 //SPOINT swf_TurnPoint(SPOINT p, MATRIX* m);
328 //SRECT swf_TurnRect(SRECT r, MATRIX* m);
329 PyTypeObject MatrixClass =
331 PyObject_HEAD_INIT(NULL)
334 tp_basicsize: sizeof(MatrixObject),
336 tp_dealloc: matrix_dealloc,
338 tp_getattr: matrix_getattr,
339 tp_setattr: matrix_setattr,
345 tp_hash: 0, // dict(x)
349 //----------------------------------------------------------------------------
355 PyObject* f_ColorTransform(PyObject* _self, PyObject* args, PyObject* kwargs)
357 int r0=256,g0=256,b0=256,a0=256,r1=0,g1=0,b1=0,a1=0;
358 static char *kwlist[] = {"r_mul", "g_mul", "b_mul", "a_mul", "r_add", "g_add", "b_add", "a_add", NULL};
361 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiiii", kwlist,
368 c.r0 = r0; c.g0 = g0; c.b0 = b0; c.a0 = a0;
369 c.r1 = r1; c.g1 = g1; c.b1 = b1; c.a1 = a1;
371 CXFormObject*self = PyObject_New(CXFormObject, &CXFormClass);
373 return (PyObject*)self;
375 static PyObject* colortransform_getattr(PyObject * self, char* a)
379 static int colortransform_setattr(PyObject * self, char* a, PyObject* o)
383 CXFORM colortransform_getCXForm(PyObject*self)
385 CXFormObject*cxform= 0;
386 if (!PyArg_Parse(self, "O!", &CXFormClass, &cxform)) {
388 memset(&dummy, 0, sizeof(dummy));
389 mylog("Error: wrong type for function color_getRGBA");
392 return cxform->cxform;
394 void colortransform_dealloc(PyObject* self)
396 mylog("-%08x(%d) colortransform_dealloc", self, self->ob_refcnt);
399 PyTypeObject CXFormClass =
401 PyObject_HEAD_INIT(NULL)
403 tp_name: "ColorTransform",
404 tp_basicsize: sizeof(CXFormObject),
406 tp_dealloc: colortransform_dealloc,
408 tp_getattr: colortransform_getattr,
409 tp_setattr: colortransform_setattr,
411 //----------------------------------------------------------------------------
417 PyObject* f_Gradient(PyObject* self, PyObject* args, PyObject* kwargs)
421 static PyObject* gradient_getattr(PyObject * self, char* a)
425 static int gradient_setattr(PyObject * self, char* a, PyObject* o)
429 GRADIENT gradient_getGradient(PyObject*self)
431 GradientObject*gradient = 0;
432 if (!PyArg_Parse(self, "O!", &gradient, &gradient)) {
434 memset(&dummy, 0, sizeof(dummy));
435 mylog("Error: wrong type for function color_getRGBA");
438 return gradient->gradient;
440 void gradient_dealloc(PyObject* self)
442 mylog("-%08x(%d) gradient_dealloc", self, self->ob_refcnt);
445 PyTypeObject GradientClass =
447 PyObject_HEAD_INIT(NULL)
450 tp_basicsize: sizeof(GradientObject),
452 tp_dealloc: gradient_dealloc,
454 tp_getattr: gradient_getattr,
455 tp_setattr: gradient_setattr,
457 //----------------------------------------------------------------------------
464 PyObject* f_LineStyle2(RGBA color, int width)
466 LineStyleObject* self = PyObject_New(LineStyleObject, &LineStyleClass);
467 self->ls.color = color;
468 self->ls.width = width;
469 return (PyObject*)self;
471 PyObject* f_LineStyle3(LINESTYLE ls)
473 LineStyleObject* self = PyObject_New(LineStyleObject, &LineStyleClass);
475 return (PyObject*)self;
477 PyObject* f_LineStyle(PyObject* _self, PyObject* args, PyObject* kwargs)
479 static char *kwlist[] = {"line", "color", NULL};
483 if (!PyArg_ParseTuple(args, "fO!", &linewidth, &ColorClass, &color))
486 return f_LineStyle2(color_getRGBA(color), (int)(linewidth*20));
488 LINESTYLE linestyle_getLineStyle(PyObject*_self)
490 LineStyleObject* self = (LineStyleObject*)_self;
493 static PyObject* linestyle_getattr(PyObject * _self, char* a)
495 LineStyleObject*self = (LineStyleObject*)_self;
496 if(!strcmp(a, "width")) {
497 return Py_BuildValue("i", self->ls.width);
498 } else if(!strcmp(a, "color")) {
499 return f_Color2(self->ls.color.r, self->ls.color.g, self->ls.color.b, self->ls.color.a);
503 static int linestyle_setattr(PyObject * _self, char* a, PyObject* o)
505 LineStyleObject*self = (LineStyleObject*)_self;
506 if(!strcmp(a, "color")) {
507 self->ls.color = color_getRGBA(o);
512 static LINESTYLE linestyle_getlinestyle(PyObject*_self)
514 LineStyleObject*self = (LineStyleObject*)_self;
517 static void linestyle_dealloc(PyObject* self)
519 mylog("-%08x(%d) linestyle_dealloc", self, self->ob_refcnt);
522 static int linestyle_print(PyObject * _self, FILE *fi, int flags) //flags&Py_PRINT_RAW
524 LineStyleObject* self = (LineStyleObject*)_self;
525 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);
528 PyTypeObject LineStyleClass =
530 PyObject_HEAD_INIT(NULL)
532 tp_name: "linestyle",
533 tp_basicsize: sizeof(LineStyleObject),
535 tp_dealloc: linestyle_dealloc,
536 tp_print: linestyle_print,
537 tp_getattr: linestyle_getattr,
538 tp_setattr: linestyle_setattr,
540 //----------------------------------------------------------------------------
547 PyObject* f_FillStyle2(FILLSTYLE fs)
549 FillStyleObject* self = PyObject_New(FillStyleObject, &FillStyleClass);
551 return (PyObject*)self;
553 PyObject* f_SolidFillStyle2(RGBA color)
555 FillStyleObject* self = PyObject_New(FillStyleObject, &FillStyleClass);
556 self->fs.type = FILL_SOLID;
557 self->fs.color = color;
558 return (PyObject*)self;
560 PyObject* f_SolidFillStyle(PyObject* _self, PyObject* args, PyObject* kwargs)
562 static char *kwlist[] = {"color", NULL};
565 if (!PyArg_ParseTuple(args, "O!", &ColorClass, &color))
568 return f_SolidFillStyle2(color_getRGBA(color));
570 FILLSTYLE fillstyle_getFillStyle(PyObject*_self)
572 FillStyleObject* self = (FillStyleObject*)_self;
575 static void fillstyle_dealloc(PyObject* self)
577 mylog("-%08x(%d) linestyle_dealloc", self, self->ob_refcnt);
580 static int fillstyle_print(PyObject * _self, FILE *fi, int flags) //flags&Py_PRINT_RAW
582 FillStyleObject* self = (FillStyleObject*)_self;
583 if(self->fs.type == FILL_SOLID)
584 fprintf(fi, "fill-solid(%02x%02x%02x%02x)", self->fs.color.r, self->fs.color.g, self->fs.color.b, self->fs.color.a);
586 fprintf(fi, "fill-%02x", self->fs.type);
589 PyObject* fillstyle_issolid(PyObject*_self, PyObject*args)
591 FillStyleObject* self = (FillStyleObject*)_self;
592 int b = self->fs.type == FILL_SOLID;
593 return PyInt_FromLong(b);
595 static PyMethodDef FillStyleMethods[] =
597 /* Module functions */
598 {"isSolid", fillstyle_issolid, METH_VARARGS, "Queries whether this is a solid fill"},
601 static PyObject* fillstyle_getattr(PyObject * _self, char* a)
603 FillStyleObject* self = (FillStyleObject*)_self;
604 if(!strcmp(a, "color")) {
605 return f_Color2(self->fs.color.r, self->fs.color.g, self->fs.color.b, self->fs.color.a);
607 return Py_FindMethod(FillStyleMethods, _self, a);
609 static int fillstyle_setattr(PyObject * _self, char* a, PyObject* o)
611 FillStyleObject*self = (FillStyleObject*)_self;
612 if(!strcmp(a, "color")) {
613 self->fs.color = color_getRGBA(o);
619 PyTypeObject FillStyleClass =
621 PyObject_HEAD_INIT(NULL)
623 tp_name: "fillstyle",
624 tp_basicsize: sizeof(FillStyleObject),
626 tp_dealloc: fillstyle_dealloc,
627 tp_print: fillstyle_print,
628 tp_getattr: fillstyle_getattr,
629 tp_setattr: fillstyle_setattr,
631 //----------------------------------------------------------------------------
632 static PyMethodDef primitive_methods[] =
634 {"Color", (PyCFunction)f_Color, METH_KEYWORDS, "Create a new color object."},
635 {"Gradient", (PyCFunction)f_Gradient, METH_KEYWORDS, "Create a new gradient object."},
636 {"ColorTransform", (PyCFunction)f_ColorTransform, METH_KEYWORDS, "Create a new colortransform object."},
637 {"Matrix", (PyCFunction)f_Matrix, METH_KEYWORDS, "Create a new matrix object."},
638 {"BBox", (PyCFunction)f_BBox, METH_KEYWORDS, "Create a new bounding box object."},
639 {"SolidFillStyle", (PyCFunction)f_SolidFillStyle, METH_KEYWORDS, "Creates a new solid fill style."},
640 {"LineStyle", (PyCFunction)f_SolidFillStyle, METH_KEYWORDS, "Creates a new line style."},
641 {NULL, NULL, 0, NULL}
644 PyMethodDef* primitive_getMethods()
646 GradientClass.ob_type = &PyType_Type;
647 CXFormClass.ob_type = &PyType_Type;
648 BBoxClass.ob_type = &PyType_Type;
649 MatrixClass.ob_type = &PyType_Type;
650 FillStyleClass.ob_type = &PyType_Type;
651 LineStyleClass.ob_type = &PyType_Type;
652 return primitive_methods;