X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fmodules%2Fswftext.c;h=b0d3de5be79bac50236047895cc118c3b2353050;hb=ac58a40c594fa441eb4192cc4c86068372a7f18e;hp=f9840ed4cf0cb9517397cbb65952eea6502023f1;hpb=8c9f2f5a3419e0582c11f411bac26bc132af7234;p=swftools.git diff --git a/lib/modules/swftext.c b/lib/modules/swftext.c index f9840ed..b0d3de5 100644 --- a/lib/modules/swftext.c +++ b/lib/modules/swftext.c @@ -312,6 +312,8 @@ int swf_FontExtract_DefineFont2(int id, SWFFONT * font, TAG * tag) if (glyphcount) /* this _if_ is not in the specs */ swf_GetU16(tag); // fontcodeoffset } + /* TODO: we should use the offset positions, not just + blindly read in shapes */ for (t = 0; t < glyphcount; t++) swf_GetSimpleShape(tag, &(font->glyph[t].shape)); @@ -543,43 +545,109 @@ int swf_FontSetID(SWFFONT * f, U16 id) return 0; } -int swf_FontReduce(SWFFONT * f, FONTUSAGE * use) +void swf_LayoutFree(SWFLAYOUT * l) +{ + if (l) { + if (l->kerning) + free(l->kerning); + l->kerning = NULL; + if (l->bounds) + free(l->bounds); + l->bounds = NULL; + } + free(l); +} + + +static void font_freeglyphnames(SWFFONT*f) +{ + if (f->glyphnames) { + int t; + for (t = 0; t < f->numchars; t++) { + if (f->glyphnames[t]) + free(f->glyphnames[t]); + } + free(f->glyphnames); + f->glyphnames = 0; + } + +} +static void font_freeusage(SWFFONT*f) +{ + if (f->use) { + if(f->use->chars) { + free(f->use->chars);f->use->chars = 0; + } + 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) { + free(f->name); + f->name = 0; + } +} + +int swf_FontReduce(SWFFONT * f) { int i, j; - if ((!f) || (!use)) + int max_unicode = 0; + if ((!f) || (!f->use) || f->use->is_reduced) return -1; - /* TODO: layout, glyphnames */ j = 0; - for (i = 0; i < f->numchars; i++) - if (f->glyph[i].shape) { - if (f->glyph2ascii[i] < f->maxascii && use->code[f->glyph2ascii[i]]) { - f->ascii2glyph[f->glyph2ascii[i]] = j; - f->glyph2ascii[j] = f->glyph2ascii[i]; - f->glyph[j] = f->glyph[i]; - j++; - } else { + + 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->ascii2glyph[f->glyph2ascii[i]] = -1; - f->glyph2ascii[i] = 0; - f->glyph[i].shape = NULL; + f->glyph[i].shape = 0; f->glyph[i].advance = 0; } - } else - f->ascii2glyph[f->glyph2ascii[i]] = -1; - + 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; } void swf_FontSort(SWFFONT * font) { - if (!font) - return; int i, j, k; - int *newplace = malloc(sizeof(int) * font->numchars); + int *newplace; int *newpos; + if (!font) + return; + + newplace = malloc(sizeof(int) * font->numchars); for (i = 0; i < font->numchars; i++) { newplace[i] = i; @@ -637,33 +705,53 @@ void swf_FontPrepareForEditText(SWFFONT * font) swf_FontSort(font); } -int swf_FontInitUsage(SWFFONT * f, FONTUSAGE * use) +int swf_FontInitUsage(SWFFONT * f) { - if (!use) + if (!f) return -1; - use->code = malloc(sizeof(use->code[0]) * f->maxascii); - memset(use->code, 0, sizeof(use->code[0]) * f->maxascii); + if(f->use) { + fprintf(stderr, "Usage initialized twice"); + return -1; + } + f->use = malloc(sizeof(FONTUSAGE)); + f->use->is_reduced = 0; + f->use->chars = malloc(sizeof(f->use->chars[0]) * f->numchars); + memset(f->use->chars, 0, sizeof(f->use->chars[0]) * f->numchars); return 0; } -void swf_FontClearUsage(SWFFONT * f, FONTUSAGE * use) +void swf_FontClearUsage(SWFFONT * f) { - if (!use) + if (!f || !f->use) return; - free(use->code); + free(f->use->chars); f->use->chars = 0; + free(f->use); f->use = 0; } -int swf_FontUse(SWFFONT * f, FONTUSAGE * use, U8 * s) +int swf_FontUse(SWFFONT * f, U8 * s) { - if ((!use) || (!s)) + if (!f->use) + swf_FontInitUsage(f); + if( (!s)) return -1; - while (s[0]) { - use->code[s[0]] = 1; + while (*s) { + if(*s < f->maxascii && f->ascii2glyph[*s]>=0) + f->use->chars[f->ascii2glyph[*s]] = 1; s++; } return 0; } +int swf_FontUseGlyph(SWFFONT * f, int glyph) +{ + if (!f->use) + swf_FontInitUsage(f); + if(glyph < 0 || glyph > f->numchars) + return -1; + f->use->chars[glyph] = 1; + return 0; +} + int swf_FontSetDefine(TAG * t, SWFFONT * f) { U16 *ofs = (U16 *) malloc(f->numchars * 2); @@ -703,7 +791,11 @@ static inline int fontSize(SWFFONT * font) int t; int size = 0; for (t = 0; t < font->numchars; t++) { - int l = (font->glyph[t].shape->bitlen + 7) / 8; + int l = 0; + if(font->glyph[t].shape) + l = (font->glyph[t].shape->bitlen + 7) / 8; + else + l = 8; size += l + 1; } return size + (font->numchars + 1) * 2; @@ -745,7 +837,7 @@ int swf_FontSetDefine2(TAG * tag, SWFFONT * f) swf_SetBlock(tag, f->name, strlen(f->name)); } else { /* font name (="") */ - swf_SetU8(tag, 0); /*placeholder */ + swf_SetU8(tag, 0); } /* number of glyphs */ swf_SetU16(tag, f->numchars); @@ -772,8 +864,13 @@ int swf_FontSetDefine2(TAG * tag, SWFFONT * f) tag->data[pos + t * 2] = (tag->len - pos); tag->data[pos + t * 2 + 1] = (tag->len - pos) >> 8; } - if (t < f->numchars) - swf_SetSimpleShape(tag, f->glyph[t].shape); + if (t < f->numchars) { + if(f->glyph[t].shape) { + swf_SetSimpleShape(tag, f->glyph[t].shape); + } else { + swf_SetU8(tag, 0); //non-edge(1) + edge flags(5) + } + } } @@ -874,58 +971,34 @@ int swf_TextPrintDefineText(TAG * t, SWFFONT * f) return 0; } -void swf_LayoutFree(SWFLAYOUT * l) -{ - if (l) { - if (l->kerning) - free(l->kerning); - l->kerning = NULL; - if (l->bounds) - free(l->bounds); - l->bounds = NULL; - } - free(l); -} - void swf_FontFree(SWFFONT * f) { - if (f) { - int i; - - if (f->name) - free(f->name); - if (f->layout) - swf_LayoutFree(f->layout); - - f->name = NULL; - f->layout = NULL; + int i; + if (!f) + return; - if (f->glyph) { - for (i = 0; i < f->numchars; i++) - if (f->glyph[i].shape) { - swf_ShapeFree(f->glyph[i].shape); - f->glyph[i].shape = NULL; - } - free(f->glyph); - f->glyph = NULL; - } - if (f->ascii2glyph) { - free(f->ascii2glyph); - f->ascii2glyph = NULL; - } - if (f->glyph2ascii) { - free(f->glyph2ascii); - f->glyph2ascii = NULL; - } - if (f->glyphnames) { - int t; - for (t = 0; t < f->numchars; t++) { - if (f->glyphnames[t]) - free(f->glyphnames[t]); + if (f->glyph) { + for (i = 0; i < f->numchars; i++) + if (f->glyph[i].shape) { + swf_ShapeFree(f->glyph[i].shape); + f->glyph[i].shape = NULL; } - free(f->glyphnames); - } + free(f->glyph); + f->glyph = NULL; + } + if (f->ascii2glyph) { + free(f->ascii2glyph); + f->ascii2glyph = NULL; + } + if (f->glyph2ascii) { + free(f->glyph2ascii); + f->glyph2ascii = NULL; } + font_freename(f); + font_freelayout(f); + font_freeglyphnames(f); + font_freeusage(f); + free(f); } @@ -947,10 +1020,16 @@ int swf_TextSetInfoRecord(TAG * t, SWFFONT * font, U16 size, RGBA * color, int d else swf_SetRGB(t, color); } - if (dx) + if (dx) { + if(dx>32767 || dx<-32768) + fprintf(stderr, "Warning: Horizontal char position overflow: %d\n", dx); swf_SetS16(t, dx); - if (dy) + } + if (dy) { + if(dy>32767 || dy<-32768) + fprintf(stderr, "Warning: Vertical char position overflow: %d\n", dy); swf_SetS16(t, dy); + } if (font) swf_SetU16(t, size); @@ -1108,7 +1187,7 @@ SWFFONT *swf_ReadFont(char *filename) SWF swf; if (!filename) return 0; - f = open(filename, O_RDONLY); + f = open(filename, O_RDONLY|O_BINARY); if (f < 0 || swf_ReadSWF(f, &swf) < 0) { fprintf(stderr, "%s is not a valid SWF font file or contains errors.\n", filename); @@ -1281,7 +1360,7 @@ void swf_WriteFont(SWFFONT * font, char *filename) t = swf_InsertTag(t, ST_END); - f = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644); + f = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0644); if FAILED (swf_WriteSWF(f, &swf)) fprintf(stderr, "WriteSWF() failed in writeFont().\n"); close(f);