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_new tag=%08x\n", (int)tag, tag->ob_refcnt, 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 int * positions = malloc(num*sizeof(int));
157 swf_GetUsedIDs(t, positions);
160 int id = GET16(&t->data[positions[i]]);
161 PyObject*obj = tagmap_id2obj(tagmap, id);
163 PyErr_SetString(PyExc_Exception, setError("TagID %d not defined", id));
166 tagmap_addMapping(tag->tagmap, id, obj);
169 return (PyObject*)tag;
172 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
174 static char *kwlist[] = {"color", NULL};
179 tag = (TagObject*)tag_new();
181 /* 1st try- copy constructor */
182 if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
183 /* 2nd try- color's contructor */
184 color = f_Color(NULL, args, kwargs);
189 tag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
190 RGBA rgba = color_getRGBA(color);
191 swf_SetU8(tag->tag, rgba.r);
192 swf_SetU8(tag->tag, rgba.g);
193 swf_SetU8(tag->tag, rgba.b);
194 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);
195 return (PyObject*)tag;
197 //----------------------------------------------------------------------------
198 static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
200 static char *kwlist[] = {"filename", NULL};
205 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename))
208 font = swf_LoadFont(filename);
210 PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename));
214 tag = (TagObject*)tag_new();
216 tag->tag = swf_InsertTag(0, ST_DEFINEFONT2);
218 swf_FontSetDefine2(tag->tag, tag->font);
219 mylog("+%08x(%d) DefineFont\n", (int)tag, tag->ob_refcnt);
220 return (PyObject*)tag;
222 static SWFFONT* fonttag_getSWFFONT(PyObject*self)
224 PY_ASSERT_TYPE(self, &TagClass);
225 TagObject*font = (TagObject*)self;
228 //----------------------------------------------------------------------------
229 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
231 static char *kwlist[] = {"password", NULL};
235 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
238 tag = (TagObject*)tag_new();
239 tag->tag = swf_InsertTag(0, ST_PROTECT);
241 swf_SetPassword(tag->tag, password);
243 mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
244 return (PyObject*)tag;
246 //----------------------------------------------------------------------------
247 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
249 static char *kwlist[] = {"font", "text", "size", "color", NULL};
256 RGBA rgba = {255,0,0,0};
261 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color))
264 unicode16 = PyUnicode_DecodeUTF16(text, textlen*2, NULL, NULL);
265 unicode8 = PyUnicode_AsUTF8String(unicode16);
266 text = PyString_AS_STRING(unicode8);
269 rgba = color_getRGBA(color);
271 mylog("DefineText: text = %s", text);
273 tag = (TagObject*)tag_new();
275 int font_id = tagmap_add(tag->tagmap, font); // add dependency on font
277 fonttag_getSWFFONT(font)->id = font_id;
279 tag ->tag= swf_InsertTag(0, ST_DEFINETEXT2);
280 swf_SetU16(tag->tag, /*ID*/0);
281 r = swf_SetDefineText(tag->tag, fonttag_getSWFFONT(font), &rgba, text, size);
282 mylog("+%08x(%d) DefineText tag=%08x \n", (int)tag, tag->ob_refcnt);
284 //Py_DECREF(unicode16);
285 //Py_DECREF(unicode8);
288 return (PyObject*)tag;
290 //----------------------------------------------------------------------------
291 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
293 static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
296 TagObject*character = 0;
305 po = malloc(sizeof(SWFPLACEOBJECT));
306 memset(po, 0, sizeof(SWFPLACEOBJECT));
308 swf_GetPlaceObject(0, po);
310 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!i|O!O!isiO!", kwlist,
311 &TagClass, &character,
313 &MatrixClass, &matrix,
314 &CXFormClass, &cxform,
318 &ActionClass, &action
323 po->clipdepth = clipdepth;
326 if(clipdepth) po->clipdepth = clipdepth;
327 if(matrix) po->matrix = matrix_getMatrix(matrix);
328 if(cxform) po->cxform = colortransform_getCXForm(cxform);
329 if(action) po->actions = action_getAction(action);
331 tag = (TagObject*)tag_new();
332 tag->placeobject = po;
333 Py_INCREF(character);
334 tag->character = (PyObject*)character;
335 tag->tag= swf_InsertTag(0, ST_PLACEOBJECT2);
337 po->id = tagmap_add(tag->tagmap,(PyObject*)character);
339 swf_SetPlaceObject(tag->tag, po);
340 mylog("+%08x(%d) PlaceObject %08x(%d)\n", (int)tag, tag->ob_refcnt, character, character->ob_refcnt);
342 return (PyObject*)tag;
346 TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap)
348 TagObject*tag = (TagObject*)self;
350 mylog(" %08x(%d) tag_getTAG: tag=%08x id=%d (%s)", (int)self, self->ob_refcnt, tag->tag, tag->tag->id, swf_TagGetName(tag->tag));
352 TAG* t = swf_InsertTag(prevTag, tag->tag->id);
353 swf_SetBlock(t, tag->tag->data, tag->tag->len);
355 if(swf_isDefiningTag(t)) {
356 int newid = tagmap_add(tagmap, self);
357 swf_SetDefineID(t, newid);
360 int num = swf_GetNumUsedIDs(t);
361 int * positions = malloc(num*sizeof(int));
362 swf_GetUsedIDs(t, positions);
365 int id = GET16(&t->data[positions[i]]);
366 PyObject* obj = tagmap_id2obj(tag->tagmap, id);
368 PyErr_SetString(PyExc_Exception, setError("Internal error: id %d not known in taglist:"));
372 int newid = tagmap_obj2id(tagmap, obj);
373 PUT16(&t->data[positions[i]], newid);
379 PyObject* tag_getDependencies(PyObject*self)
381 TagObject*tag = (TagObject*)self;
382 mylog(" %08x(%d) tag_getDependencies id=%d tag=%s\n", (int)self, self->ob_refcnt, tag->tag->id, swf_TagGetName(tag->tag));
383 return tagmap_getObjectList(tag->tagmap);
386 int tag_print (PyObject * self, FILE * fi, int flags)
388 mylog(" %08x(%d) tag_print flags=%08x\n", (int)self, self->ob_refcnt, flags);
392 PyTypeObject TagClass =
394 PyObject_HEAD_INIT(NULL)
397 tp_basicsize: sizeof(TagObject),
399 tp_dealloc: tag_dealloc,
401 tp_getattr: tag_getattr,
403 static PyMethodDef TagMethods[] =
406 {"SetBackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
407 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
408 {"DefineFont", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
409 {"DefineText", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
410 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
411 {NULL, NULL, 0, NULL}
413 PyMethodDef* tag_getMethods()
415 TagClass.ob_type = &PyType_Type;