+RGBA* swf_Render(RENDERBUF*dest)
+{
+ renderbuf_internal*i = (renderbuf_internal*)dest->internal;
+ RGBA* img = (RGBA*)rfx_alloc(sizeof(RGBA)*dest->width*dest->height);
+ int y;
+ int antialize = i->antialize;
+
+ if(antialize <= 1) /* no antializing */ {
+ for(y=0;y<i->height2;y++) {
+ RGBA*line = &i->img[y*i->width2];
+ memcpy(&img[y*dest->width], line, sizeof(RGBA)*dest->width);
+ }
+ } else {
+ RGBA**lines = (RGBA**)rfx_calloc(sizeof(RGBA*)*antialize);
+ int q = antialize*antialize;
+ int ypos = 0;
+ for(y=0;y<i->height2;y++) {
+ int n;
+ ypos = y % antialize;
+ lines[ypos] = &i->img[y*i->width2];
+ if(ypos == antialize-1) {
+ RGBA*out = &img[(y / antialize)*dest->width];
+ int x;
+ int r,g,b,a;
+ for(x=0;x<dest->width;x++) {
+ int xpos = x*antialize;
+ int yp;
+ U32 r=0,g=0,b=0,a=0;
+ for(yp=0;yp<antialize;yp++) {
+ RGBA*lp = &lines[yp][xpos];
+ int xp;
+ for(xp=0;xp<antialize;xp++) {
+ RGBA*p = &lp[xp];
+ r += p->r;
+ g += p->g;
+ b += p->b;
+ a += p->a;
+ }
+ }
+ out[x].r = r / q;
+ out[x].g = g / q;
+ out[x].b = b / q;
+ out[x].a = a / q;
+ }
+ }
+ }
+ rfx_free(lines);
+ }
+ return img;
+}
+
+typedef struct
+{
+ int numchars;
+ SHAPE2**glyphs;
+} font_t;
+
+typedef struct
+{
+ TAG*tag;
+ SRECT*bbox;
+ enum {none_type, shape_type, image_type, text_type, font_type} type;
+ union {
+ SHAPE2*shape;
+ font_t*font;
+ } obj;
+} character_t;
+
+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].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);
+ }
+ }
+}
+
+void swf_RenderSWF(RENDERBUF*buf, SWF*swf)
+{
+ TAG*tag;
+ int t;
+ int numplacements;
+ RGBA color;
+ SWFPLACEOBJECT* placements;
+
+ character_t* idtable = rfx_calloc(sizeof(character_t)*65536); // id to character mapping
+ SWFPLACEOBJECT** depthtable = rfx_calloc(sizeof(SWFPLACEOBJECT*)*65536); // depth to placeobject mapping
+
+ tag = swf->firstTag;
+ numplacements = 0;
+ while(tag) {
+ if(tag->id == ST_PLACEOBJECT ||
+ tag->id == ST_PLACEOBJECT2) {
+ numplacements++;
+ }
+ tag = tag->next;
+ }
+ placements = rfx_calloc(sizeof(SWFPLACEOBJECT)*numplacements);
+ numplacements = 0;
+
+ /* set background color */
+ color = swf_GetSWFBackgroundColor(swf);
+ swf_Render_SetBackgroundColor(buf, color);
+
+ /* parse definitions */
+ tag = swf->firstTag;
+ while(tag) {
+ if(swf_isDefiningTag(tag)) {
+ int id = swf_GetDefineID(tag);
+ idtable[id].tag = tag;
+ idtable[id].bbox = rfx_alloc(sizeof(SRECT));
+ *idtable[id].bbox = swf_GetDefineBBox(tag);
+
+ if(swf_isShapeTag(tag)) {
+ SHAPE2* shape = rfx_calloc(sizeof(SHAPE2));
+ swf_ParseDefineShape(tag, shape);
+ idtable[id].type = shape_type;
+ idtable[id].obj.shape = shape;
+ } else if(swf_isImageTag(tag)) {
+ int width,height;
+ RGBA*data = swf_ExtractImage(tag, &width, &height);
+ idtable[id].type = image_type;
+ swf_Render_AddImage(buf, id, data, width, height);
+ free(data);
+ } else if(tag->id == ST_DEFINEFONT ||
+ tag->id == ST_DEFINEFONT2) {
+ int t;
+ SWFFONT*swffont;
+ font_t*font = rfx_calloc(sizeof(font_t));
+ idtable[id].obj.font = font;
+ swf_FontExtract(swf,id,&swffont);
+ font->numchars = swffont->numchars;
+ font->glyphs = rfx_calloc(sizeof(SHAPE2*)*font->numchars);
+ for(t=0;t<font->numchars;t++) {
+ if(!swffont->glyph[t].shape->fillstyle.n) {
+ /* the actual fill color will be overwritten while rendering */
+ swf_ShapeAddSolidFillStyle(swffont->glyph[t].shape, &color_white);
+ }
+ font->glyphs[t] = swf_ShapeToShape2(swffont->glyph[t].shape);
+ }
+ swf_FontFree(swffont);
+ idtable[id].type = font_type;
+
+ } else if(tag->id == ST_DEFINEFONTINFO ||
+ tag->id == ST_DEFINEFONTINFO2) {
+ idtable[id].type = font_type;
+ } else if(tag->id == ST_DEFINETEXT ||
+ tag->id == ST_DEFINETEXT2) {
+ idtable[id].type = text_type;