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)
359 static PyObject* colortransform_getattr(PyObject * self, char* a)
363 static int colortransform_setattr(PyObject * self, char* a, PyObject* o)
367 CXFORM colortransform_getCXForm(PyObject*self)
369 CXFormObject*cxform= 0;
370 if (!PyArg_Parse(self, "O!", &CXFormClass, &cxform)) {
372 memset(&dummy, 0, sizeof(dummy));
373 mylog("Error: wrong type for function color_getRGBA");
376 return cxform->cxform;
378 void colortransform_dealloc(PyObject* self)
380 mylog("-%08x(%d) colortransform_dealloc", self, self->ob_refcnt);
383 PyTypeObject CXFormClass =
385 PyObject_HEAD_INIT(NULL)
387 tp_name: "ColorTransform",
388 tp_basicsize: sizeof(CXFormObject),
390 tp_dealloc: colortransform_dealloc,
392 tp_getattr: colortransform_getattr,
393 tp_setattr: colortransform_setattr,
395 //----------------------------------------------------------------------------
401 PyObject* f_Gradient(PyObject* self, PyObject* args, PyObject* kwargs)
405 static PyObject* gradient_getattr(PyObject * self, char* a)
409 static int gradient_setattr(PyObject * self, char* a, PyObject* o)
413 GRADIENT gradient_getGradient(PyObject*self)
415 GradientObject*gradient = 0;
416 if (!PyArg_Parse(self, "O!", &gradient, &gradient)) {
418 memset(&dummy, 0, sizeof(dummy));
419 mylog("Error: wrong type for function color_getRGBA");
422 return gradient->gradient;
424 void gradient_dealloc(PyObject* self)
426 mylog("-%08x(%d) gradient_dealloc", self, self->ob_refcnt);
429 PyTypeObject GradientClass =
431 PyObject_HEAD_INIT(NULL)
434 tp_basicsize: sizeof(GradientObject),
436 tp_dealloc: gradient_dealloc,
438 tp_getattr: gradient_getattr,
439 tp_setattr: gradient_setattr,
441 //----------------------------------------------------------------------------
448 PyObject* f_LineStyle2(RGBA color, int width)
450 LineStyleObject* self = PyObject_New(LineStyleObject, &LineStyleClass);
451 self->ls.color = color;
452 self->ls.width = width;
453 return (PyObject*)self;
455 PyObject* f_LineStyle3(LINESTYLE ls)
457 LineStyleObject* self = PyObject_New(LineStyleObject, &LineStyleClass);
459 return (PyObject*)self;
461 PyObject* f_LineStyle(PyObject* _self, PyObject* args, PyObject* kwargs)
463 static char *kwlist[] = {"line", "color", NULL};
467 if (!PyArg_ParseTuple(args, "fO!", &linewidth, &ColorClass, &color))
470 return f_LineStyle2(color_getRGBA(color), (int)(linewidth*20));
472 LINESTYLE linestyle_getLineStyle(PyObject*_self)
474 LineStyleObject* self = (LineStyleObject*)_self;
477 static PyObject* linestyle_getattr(PyObject * _self, char* a)
479 LineStyleObject*self = (LineStyleObject*)_self;
480 if(!strcmp(a, "width")) {
481 return Py_BuildValue("i", self->ls.width);
482 } else if(!strcmp(a, "color")) {
483 return f_Color2(self->ls.color.r, self->ls.color.g, self->ls.color.b, self->ls.color.a);
487 static int linestyle_setattr(PyObject * _self, char* a, PyObject* o)
489 LineStyleObject*self = (LineStyleObject*)_self;
490 if(!strcmp(a, "color")) {
491 self->ls.color = color_getRGBA(o);
496 static LINESTYLE linestyle_getlinestyle(PyObject*_self)
498 LineStyleObject*self = (LineStyleObject*)_self;
501 static void linestyle_dealloc(PyObject* self)
503 mylog("-%08x(%d) linestyle_dealloc", self, self->ob_refcnt);
506 static int linestyle_print(PyObject * _self, FILE *fi, int flags) //flags&Py_PRINT_RAW
508 LineStyleObject* self = (LineStyleObject*)_self;
509 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);
512 PyTypeObject LineStyleClass =
514 PyObject_HEAD_INIT(NULL)
516 tp_name: "linestyle",
517 tp_basicsize: sizeof(LineStyleObject),
519 tp_dealloc: linestyle_dealloc,
520 tp_print: linestyle_print,
521 tp_getattr: linestyle_getattr,
522 tp_setattr: linestyle_setattr,
524 //----------------------------------------------------------------------------
531 PyObject* f_FillStyle2(FILLSTYLE fs)
533 FillStyleObject* self = PyObject_New(FillStyleObject, &FillStyleClass);
535 return (PyObject*)self;
537 PyObject* f_SolidFillStyle2(RGBA color)
539 FillStyleObject* self = PyObject_New(FillStyleObject, &FillStyleClass);
540 self->fs.type = FILL_SOLID;
541 self->fs.color = color;
542 return (PyObject*)self;
544 PyObject* f_SolidFillStyle(PyObject* _self, PyObject* args, PyObject* kwargs)
546 static char *kwlist[] = {"color", NULL};
549 if (!PyArg_ParseTuple(args, "O!", &ColorClass, &color))
552 return f_SolidFillStyle2(color_getRGBA(color));
554 FILLSTYLE fillstyle_getFillStyle(PyObject*_self)
556 FillStyleObject* self = (FillStyleObject*)_self;
559 static void fillstyle_dealloc(PyObject* self)
561 mylog("-%08x(%d) linestyle_dealloc", self, self->ob_refcnt);
564 static int fillstyle_print(PyObject * _self, FILE *fi, int flags) //flags&Py_PRINT_RAW
566 FillStyleObject* self = (FillStyleObject*)_self;
567 if(self->fs.type == FILL_SOLID)
568 fprintf(fi, "fill-solid(%02x%02x%02x%02x)", self->fs.color.r, self->fs.color.g, self->fs.color.b, self->fs.color.a);
570 fprintf(fi, "fill-%02x", self->fs.type);
573 PyObject* fillstyle_issolid(PyObject*_self, PyObject*args)
575 FillStyleObject* self = (FillStyleObject*)_self;
576 int b = self->fs.type == FILL_SOLID;
577 return PyInt_FromLong(b);
579 static PyMethodDef FillStyleMethods[] =
581 /* Module functions */
582 {"isSolid", fillstyle_issolid, METH_VARARGS, "Queries whether this is a solid fill"},
585 static PyObject* fillstyle_getattr(PyObject * _self, char* a)
587 FillStyleObject* self = (FillStyleObject*)_self;
588 if(!strcmp(a, "color")) {
589 return f_Color2(self->fs.color.r, self->fs.color.g, self->fs.color.b, self->fs.color.a);
591 return Py_FindMethod(FillStyleMethods, _self, a);
593 static int fillstyle_setattr(PyObject * _self, char* a, PyObject* o)
595 FillStyleObject*self = (FillStyleObject*)_self;
596 if(!strcmp(a, "color")) {
597 self->fs.color = color_getRGBA(o);
603 PyTypeObject FillStyleClass =
605 PyObject_HEAD_INIT(NULL)
607 tp_name: "fillstyle",
608 tp_basicsize: sizeof(FillStyleObject),
610 tp_dealloc: fillstyle_dealloc,
611 tp_print: fillstyle_print,
612 tp_getattr: fillstyle_getattr,
613 tp_setattr: fillstyle_setattr,
615 //----------------------------------------------------------------------------
616 static PyMethodDef primitive_methods[] =
618 {"Color", (PyCFunction)f_Color, METH_KEYWORDS, "Create a new color object."},
619 {"Gradient", (PyCFunction)f_Gradient, METH_KEYWORDS, "Create a new gradient object."},
620 {"ColorTransform", (PyCFunction)f_ColorTransform, METH_KEYWORDS, "Create a new colortransform object."},
621 {"Matrix", (PyCFunction)f_Matrix, METH_KEYWORDS, "Create a new matrix object."},
622 {"BBox", (PyCFunction)f_BBox, METH_KEYWORDS, "Create a new bounding box object."},
623 {"SolidFillStyle", (PyCFunction)f_SolidFillStyle, METH_KEYWORDS, "Creates a new solid fill style."},
624 {"LineStyle", (PyCFunction)f_SolidFillStyle, METH_KEYWORDS, "Creates a new line style."},
625 {NULL, NULL, 0, NULL}
628 PyMethodDef* primitive_getMethods()
630 GradientClass.ob_type = &PyType_Type;
631 CXFormClass.ob_type = &PyType_Type;
632 BBoxClass.ob_type = &PyType_Type;
633 MatrixClass.ob_type = &PyType_Type;
634 FillStyleClass.ob_type = &PyType_Type;
635 LineStyleClass.ob_type = &PyType_Type;
636 return primitive_methods;