Commits

kaiwa committed a64084e

Refactored swffont to gfxfont conversion

Comments (0)

Files changed (1)

lib/readers/swf.c

 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;
-    gfxfont_t*font = 0;
-    int t;
-    character_t*cfont = map16_get_id(info->r->id2char, fontid);
-    gfxcolor_t* gfxcolor = (gfxcolor_t*) color;
+    gfxcolor_t*           gfxcolor = (gfxcolor_t*) color;
+    textcallbackblock_t*  info     = (textcallbackblock_t*)self;
+    character_t*          cfont    = map16_get_id(info->r->id2char, fontid);
+    gfxfont_t*            font     = (gfxfont_t*) rfx_calloc(sizeof(gfxfont_t*));
+    gfxdevice_t*          device   = (gfxdevice_t*) rfx_calloc(sizeof(gfxdevice_t*));
+    int                   t;
 
     if(!cfont) {
 	fprintf(stderr, "Font %d unknown\n", fontid);
 	fprintf(stderr, "ID %d is not a font\n", fontid);
 	return;
     }
-    font = cfont->data;
 
-    info->r->device->addfont(info->r->device, font);
+    font   = cfont->data;
+    device = info->r->device;
+
+    device->addfont(device, font);
 
     for(t=0;t<nr;t++) {
 	int x = xstart + xpos[t];
 	if(chars[t]<0 || chars[t]>= font->num_glyphs) {
 	    fprintf(stderr, "Character out of range: %d\n", chars[t]);
 	} else {
-            info->r->device->drawchar(info->r->device, font, chars[t], gfxcolor, &gm);
+            device->drawchar(device, font, chars[t], gfxcolor, &gm);
 	}
     }
 }
 
+gfxfont_t* swffont_to_gfxfont(SWFFONT *swffont, int transform)
+{
+	    gfxfont_t*font = (gfxfont_t*) rfx_calloc(sizeof(gfxfont_t));
+
+            font->id         = swffont->name;
+            font->num_glyphs = swffont->numchars;
+            font->glyphs     = (gfxglyph_t*) rfx_calloc(sizeof(gfxglyph_t) * swffont->numchars);
+
+            int t;
+            RGBA color_white = {255,255,255,255};
+
+            for (t=0; t < font->num_glyphs; t++) {
+                gfxglyph_t glyph;
+
+                if (!swffont->glyph[t].shape->fillstyle.n) {
+                    swf_ShapeAddSolidFillStyle(swffont->glyph[t].shape, &color_white);
+                }
+
+                SHAPE2 *shape2 = swf_ShapeToShape2(swffont->glyph[t].shape);
+
+                glyph.line = swfline_to_gfxline(shape2->lines, 0, swffont->glyph[t].shape->fillstyle.n);
+
+		if (transform) {
+                    gfxmatrix_t matrix = {1/20.0,0,0, 0,1/20.0,0};
+		    gfxline_transform(glyph.line, &matrix);
+		}
+                
+                swf_Shape2Free(shape2);
+
+                glyph.advance = swffont->glyph[t].advance;
+                glyph.name    = getUTF8(swffont->glyph2ascii[t]);
+                glyph.unicode = swffont->glyph2ascii[t];
+
+                font->glyphs[t] = glyph;
+            }
+            //swf_FontFree(swffont);
+
+            return font;
+}
 
 //---- tag handling ----
 
 	    id = swf_GetDefineID(tag);
 	}
 
+        character_t*c = rfx_calloc(sizeof(character_t));
+
 	if(tag->id == ST_DEFINESPRITE) {
-	    character_t*c = rfx_calloc(sizeof(character_t));
 	    sprite_t*s = rfx_calloc(sizeof(sprite_t));
 	    swf_SetTagPos(tag, 0);
 	    swf_GetU16(tag); //id
 	else if(tag->id == ST_DEFINESHAPE ||
 		tag->id == ST_DEFINESHAPE2 ||
 		tag->id == ST_DEFINESHAPE3) {
-	    character_t*c = rfx_calloc(sizeof(character_t));
 	    c->tag = tag;
 	    c->type = TYPE_SHAPE;
 	    map16_add_id(map, id, c);
 	else if(tag->id == ST_DEFINEFONT ||
 		tag->id == ST_DEFINEFONT2 ||
 		tag->id == ST_DEFINEFONT3) {
-	    character_t*c = rfx_calloc(sizeof(character_t));
-	    SWFFONT*swffont = 0;
-	    gfxfont_t*font = (gfxfont_t*)rfx_calloc(sizeof(gfxfont_t));
-	    swf_FontExtract(swf, id, &swffont);
-            font->id         = swffont->name;
-            font->num_glyphs = swffont->numchars;
-            font->glyphs     = (gfxglyph_t*)rfx_calloc(sizeof(gfxglyph_t*)*font->num_glyphs);
-            int t;
-            RGBA color_white = {255,255,255,255};
-            for(t=0;t<font->num_glyphs;t++) {
-                if(!swffont->glyph[t].shape->fillstyle.n) {
-                    swf_ShapeAddSolidFillStyle(swffont->glyph[t].shape, &color_white);
-                }
-                SHAPE2*s2 = swf_ShapeToShape2(swffont->glyph[t].shape);
-                font->glyphs[t].line = swfline_to_gfxline(s2->lines, 0, 1);
 
-		if(tag->id==ST_DEFINEFONT3) {
-		    gfxmatrix_t m = {1/20.0,0,0, 0,1/20.0,0};
-		    gfxline_transform(font->glyphs[t].line, &m);
-		}
-                //swf_Shape2Free(s2);
-                
-                font->glyphs[t].advance = swffont->glyph[t].advance;
-                font->glyphs[t].name    = getUTF8(swffont->glyph2ascii[t]);
-                font->glyphs[t].unicode = swffont->glyph2ascii[t];
-            }
-            //swf_FontFree(swffont);
+	    SWFFONT *swffont = 0;
+            int transform = (tag->id == ST_DEFINEFONT3);
+
+            swf_FontExtract(swf, id, &swffont);
+
+            gfxfont_t* gfxfont = (gfxfont_t*) rfx_calloc(sizeof(gfxfont_t*));
+            gfxfont = swffont_to_gfxfont(swffont, transform);
 
 	    c->tag = tag;
 	    c->type = TYPE_FONT;
-	    c->data = font;
+	    c->data = gfxfont;
+
 	    map16_add_id(map, id, c);
 	}
 	else if(tag->id == ST_DEFINETEXT ||
 		tag->id == ST_DEFINETEXT2) {
-	    character_t*c = rfx_calloc(sizeof(character_t));
 	    c->tag = tag;
 	    c->type = TYPE_TEXT;
 	    c->data = 0;
 		tag->id == ST_DEFINEBITSJPEG3 ||
 		tag->id == ST_DEFINEBITSLOSSLESS || 
 		tag->id == ST_DEFINEBITSLOSSLESS2) {
-	    character_t*c = rfx_calloc(sizeof(character_t));
 	    int width, height;
 	    void*data = swf_ExtractImage(tag, &width, &height);
 	    gfximage_t*b = gfximage_new(data, width, height);