-int swf_FontSetID(SWFFONT * f,U16 id) { if (!f) return -1; f->id = id; return 0; }
-
-int swf_FontReduce(SWFFONT * f,FONTUSAGE * use)
-{ int i,j;
- if ((!f)||(!use)) return -1;
-
- j = 0;
- for (i=0;i<f->numchars;i++)
- if (f->glyph[i].shape)
- { if (f->glyph2ascii[i]<MAX_CHAR_PER_FONT &&
- 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
- { swf_ShapeFree(f->glyph[i].shape);
- f->ascii2glyph[f->glyph2ascii[i]] = -1;
- f->glyph2ascii[i] = 0;
- f->glyph[i].shape = NULL;
- f->glyph[i].advance = 0;
- }
- } else f->ascii2glyph[f->glyph2ascii[i]] = -1;
-
- f->numchars = j;
-
- return j;
+void swf_FontSort(SWFFONT * font)
+{
+ int i, j, k;
+ int *newplace;
+ int *newpos;
+ if (!font)
+ return;
+
+ newplace = rfx_alloc(sizeof(int) * font->numchars);
+
+ for (i = 0; i < font->numchars; i++) {
+ newplace[i] = i;
+ }
+ 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 = 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(newpos);
+ rfx_free(newplace);
+}
+
+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 = rfx_alloc(sizeof(FONTUSAGE));
+ f->use->is_reduced = 0;
+ f->use->chars = 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]);
+ s++;
+ }
+ return 0;
+}
+
+int swf_FontUseUTF8(SWFFONT * f, U8 * s)
+{
+ if( (!s))
+ return -1;
+ int ascii;
+ while (*s)
+ {
+ ascii = readUTF8char(&s);
+ if(ascii < f->maxascii && f->ascii2glyph[ascii]>=0)
+ swf_FontUseGlyph(f, f->ascii2glyph[ascii]);
+ }
+ return 0;