6 #include "primitives.h"
11 //----------------------------------------------------------------------------
18 /* ST_PLACEOBJECT, ST_PLACEOBJECT2*/
19 SWFPLACEOBJECT* placeobject;
23 //----------------------------------------------------------------------------
24 static void tag_dealloc(PyObject * self)
26 TagObject*tag = (TagObject*)self;
28 mylog("-%08x(%d) tag_dealoc [%s]\n", (int)self, self->ob_refcnt, swf_TagGetName(tag->tag));
30 mylog("-%08x(%d) tag_dealoc [?]\n", (int)self, self->ob_refcnt);
31 if(tag->placeobject) {
32 swf_PlaceObjectFree(tag->placeobject);
36 swf_FontFree(tag->font);
40 Py_DECREF(tag->character);
44 swf_DeleteTag(tag->tag);
47 Py_DECREF(tag->tagmap);
51 //----------------------------------------------------------------------------
52 static PyObject* tag_setU8(PyObject * self, PyObject*other)
56 //----------------------------------------------------------------------------
57 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
59 TagObject*tag = (TagObject*)self;
61 r = tag->tag->data[0];
62 g = tag->tag->data[1];
63 b = tag->tag->data[2];
64 return Py_BuildValue("(iii)", r,g,b);
66 //----------------------------------------------------------------------------
68 static struct tagfunctions {
74 ST_SETBACKGROUNDCOLOR,
75 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
80 static PyMethodDef common_tagfunctions[] =
81 {{"setU8", tag_setU8, METH_VARARGS, "sets a byte to the tag data"},
85 static PyObject* tag_getattr(PyObject * self, char* a)
87 TagObject*tag = (TagObject*)self;
89 int id = tag->tag->id;
93 if(!strcmp(a, "id")) {
94 return Py_BuildValue("i", id);
96 if(!strcmp(a, "name")) {
97 char* name = swf_TagGetName(tag->tag);
98 return Py_BuildValue("s", name);
100 /* if(!strcmp(a, "swf")) {
102 return Py_BuildValue("s", 0);
110 /* search for a tag specific function */
111 for(t=0;t<sizeof(tagfunctions)/sizeof(tagfunctions[0]);t++)
113 if(id==tagfunctions[t].id) {
114 mylog(" %08x(%d) tag_getattr: id %d found\n", (int)self, self->ob_refcnt, id);
115 ret = Py_FindMethod(tagfunctions[t].f, self, a);
117 ret = FindMethodMore(ret, common_tagfunctions, self, a);
118 mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
123 ret = Py_FindMethod(common_tagfunctions, self, a);
125 mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
128 //----------------------------------------------------------------------------
130 //----------------------------------------------------------------------------
133 TagObject*tag = PyObject_New(TagObject, &TagClass);
134 mylog("+%08x(%d) tag_new\n", (int)tag, tag->ob_refcnt);
137 tag->placeobject = 0;
139 tag->tagmap = tagmap_new();
140 return (PyObject*)tag;
142 PyObject* tag_new2(TAG*t, PyObject* tagmap)
144 TagObject*tag = PyObject_New(TagObject, &TagClass);
145 mylog("+%08x(%d) tag_new2 tag=%08x id=%d (%s)\n", (int)tag, tag->ob_refcnt, t, t->id, swf_TagGetName(t));
148 tag->placeobject = 0;
149 tag->tagmap = tagmap_new();
151 tag->tag = swf_InsertTag(0, t->id);
152 swf_SetBlock(tag->tag, t->data, t->len);
155 int num = swf_GetNumUsedIDs(t);
156 if(num) { // tag has dependencies
157 int * positions = malloc(num*sizeof(int));
158 swf_GetUsedIDs(t, positions);
161 int id = GET16(&t->data[positions[i]]);
162 PyObject*obj = tagmap_id2obj(tagmap, id);
164 PyErr_SetString(PyExc_Exception, setError("TagID %d not defined", id));
167 //mylog("+%08x(%d) tag_new2 handling id %d at %d/%d\n", (int)tag, tag->ob_refcnt, id, positions[i], t->len);
168 //mylog("+%08x(%d) tag_new2 add dependency %d to id %d, object %08x(%d)\n", (int)tag, tag->ob_refcnt, i, id, obj, obj->ob_refcnt);
169 tagmap_addMapping(tag->tagmap, id, obj);
173 return (PyObject*)tag;
176 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
178 static char *kwlist[] = {"color", NULL};
183 tag = (TagObject*)tag_new();
185 /* 1st try- copy constructor */
186 if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
187 /* 2nd try- color's contructor */
188 color = f_Color(NULL, args, kwargs);
193 tag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
194 RGBA rgba = color_getRGBA(color);
195 swf_SetU8(tag->tag, rgba.r);
196 swf_SetU8(tag->tag, rgba.g);
197 swf_SetU8(tag->tag, rgba.b);
198 mylog(" %08x(%d) SetBackgroundColor(%02x,%02x,%02x) (colorobj=%08x(%d))\n", (int)tag, tag->ob_refcnt, rgba.r, rgba.g, rgba.b, color, color->ob_refcnt);
199 return (PyObject*)tag;
201 //----------------------------------------------------------------------------
202 static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
204 static char *kwlist[] = {"filename", NULL};
209 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename))
212 font = swf_LoadFont(filename);
214 PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename));
218 tag = (TagObject*)tag_new();
220 tag->tag = swf_InsertTag(0, ST_DEFINEFONT2);
222 swf_FontSetDefine2(tag->tag, tag->font);
223 mylog("+%08x(%d) DefineFont\n", (int)tag, tag->ob_refcnt);
224 return (PyObject*)tag;
226 static SWFFONT* fonttag_getSWFFONT(PyObject*self)
228 PY_ASSERT_TYPE(self, &TagClass);
229 TagObject*font = (TagObject*)self;
232 //----------------------------------------------------------------------------
233 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
235 static char *kwlist[] = {"password", NULL};
239 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
242 tag = (TagObject*)tag_new();
243 tag->tag = swf_InsertTag(0, ST_PROTECT);
245 swf_SetPassword(tag->tag, password);
247 mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
248 return (PyObject*)tag;
250 //----------------------------------------------------------------------------
251 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
253 static char *kwlist[] = {"font", "text", "size", "color", NULL};
260 RGBA rgba = {255,0,0,0};
265 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color))
268 unicode16 = PyUnicode_DecodeUTF16(text, textlen*2, NULL, NULL);
269 unicode8 = PyUnicode_AsUTF8String(unicode16);
270 text = PyString_AS_STRING(unicode8);
273 rgba = color_getRGBA(color);
275 mylog("DefineText: text = %s", text);
277 tag = (TagObject*)tag_new();
279 int font_id = tagmap_add(tag->tagmap, font); // add dependency on font
281 fonttag_getSWFFONT(font)->id = font_id;
283 tag ->tag= swf_InsertTag(0, ST_DEFINETEXT2);
284 swf_SetU16(tag->tag, /*ID*/0);
285 r = swf_SetDefineText(tag->tag, fonttag_getSWFFONT(font), &rgba, text, size);
286 mylog("+%08x(%d) DefineText tag=%08x \n", (int)tag, tag->ob_refcnt);
288 //Py_DECREF(unicode16);
289 //Py_DECREF(unicode8);
292 return (PyObject*)tag;
294 //----------------------------------------------------------------------------
295 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
297 static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
300 TagObject*character = 0;
309 po = malloc(sizeof(SWFPLACEOBJECT));
310 memset(po, 0, sizeof(SWFPLACEOBJECT));
312 swf_GetPlaceObject(0, po);
314 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!i|O!O!isiO!", kwlist,
315 &TagClass, &character,
317 &MatrixClass, &matrix,
318 &CXFormClass, &cxform,
322 &ActionClass, &action
327 po->clipdepth = clipdepth;
330 if(clipdepth) po->clipdepth = clipdepth;
331 if(matrix) po->matrix = matrix_getMatrix(matrix);
332 if(cxform) po->cxform = colortransform_getCXForm(cxform);
333 if(action) po->actions = action_getAction(action);
335 tag = (TagObject*)tag_new();
336 tag->placeobject = po;
337 Py_INCREF(character);
338 tag->character = (PyObject*)character;
339 tag->tag= swf_InsertTag(0, ST_PLACEOBJECT2);
341 po->id = tagmap_add(tag->tagmap,(PyObject*)character);
343 swf_SetPlaceObject(tag->tag, po);
344 mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
346 return (PyObject*)tag;
350 TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap)
352 TagObject*tag = (TagObject*)self;
354 mylog(" %08x(%d) tag_getTAG: tag=%08x id=%d (%s)", (int)self, self->ob_refcnt, tag->tag, tag->tag->id, swf_TagGetName(tag->tag));
356 TAG* t = swf_InsertTag(prevTag, tag->tag->id);
357 swf_SetBlock(t, tag->tag->data, tag->tag->len);
359 if(swf_isDefiningTag(t)) {
360 int newid = tagmap_add(tagmap, self);
361 swf_SetDefineID(t, newid);
364 int num = swf_GetNumUsedIDs(t);
365 int * positions = malloc(num*sizeof(int));
366 swf_GetUsedIDs(t, positions);
369 int id = GET16(&t->data[positions[i]]);
370 PyObject* obj = tagmap_id2obj(tag->tagmap, id);
372 PyErr_SetString(PyExc_Exception, setError("Internal error: id %d not known in taglist:"));
376 int newid = tagmap_obj2id(tagmap, obj);
377 PUT16(&t->data[positions[i]], newid);
383 PyObject* tag_getDependencies(PyObject*self)
385 TagObject*tag = (TagObject*)self;
386 mylog(" %08x(%d) tag_getDependencies id=%d tag=%s\n", (int)self, self->ob_refcnt, tag->tag->id, swf_TagGetName(tag->tag));
387 return tagmap_getObjectList(tag->tagmap);
390 int tag_print (PyObject * self, FILE * fi, int flags)
392 mylog(" %08x(%d) tag_print flags=%08x\n", (int)self, self->ob_refcnt, flags);
396 PyTypeObject TagClass =
398 PyObject_HEAD_INIT(NULL)
401 tp_basicsize: sizeof(TagObject),
403 tp_dealloc: tag_dealloc,
405 tp_getattr: tag_getattr,
407 static PyMethodDef TagMethods[] =
410 {"SetBackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
411 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
412 {"DefineFont", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
413 {"DefineText", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
414 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
415 {NULL, NULL, 0, NULL}
417 PyMethodDef* tag_getMethods()
419 TagClass.ob_type = &PyType_Type;