6 #include "primitives.h"
10 //----------------------------------------------------------------------------
16 /* ST_PLACEOBJECT, ST_PLACEOBJECT2*/
17 SWFPLACEOBJECT* placeobject;
21 static void tag_dealloc(PyObject * self)
23 TagObject*tag = (TagObject*)self;
24 mylog("tag_dealoc %08x(%d)\n", (int)self, self->ob_refcnt);
25 if(tag->placeobject) {
26 swf_PlaceObjectFree(tag->placeobject);
30 swf_FontFree(tag->font);
34 Py_DECREF(tag->character);
39 //----------------------------------------------------------------------------
40 static PyObject* tag_setU8(PyObject * self, PyObject*other)
44 //----------------------------------------------------------------------------
45 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
47 TagObject*tag = (TagObject*)self;
49 r = tag->tag->data[0];
50 g = tag->tag->data[1];
51 b = tag->tag->data[2];
52 return Py_BuildValue("(iii)", r,g,b);
54 //----------------------------------------------------------------------------
56 static struct tagfunctions {
62 ST_SETBACKGROUNDCOLOR,
63 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
68 static PyMethodDef common_tagfunctions[] =
69 {{"setU8", tag_setU8, METH_VARARGS, "sets a byte to the tag data"},
73 static PyObject* tag_getattr(PyObject * self, char* a)
75 TagObject*tag = (TagObject*)self;
77 int id = tag->tag->id;
81 if(!strcmp(a, "id")) {
82 return Py_BuildValue("i", id);
84 if(!strcmp(a, "name")) {
85 char* name = swf_TagGetName(tag->tag);
86 return Py_BuildValue("s", name);
90 /* search for a tag specific function */
91 for(t=0;t<sizeof(tagfunctions)/sizeof(tagfunctions[0]);t++)
93 if(id==tagfunctions[t].id) {
94 mylog("tag_getattr: id %d found\n", id);
95 ret = Py_FindMethod(tagfunctions[t].f, self, a);
97 ret = FindMethodMore(ret, common_tagfunctions, self, a);
98 mylog("tag_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
103 ret = Py_FindMethod(common_tagfunctions, self, a);
105 mylog("tag_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
108 //----------------------------------------------------------------------------
110 //----------------------------------------------------------------------------
113 TagObject*tag = PyObject_New(TagObject, &TagClass);
116 tag->placeobject = 0;
118 return (PyObject*)tag;
120 PyObject* tag_new2(TAG*_tag)
122 TagObject*tag = PyObject_New(TagObject, &TagClass);
125 tag->placeobject = 0;
127 return (PyObject*)tag;
130 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
132 static char *kwlist[] = {"color", NULL};
137 /* 1st try- copy constructor */
138 if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
139 /* 2nd try- color's contructor */
140 color = f_Color(NULL, args, kwargs);
145 tag = (TagObject*)tag_new();
146 tag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
147 RGBA rgba = color_getRGBA(color);
148 swf_SetU8(tag->tag, rgba.r);
149 swf_SetU8(tag->tag, rgba.g);
150 swf_SetU8(tag->tag, rgba.b);
151 mylog("SetBackgroundColor(%02x,%02x,%02x) %08x -> %08x\n", rgba.r, rgba.g, rgba.b, (int)self, tag);
152 return (PyObject*)tag;
154 //----------------------------------------------------------------------------
155 static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
157 static char *kwlist[] = {"filename", NULL};
162 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename))
165 font = swf_LoadFont(filename);
166 mylog("font=%08x",font);
168 PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename));
172 tag = (TagObject*)tag_new();
174 tag->tag = swf_InsertTag(0, ST_DEFINEFONT2);
175 tag->font->id = 0xabcd; // swf_SetU16(id);
176 swf_FontSetDefine2(tag->tag, tag->font); // TODO: should this be done later, in taglist?
177 mylog("DefineFont %08x -> %08x\n", (int)self, (int)tag);
178 return (PyObject*)tag;
180 //----------------------------------------------------------------------------
181 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
183 static char *kwlist[] = {"password", NULL};
187 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
190 tag = (TagObject*)tag_new();
191 tag->tag = swf_InsertTag(0, ST_PROTECT);
193 swf_SetPassword(tag->tag, password);
195 mylog("f_Protect %08x -> %08x\n", (int)self, (int)tag);
196 return (PyObject*)tag;
198 //----------------------------------------------------------------------------
199 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
201 static char *kwlist[] = {"font", "text", "size", "color", NULL};
208 RGBA rgba = {0,0,0,255};
213 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color))
216 unicode16 = PyUnicode_DecodeUTF16(text, textlen*2, NULL, NULL);
217 unicode8 = PyUnicode_AsUTF8String(unicode16);
218 text = PyString_AS_STRING(unicode8);
221 rgba = color_getRGBA(color);
223 mylog("DefineText: text = %s", text);
225 tag = (TagObject*)tag_new();
226 tag ->tag= swf_InsertTag(0, ST_DEFINETEXT2);
227 swf_SetU16(tag->tag, /*ID*/0);
228 r = swf_SetDefineText(tag->tag, font->font, &rgba, text, size);
229 mylog("DefineText %08x -> %08x\n", (int)self, (int)tag);
230 return (PyObject*)tag;
232 //----------------------------------------------------------------------------
233 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
235 static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
238 TagObject*character = 0;
247 po = malloc(sizeof(SWFPLACEOBJECT));
248 memset(po, 0, sizeof(SWFPLACEOBJECT));
250 swf_GetPlaceObject(0, po);
252 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!i|O!O!isiO!", kwlist,
253 &TagClass, &character,
255 &MatrixClass, &matrix,
256 &CXFormClass, &cxform,
265 po->clipdepth = clipdepth;
268 if(clipdepth) po->matrix = matrix_getMatrix(matrix);
269 if(cxform) po->cxform = colortransform_getCXForm(cxform);
270 if(action) po->actions = action_getAction(action);
272 tag = (TagObject*)tag_new();
273 tag->placeobject = po;
274 Py_INCREF(character);
275 tag->character = (PyObject*)character;
276 tag->tag= swf_InsertTag(0, ST_PLACEOBJECT2);
277 swf_SetPlaceObject(tag->tag, po);
278 mylog("PlaceObject %08x -> %08x\n", (int)self, (int)tag);
279 return (PyObject*)tag;
282 TAG* tag_getTAG(PyObject*self)
285 return ((TagObject*)self)->tag;
288 PyTypeObject TagClass =
290 PyObject_HEAD_INIT(NULL)
293 tp_basicsize: sizeof(TagObject),
295 tp_dealloc: tag_dealloc,
297 tp_getattr: tag_getattr,
299 static PyMethodDef TagMethods[] =
302 {"SetBackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
303 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
304 {"DefineFont", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
305 {"DefineText", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
306 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
307 {NULL, NULL, 0, NULL}
309 PyMethodDef* tag_getMethods()
311 TagClass.ob_type = &PyType_Type;