-int swf_FontExtract(SWF * swf,int id,SWFFONT * * font)
-{ TAG * t;
- SWFFONT * f;
-
- if ((!swf)||(!font)) return -1;
-
- f = (SWFFONT *)malloc(sizeof(SWFFONT));
- memset(f,0x00,sizeof(SWFFONT));
-
- t = swf->firstTag;
-
- while (t)
- { int nid = 0;
- switch (swf_GetTagID(t))
- { case ST_DEFINEFONT:
- nid = swf_FontExtract_DefineFont(id,f,t);
- break;
-
- case ST_DEFINEFONT2:
- nid = swf_FontExtract_DefineFont2(id,f,t);
- break;
-
- case ST_DEFINEFONTINFO:
- case ST_DEFINEFONTINFO2:
- nid = swf_FontExtract_DefineFontInfo(id,f,t);
- break;
-
- case ST_DEFINETEXT:
- case ST_DEFINETEXT2:
- nid = swf_FontExtract_DefineText(id,f,t,f->layout?0:FEDTJ_MODIFY);
- break;
-
- case ST_GLYPHNAMES:
- nid = swf_FontExtract_GlyphNames(id,f,t);
- break;
- }
- if (nid>0) id = nid;
- t = swf_NextTag(t);
- }
- if(f->id != id) {
- free(f);
- f=0;
- }
- font[0] = f;
- return 0;
+int swf_FontExtract(SWF * swf, int id, SWFFONT * *font)
+{
+ TAG *t;
+ SWFFONT *f;
+
+ if ((!swf) || (!font))
+ return -1;
+
+ f = (SWFFONT *) rfx_calloc(sizeof(SWFFONT));
+
+ t = swf->firstTag;
+
+ while (t) {
+ int nid = 0;
+ switch (swf_GetTagID(t)) {
+ case ST_DEFINEFONT:
+ nid = swf_FontExtract_DefineFont(id, f, t);
+ break;
+
+ case ST_DEFINEFONT2:
+ nid = swf_FontExtract_DefineFont2(id, f, t);
+ break;
+
+ case ST_DEFINEFONTINFO:
+ case ST_DEFINEFONTINFO2:
+ nid = swf_FontExtract_DefineFontInfo(id, f, t);
+ break;
+
+ case ST_DEFINETEXT:
+ case ST_DEFINETEXT2:
+ nid = swf_FontExtract_DefineText(id, f, t, f->layout ? 0 : FEDTJ_MODIFY);
+ break;
+
+ case ST_GLYPHNAMES:
+ nid = swf_FontExtract_GlyphNames(id, f, t);
+ break;
+ }
+ if (nid > 0)
+ id = nid;
+ t = swf_NextTag(t);
+ }
+ if (f->id != id) {
+ rfx_free(f);
+ f = 0;
+ }
+ font[0] = f;
+ return 0;
+}
+
+int swf_FontSetID(SWFFONT * f, U16 id)
+{
+ if (!f)
+ return -1;
+ f->id = id;
+ return 0;
+}
+
+void swf_LayoutFree(SWFLAYOUT * l)
+{
+ if (l) {
+ if (l->kerning)
+ rfx_free(l->kerning);
+ l->kerning = NULL;
+ if (l->bounds)
+ rfx_free(l->bounds);
+ l->bounds = NULL;
+ }
+ rfx_free(l);
+}
+
+
+static void font_freeglyphnames(SWFFONT*f)
+{
+ if (f->glyphnames)
+ {
+ int t;
+ for (t = 0; t < f->numchars; t++)
+ {
+ if (f->glyphnames[t])
+ {
+ rfx_free(f->glyphnames[t]);
+ f->glyphnames[t] = 0;
+ }
+ }
+ rfx_free(f->glyphnames);
+ f->glyphnames = 0;
+ }
+}
+static void font_freeusage(SWFFONT*f)
+{
+ if (f->use) {
+ if(f->use->chars) {
+ rfx_free(f->use->chars);f->use->chars = 0;
+ }
+ rfx_free(f->use); f->use = 0;
+ }
+}
+static void font_freelayout(SWFFONT*f)
+{
+ if (f->layout) {
+ swf_LayoutFree(f->layout);
+ f->layout = 0;
+ }
+}
+static void font_freename(SWFFONT*f)
+{
+ if (f->name) {
+ rfx_free(f->name);
+ f->name = 0;
+ }
+}
+
+int swf_FontReduce_old(SWFFONT * f)
+{
+ int i, j;
+ int max_unicode = 0;
+ if ((!f) || (!f->use) || f->use->is_reduced)
+ return -1;
+
+ j = 0;
+
+ for (i = 0; i < f->numchars; i++) {
+ if (f->glyph[i].shape && f->use->chars[i]) {
+ f->glyph2ascii[j] = f->glyph2ascii[i];
+ f->glyph[j] = f->glyph[i];
+ f->use->chars[i] = j;
+ j++;
+ } else {
+ f->glyph2ascii[i] = 0;
+ if(f->glyph[i].shape) {
+ swf_ShapeFree(f->glyph[i].shape);
+ f->glyph[i].shape = 0;
+ f->glyph[i].advance = 0;
+ }
+ f->use->chars[i] = -1;
+ j++; //TODO: remove
+ }
+ }
+ for (i = 0; i < f->maxascii; i++) {
+ if(f->use->chars[f->ascii2glyph[i]]<0) {
+ f->ascii2glyph[i] = -1;
+ } else {
+ f->ascii2glyph[i] = f->use->chars[f->ascii2glyph[i]];
+ max_unicode = i;
+ }
+ }
+ f->maxascii = max_unicode;
+ f->use->is_reduced = 1;
+ f->numchars = j;
+ font_freelayout(f);
+ font_freeglyphnames(f);
+ font_freename(f);
+ return j;
+}
+
+int swf_FontReduce_swfc(SWFFONT * f)
+{
+ int i, j;
+ int max_unicode = 0;
+ if ((!f) || (!f->use) || f->use->is_reduced)
+ return -1;
+
+ font_freeglyphnames(f);
+
+ j = 0;
+ for (i = 0; i < f->numchars; i++) {
+ if (f->glyph[i].shape && f->use->chars[i]) {
+ f->glyph2ascii[j] = f->glyph2ascii[i];
+ if (f->layout)
+ f->layout->bounds[j] = f->layout->bounds[i];
+ f->glyph[j] = f->glyph[i];
+ f->use->chars[i] = j;
+ j++;
+ } else {
+ f->glyph2ascii[i] = 0;
+ if(f->glyph[i].shape) {
+ swf_ShapeFree(f->glyph[i].shape);
+ f->glyph[i].shape = 0;
+ f->glyph[i].advance = 0;
+ }
+ f->use->chars[i] = -1;
+ }
+ }
+ f->use->used_glyphs = j;
+ for (i = 0; i < f->maxascii; i++) {
+ if(f->ascii2glyph[i] > -1) {
+ if (f->use->chars[f->ascii2glyph[i]]<0) {
+ f->use->chars[f->ascii2glyph[i]] = 0;
+ f->ascii2glyph[i] = -1;
+ } else {
+ f->ascii2glyph[i] = f->use->chars[f->ascii2glyph[i]];
+ f->use->chars[f->ascii2glyph[i]] = 1;
+ max_unicode = i + 1;
+ }
+ }
+ }
+ f->maxascii = max_unicode;
+ f->use->is_reduced = 1;
+ f->numchars = j;
+ font_freename(f);
+ return j;