+int compare_placements(const void *v1, const void *v2)
+{
+ SWFPLACEOBJECT*p1 = (SWFPLACEOBJECT*)v1;
+ SWFPLACEOBJECT*p2 = (SWFPLACEOBJECT*)v2;
+ if(p1->depth != p2->depth)
+ return (int)p1->depth - (int)p2->depth;
+ else
+ if(p2->clipdepth)
+ return 1; // do the clip first
+ else
+ return -1;
+
+/* if(!p1->clipdepth) {
+ if(!p2->clipdepth) {
+ // !p1->clipdepth && !p2->clipdepth
+ return (int)p1->depth - (int)p2->depth;
+ } else {
+ // !p1->clipdepth && p2->clipdepth
+ if(p1->depth != p2->clipdepth)
+ return (int)p1->depth - (int)p2->clipdepth;
+ else
+ return 1; // do the clip first
+ }
+ } else {
+ if(!p2->clipdepth) {
+ // p1->clipdepth && !p2->clipdepth
+ if(p1->clipdepth != p2->depth)
+ return (int)p1->clipdepth - (int)p2->depth;
+ else
+ return -1;// do the clip first
+ } else {
+ if(p1->clipdepth != p2->clipdepth)
+ return (int)p1->clipdepth - (int)p2->clipdepth;
+ else
+ return (int)p1->depth - (int)p2->depth;
+ }
+ }*/
+}
+
+typedef struct textcallbackblock
+{
+ character_t*idtable;
+ U16 depth;
+ U16 clipdepth;
+ CXFORM* cxform;
+ MATRIX m;
+ RENDERBUF*buf;
+} textcallbackblock_t;
+
+static void textcallback(void*self, int*chars, int*xpos, int nr, int fontid, int fontsize,
+ int xstart, int ystart, RGBA* color)
+{
+ textcallbackblock_t * info = (textcallbackblock_t*)self;
+ font_t*font = 0;
+ int t;
+ if(info->idtable[fontid].type != font_type) {
+ fprintf(stderr, "ID %d is not a font\n", fontid);
+ return;
+ } else if(!info->idtable[fontid].obj.font) {
+ fprintf(stderr, "Font %d unknown\n", fontid);
+ return;
+ } else {
+ font = info->idtable[fontid].obj.font;
+ }
+ for(t=0;t<nr;t++) {
+ int x = xstart + xpos[t];
+ int y = ystart;
+ MATRIX m = info->m;
+ SPOINT p;
+
+ p.x = x; p.y = y;
+ p = swf_TurnPoint(p, &m);
+
+ m.sx = (m.sx * fontsize) / 1024;
+ m.sy = (m.sy * fontsize) / 1024;
+ m.r0 = (m.r0 * fontsize) / 1024;
+ m.r1 = (m.r1 * fontsize) / 1024;
+ m.tx = p.x;
+ m.ty = p.y;
+
+ if(chars[t]<0 || chars[t]>= font->numchars) {
+ fprintf(stderr, "Character out of range: %d\n", chars[t]);
+ } else {
+ SHAPE2*shape = font->glyphs[chars[t]];
+ shape->fillstyles[0].color = *color; //q&d
+ /*printf("Rendering char %d (size %d, x:%d, y:%d) color:%02x%02x%02x%02x\n", chars[t], fontsize, x, y,
+ color->a, color->r, color->g, color->b);
+ swf_DumpMatrix(stdout, &m);
+ swf_DumpShape(shape);*/
+ swf_RenderShape(info->buf, shape, &m, info->cxform, info->depth, info->clipdepth);
+ }
+ }
+}
+
+static void renderFromTag(RENDERBUF*buf, character_t*idtable, TAG*firstTag, MATRIX*m)
+{
+ TAG*tag = 0;
+ int numplacements = 0;
+ SWFPLACEOBJECT* placements;
+
+ tag = firstTag;
+ numplacements = 0;
+ while(tag) {
+ if(tag->id == ST_PLACEOBJECT ||
+ tag->id == ST_PLACEOBJECT2) {
+ numplacements++;
+ }
+ if(tag->id == ST_SHOWFRAME || tag->id == ST_END)
+ break;
+ tag = tag->next;
+ }
+ placements = (SWFPLACEOBJECT*)rfx_calloc(sizeof(SWFPLACEOBJECT)*numplacements);
+ numplacements = 0;
+
+ tag = firstTag;
+ while(tag) {
+ if(swf_isPlaceTag(tag)) {
+ SWFPLACEOBJECT p;
+ swf_GetPlaceObject(tag, &p);
+ /* TODO: add move and deletion */
+ placements[numplacements++] = p;
+ swf_PlaceObjectFree(&p); //dirty! but it only frees fields we don't use
+ }
+ if(tag->id == ST_SHOWFRAME || tag->id == ST_END)
+ break;
+ tag = tag->next;
+ }
+
+ qsort(placements, numplacements, sizeof(SWFPLACEOBJECT), compare_placements);
+
+ int t;
+ for(t=0;t<numplacements;t++) {
+ SWFPLACEOBJECT*p = &placements[t];
+ int id = p->id;
+ MATRIX m2;
+ swf_MatrixJoin(&m2, m, &p->matrix);
+
+ if(!idtable[id].tag) {
+ fprintf(stderr, "rfxswf: Id %d is unknown\n", id);
+ continue;
+ }
+
+ if(idtable[id].type == shape_type) {
+ //SRECT sbbox = swf_TurnRect(*idtable[id].bbox, &p->matrix);
+ swf_RenderShape(buf, idtable[id].obj.shape, &m2, &p->cxform, p->depth, p->clipdepth);
+ } else if(idtable[id].type == sprite_type) {
+ swf_UnFoldSprite(idtable[id].tag);
+ renderFromTag(buf, idtable, idtable[id].tag->next, &m2);
+ swf_FoldSprite(idtable[id].tag);
+ } else if(idtable[id].type == text_type) {
+ TAG* tag = idtable[id].tag;
+ textcallbackblock_t info;
+ MATRIX mt;
+
+ swf_SetTagPos(tag, 0);
+ swf_GetU16(tag);
+ swf_GetRect(tag,0);
+ swf_GetMatrix(tag,&mt);
+ swf_MatrixJoin(&info.m, &m2, &mt);
+ /*printf("Text matrix:\n");
+ swf_DumpMatrix(stdout, &m);
+ printf("Placement matrix:\n");
+ swf_DumpMatrix(stdout, &p->matrix);
+ printf("Final matrix:\n");
+ swf_DumpMatrix(stdout, &info.m);*/
+
+ info.idtable = idtable;
+ info.depth = p->depth;
+ info.cxform = &p->cxform;
+ info.clipdepth = p->clipdepth;
+ info.buf = buf;
+
+ swf_ParseDefineText(tag, textcallback, &info);
+ } else if(idtable[id].type == edittext_type) {
+ TAG* tag = idtable[id].tag;
+ U16 flags = swf_GetBits(tag, 16);
+ if(flags & ET_HASTEXT) {
+ fprintf(stderr, "edittext not supported yet (id %d)\n", id);
+ }
+ } else {
+ fprintf(stderr, "Unknown/Unsupported Object Type for id %d: %s\n", id, swf_TagGetName(idtable[id].tag));
+ }
+ }
+
+ free(placements);
+}
+