+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;
+ }
+ if(f->use->neighbors) {
+ rfx_free(f->use->neighbors);f->use->neighbors = 0;
+ }
+ if(f->use->neighbors_hash) {
+ rfx_free(f->use->neighbors_hash);f->use->neighbors_hash = 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;
+}
+
+int swf_FontReduce(SWFFONT * f)
+{
+ int i;
+ int max_unicode = 0;
+ int max_glyph = 0;
+ if ((!f) || (!f->use) || f->use->is_reduced)
+ return -1;
+
+ font_freelayout(f);
+ font_freeglyphnames(f);
+
+ f->use->used_glyphs= 0;
+ for (i = 0; i < f->numchars; i++) {
+ if(!f->use->chars[i]) {
+ if(f->glyph2ascii) {
+ 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->used_glyphs++;
+ } else {
+ f->use->used_glyphs++;
+ max_glyph = i+1;
+ }
+ }
+ for (i = 0; i < f->maxascii; i++) {
+ if(f->ascii2glyph[i] > -1 && !f->use->chars[f->ascii2glyph[i]]) {
+ if(f->ascii2glyph) {
+ f->ascii2glyph[i] = -1;
+ }
+ } else {
+ max_unicode = i+1;
+ }
+ }
+ f->maxascii = max_unicode;
+ f->numchars = max_glyph;
+ font_freename(f);
+ return 0;
+}
+
+static SWFFONT* font_to_sort;
+int cmp_chars(const void*a, const void*b)
+{
+ int x = *(const int*)a;
+ int y = *(const int*)b;
+ return 0;
+}
+
+void swf_FontSort(SWFFONT * font)
+{
+ int i, j;
+ int *newplace;
+ int *newpos;
+ if (!font)
+ return;
+
+ newplace = (int*)rfx_alloc(sizeof(int) * font->numchars);
+
+ for (i = 0; i < font->numchars; i++) {
+ newplace[i] = i;
+ }
+ //qsort(newplace, sizeof(newplace[0]), font->numchars, cmp_chars);
+
+ for (i = 0; i < font->numchars; i++)
+ for (j = 0; j < i; j++) {
+ if (font->glyph2ascii[i] < font->glyph2ascii[j]) {
+ int n1, n2;
+ char *c1, *c2;
+ SWFGLYPH g1, g2;
+ SRECT r1, r2;
+ n1 = newplace[i];
+ n2 = newplace[j];
+ newplace[j] = n1;
+ newplace[i] = n2;
+ n1 = font->glyph2ascii[i];
+ n2 = font->glyph2ascii[j];
+ font->glyph2ascii[j] = n1;
+ font->glyph2ascii[i] = n2;
+ g1 = font->glyph[i];
+ g2 = font->glyph[j];
+ font->glyph[j] = g1;
+ font->glyph[i] = g2;
+ if (font->glyphnames) {
+ c1 = font->glyphnames[i];
+ c2 = font->glyphnames[j];
+ font->glyphnames[j] = c1;
+ font->glyphnames[i] = c2;
+ }
+ if (font->layout) {
+ r1 = font->layout->bounds[i];
+ r2 = font->layout->bounds[j];
+ font->layout->bounds[j] = r1;
+ font->layout->bounds[i] = r2;
+ }
+ }
+ }
+ newpos = (int*)rfx_alloc(sizeof(int) * font->numchars);
+ for (i = 0; i < font->numchars; i++) {
+ newpos[newplace[i]] = i;
+ }
+ for (i = 0; i < font->maxascii; i++) {
+ if (font->ascii2glyph[i] >= 0)
+ font->ascii2glyph[i] = newpos[font->ascii2glyph[i]];
+ }
+
+ rfx_free(newplace);
+ font->glyph2glyph = newpos;
+}
+
+void swf_FontPrepareForEditText(SWFFONT * font)
+{
+ if (!font->layout)
+ swf_FontCreateLayout(font);
+ swf_FontSort(font);
+}
+
+int swf_FontInitUsage(SWFFONT * f)
+{
+ if (!f)
+ return -1;
+ if(f->use) {
+ fprintf(stderr, "Usage initialized twice");
+ return -1;
+ }
+ f->use = (FONTUSAGE*)rfx_calloc(sizeof(FONTUSAGE));
+ f->use->smallest_size = 0xffff;
+ f->use->chars = (int*)rfx_calloc(sizeof(f->use->chars[0]) * f->numchars);
+ return 0;
+}
+
+void swf_FontClearUsage(SWFFONT * f)
+{
+ if (!f || !f->use)
+ return;
+ rfx_free(f->use->chars); f->use->chars = 0;
+ rfx_free(f->use); f->use = 0;
+}
+
+int swf_FontUse(SWFFONT * f, U8 * s)
+{
+ if( (!s))
+ return -1;
+ while (*s) {
+ if(*s < f->maxascii && f->ascii2glyph[*s]>=0)
+ swf_FontUseGlyph(f, f->ascii2glyph[*s], /*FIXME*/0xffff);
+ s++;
+ }
+ return 0;
+}
+
+int swf_FontUseUTF8(SWFFONT * f, const U8 * s, U16 size)
+{
+ if( (!s))
+ return -1;
+ int ascii;
+ while (*s)
+ {
+ ascii = readUTF8char((U8**)&s);
+ if(ascii < f->maxascii && f->ascii2glyph[ascii]>=0)
+ swf_FontUseGlyph(f, f->ascii2glyph[ascii], size);
+ }
+ return 0;
+}
+
+int swf_FontUseAll(SWFFONT* f)
+{
+ int i;
+
+ if (!f->use)
+ swf_FontInitUsage(f);
+ for (i = 0; i < f->numchars; i++)
+ f->use->chars[i] = 1;
+ f->use->used_glyphs = f->numchars;
+ return 0;
+}
+
+static unsigned hash2(int char1, int char2)
+{
+ unsigned hash = char1^(char2<<8);
+ hash += (hash << 3);
+ hash ^= (hash >> 11);
+ hash += (hash << 15);
+ return hash;
+}
+static void hashadd(FONTUSAGE*u, int char1, int char2, int nr)
+{
+ unsigned hash = hash2(char1, char2);
+ while(1) {
+ hash = hash%u->neighbors_hash_size;
+ if(!u->neighbors_hash[hash]) {
+ u->neighbors_hash[hash] = nr+1;
+ return;
+ }
+ hash++;
+ }
+}
+int swf_FontUseGetPair(SWFFONT * f, int char1, int char2)
+{
+ FONTUSAGE*u = f->use;
+ if(!u || !u->neighbors_hash_size)
+ return 0;
+ unsigned hash = hash2(char1, char2);
+ while(1) {
+ hash = hash%u->neighbors_hash_size;
+ int pos = u->neighbors_hash[hash];
+ if(!pos)
+ return 0;
+ if(pos &&
+ u->neighbors[pos-1].char1 == char1 &&
+ u->neighbors[pos-1].char2 == char2) {
+ return pos;
+ }
+ hash++;
+ }
+
+}
+void swf_FontUsePair(SWFFONT * f, int char1, int char2)
+{
+ if (!f->use)
+ swf_FontInitUsage(f);
+ FONTUSAGE*u = f->use;