+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_alloc(sizeof(FONTUSAGE));
+ f->use->is_reduced = 0;
+ f->use->used_glyphs = 0;
+ f->use->chars = (int*)rfx_calloc(sizeof(f->use->chars[0]) * f->numchars);
+ f->use->glyphs_specified = 0;
+ 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;
+}
+
+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;
+}
+
+int swf_FontUseGlyph(SWFFONT * f, int glyph)
+{
+ if (!f->use)
+ swf_FontInitUsage(f);
+ if(glyph < 0 || glyph >= f->numchars)
+ return -1;
+ if(!f->use->chars[glyph])
+ f->use->used_glyphs++;
+ f->use->chars[glyph] = 1;
+ return 0;
+}
+
+int swf_FontSetDefine(TAG * t, SWFFONT * f)
+{
+ U16 *ofs = (U16 *) rfx_alloc(f->numchars * 2);
+ int p, i, j;
+
+ if ((!t) || (!f))
+ return -1;
+ swf_ResetWriteBits(t);
+ swf_SetU16(t, f->id);
+
+ p = 0;
+ j = 0;
+ for (i = 0; i < f->numchars; i++)
+ if (f->glyph[i].shape) {
+ ofs[j++] = p;
+ p += swf_SetSimpleShape(NULL, f->glyph[i].shape);
+ }
+
+ for (i = 0; i < j; i++)
+ swf_SetU16(t, ofs[i] + j * 2);
+ if (!j) {
+ fprintf(stderr, "rfxswf: warning: Font is empty\n");
+ swf_SetU16(t, 0);
+ }
+
+ for (i = 0; i < f->numchars; i++)
+ if (f->glyph[i].shape)
+ swf_SetSimpleShape(t, f->glyph[i].shape);
+
+ swf_ResetWriteBits(t);
+ rfx_free(ofs);
+ return 0;
+}
+
+static inline int fontSize(SWFFONT * font)