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;
27 mylog("-%08x(%d) tag_dealoc\n", (int)self, self->ob_refcnt);
28 if(tag->placeobject) {
29 swf_PlaceObjectFree(tag->placeobject);
33 swf_FontFree(tag->font);
37 Py_DECREF(tag->character);
41 swf_DeleteTag(tag->tag);
44 Py_DECREF(tag->tagmap);
48 //----------------------------------------------------------------------------
49 static PyObject* tag_setU8(PyObject * self, PyObject*other)
53 //----------------------------------------------------------------------------
54 static PyObject* tag_setbackgroundcolor_getrgb(PyObject * self, PyObject*other)
56 TagObject*tag = (TagObject*)self;
58 r = tag->tag->data[0];
59 g = tag->tag->data[1];
60 b = tag->tag->data[2];
61 return Py_BuildValue("(iii)", r,g,b);
63 //----------------------------------------------------------------------------
65 static struct tagfunctions {
71 ST_SETBACKGROUNDCOLOR,
72 {{"getRGB", tag_setbackgroundcolor_getrgb, METH_VARARGS, "get's the color set by this tag"},
77 static PyMethodDef common_tagfunctions[] =
78 {{"setU8", tag_setU8, METH_VARARGS, "sets a byte to the tag data"},
82 static PyObject* tag_getattr(PyObject * self, char* a)
84 TagObject*tag = (TagObject*)self;
86 int id = tag->tag->id;
90 if(!strcmp(a, "id")) {
91 return Py_BuildValue("i", id);
93 if(!strcmp(a, "name")) {
94 char* name = swf_TagGetName(tag->tag);
95 return Py_BuildValue("s", name);
97 /* if(!strcmp(a, "swf")) {
99 return Py_BuildValue("s", 0);
107 /* search for a tag specific function */
108 for(t=0;t<sizeof(tagfunctions)/sizeof(tagfunctions[0]);t++)
110 if(id==tagfunctions[t].id) {
111 mylog(" %08x(%d) tag_getattr: id %d found\n", (int)self, self->ob_refcnt, id);
112 ret = Py_FindMethod(tagfunctions[t].f, self, a);
114 ret = FindMethodMore(ret, common_tagfunctions, self, a);
115 mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
120 ret = Py_FindMethod(common_tagfunctions, self, a);
122 mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
125 //----------------------------------------------------------------------------
127 //----------------------------------------------------------------------------
130 TagObject*tag = PyObject_New(TagObject, &TagClass);
131 mylog("+%08x(%d) tag_new\n", (int)tag, tag->ob_refcnt);
134 tag->placeobject = 0;
136 tag->tagmap = tagmap_new();
137 return (PyObject*)tag;
139 PyObject* tag_new2(TAG*t, PyObject* tagmap)
141 TagObject*tag = PyObject_New(TagObject, &TagClass);
142 mylog("+%08x(%d) tag_new tag=%08x\n", (int)tag, tag->ob_refcnt, t);
145 tag->placeobject = 0;
146 tag->tagmap = tagmap_new();
148 tag->tag = swf_InsertTag(0, t->id);
149 swf_SetBlock(tag->tag, t->data, t->len);
152 int num = swf_GetNumUsedIDs(t);
153 int * positions = malloc(num*sizeof(int));
154 swf_GetUsedIDs(t, positions);
157 int id = GET16(&t->data[positions[i]]);
158 PyObject*obj = tagmap_id2obj(tagmap, id);
160 PyErr_SetString(PyExc_Exception, setError("TagID %d not defined", id));
163 tagmap_addMapping(tag->tagmap, id, obj);
166 return (PyObject*)tag;
169 static PyObject* f_SetBackgroundColor(PyObject* self, PyObject* args, PyObject* kwargs)
171 static char *kwlist[] = {"color", NULL};
176 tag = (TagObject*)tag_new();
178 /* 1st try- copy constructor */
179 if(!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist, &ColorClass, &color)) {
180 /* 2nd try- color's contructor */
181 color = f_Color(NULL, args, kwargs);
186 tag->tag = swf_InsertTag(0, ST_SETBACKGROUNDCOLOR);
187 RGBA rgba = color_getRGBA(color);
188 swf_SetU8(tag->tag, rgba.r);
189 swf_SetU8(tag->tag, rgba.g);
190 swf_SetU8(tag->tag, rgba.b);
191 mylog(" %08x(%d) SetBackgroundColor(%02x,%02x,%02x)\n", (int)tag, tag->ob_refcnt, rgba.r, rgba.g, rgba.b);
192 return (PyObject*)tag;
194 //----------------------------------------------------------------------------
195 static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
197 static char *kwlist[] = {"filename", NULL};
202 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &filename))
205 font = swf_LoadFont(filename);
206 mylog("font=%08x",font);
208 PyErr_SetString(PyExc_Exception, setError("Could not load %s", filename));
212 tag = (TagObject*)tag_new();
214 tag->tag = swf_InsertTag(0, ST_DEFINEFONT2);
216 swf_FontSetDefine2(tag->tag, tag->font);
217 mylog("+%08x(%d) DefineFont\n", (int)tag, tag->ob_refcnt);
218 return (PyObject*)tag;
220 //----------------------------------------------------------------------------
221 static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
223 static char *kwlist[] = {"password", NULL};
227 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
230 tag = (TagObject*)tag_new();
231 tag->tag = swf_InsertTag(0, ST_PROTECT);
233 swf_SetPassword(tag->tag, password);
235 mylog("+%08x(%d) f_Protect", (int)tag, tag->ob_refcnt);
236 return (PyObject*)tag;
238 //----------------------------------------------------------------------------
239 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
241 static char *kwlist[] = {"font", "text", "size", "color", NULL};
248 RGBA rgba = {255,0,0,0};
253 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!u#i|O!", kwlist, &TagClass, &font, &text, &textlen, &size, &ColorClass, &color))
256 unicode16 = PyUnicode_DecodeUTF16(text, textlen*2, NULL, NULL);
257 unicode8 = PyUnicode_AsUTF8String(unicode16);
258 text = PyString_AS_STRING(unicode8);
261 rgba = color_getRGBA(color);
263 mylog("DefineText: text = %s", text);
265 tag = (TagObject*)tag_new();
267 /* notice: we do modify the (passed) font object here,
268 for the swf_SetDefineText call, who will write out the font id. */
269 font->font->id = tagmap_add(tag->tagmap,(PyObject*)font); // add dependency on font
271 tag ->tag= swf_InsertTag(0, ST_DEFINETEXT2);
272 swf_SetU16(tag->tag, /*ID*/0);
273 r = swf_SetDefineText(tag->tag, font->font, &rgba, text, size);
274 mylog("+%08x(%d) DefineText %08x -> %08x\n", (int)tag, tag->ob_refcnt);
276 return (PyObject*)tag;
278 //----------------------------------------------------------------------------
279 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
281 static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
284 TagObject*character = 0;
293 po = malloc(sizeof(SWFPLACEOBJECT));
294 memset(po, 0, sizeof(SWFPLACEOBJECT));
296 swf_GetPlaceObject(0, po);
298 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!i|O!O!isiO!", kwlist,
299 &TagClass, &character,
301 &MatrixClass, &matrix,
302 &CXFormClass, &cxform,
311 po->clipdepth = clipdepth;
314 if(clipdepth) po->clipdepth = clipdepth;
315 if(matrix) po->matrix = matrix_getMatrix(matrix);
316 if(cxform) po->cxform = colortransform_getCXForm(cxform);
317 if(action) po->actions = action_getAction(action);
319 tag = (TagObject*)tag_new();
320 tag->placeobject = po;
321 Py_INCREF(character);
322 tag->character = (PyObject*)character;
323 tag->tag= swf_InsertTag(0, ST_PLACEOBJECT2);
325 po->id = tagmap_add(tag->tagmap,(PyObject*)character);
327 swf_SetPlaceObject(tag->tag, po);
328 mylog("+%08x(%d) PlaceObject %08x\n", (int)tag, tag->ob_refcnt, character);
329 return (PyObject*)tag;
333 TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap)
335 TagObject*tag = (TagObject*)self;
337 TAG* t = swf_InsertTag(prevTag, tag->tag->id);
338 swf_SetBlock(t, tag->tag->data, tag->tag->len);
340 mylog(" %08x(%d) tag_getTAG tagmap=%08x tag=%08x/%08x\n", (int)self, self->ob_refcnt, tagmap, tag->tag, t);
342 if(swf_isDefiningTag(t)) {
343 int newid = tagmap_add(tagmap, self);
344 swf_SetDefineID(t, newid);
347 int num = swf_GetNumUsedIDs(t);
348 int * positions = malloc(num*sizeof(int));
349 swf_GetUsedIDs(t, positions);
352 int id = GET16(&t->data[positions[i]]);
353 PyObject* obj = tagmap_id2obj(tag->tagmap, id);
355 PyErr_SetString(PyExc_Exception, setError("Internal error: id %d not known in taglist:"));
358 int newid = tagmap_obj2id(tagmap, obj);
359 PUT16(&t->data[positions[i]], newid);
364 PyObject* tag_getDependencies(PyObject*self)
366 mylog(" %08x(%d) tag_getDependencies\n", (int)self, self->ob_refcnt);
367 TagObject*tag = (TagObject*)self;
368 return tagmap_getObjectList(tag->tagmap);
371 PyTypeObject TagClass =
373 PyObject_HEAD_INIT(NULL)
376 tp_basicsize: sizeof(TagObject),
378 tp_dealloc: tag_dealloc,
380 tp_getattr: tag_getattr,
382 static PyMethodDef TagMethods[] =
385 {"SetBackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
386 {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
387 {"DefineFont", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
388 {"DefineText", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
389 {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},
390 {NULL, NULL, 0, NULL}
392 PyMethodDef* tag_getMethods()
394 TagClass.ob_type = &PyType_Type;