- else
- { int i;
- int buf[256];
- for (i=0;i<flags;i++)
- { int glyph;
- int adv;
- glyph = swf_GetBits(t,gbits);
- adv = swf_GetBits(t,abits);
- if (id==fid) // mitlesen ?
- if (jobs&FEDTJ_PRINT) {
- { int code = f->glyph2ascii[glyph];
- printf("%c",code);
- }
- if (jobs&FEDTJ_MODIFY)
- /*if (!f->glyph[code].advance)*/ f->glyph[glyph].advance = adv;
- }
- buf[i] = glyph;
- }
- if ((id==fid)&&(jobs&FEDTJ_PRINT)) printf("\n");
- if (jobs&FEDTJ_CALLBACK)
- callback(buf, flags, fid);
- }
- flags = swf_GetU8(t);
- }
-
- swf_RestoreTagPos(t);
- return id;
-}
-
-int swf_FontExtract_DefineText(int id,SWFFONT * f,TAG * t,int jobs)
-{
- return swf_FontExtract_DefineTextCallback(id,f,t,jobs,0);
-}
-
-int swf_FontExtract(SWF * swf,int id,SWFFONT * * font)
-{ TAG * t;
- SWFFONT * f;
-
- if ((!swf)||(!font)) return -1;
-
- f = (SWFFONT *)malloc(sizeof(SWFFONT)); font[0] = f;
- if (!f) return -1;
-
- 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:
- 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;
- }
- if (nid>0) id = nid;
- t = swf_NextTag(t);
- }
- return 0;
-}
-
-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;
-}
-
-int swf_FontInitUsage(FONTUSAGE * use)
-{ if (!use) return -1;
- memset(use->code,0,sizeof(use->code[0])*MAX_CHAR_PER_FONT);
- return 0;
-}
-
-int swf_FontUse(FONTUSAGE * use,U8 * s)
-{ if ((!use)||(!s)) return -1;
- while (s[0])
- { use->code[s[0]] = 1;
- s++;
- }
- return 0;
-}
-
-int swf_FontSetDefine(TAG * t,SWFFONT * f)
-{ U16*ofs = (U16*)malloc(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);
-
- for (i=0;i<f->numchars;i++)
- if (f->glyph[i].shape)
- swf_SetSimpleShape(t,f->glyph[i].shape);
-
- swf_ResetWriteBits(t);
- free(ofs);
- return 0;
-}
-
-int swf_FontSetDefine2(TAG *tag, SWFFONT * f)
+
+ swf_RestoreTagPos(t);
+ return id;
+}
+
+int swf_ParseDefineText(TAG * tag,
+ void (*callback) (void *self, int *chars, int *ypos, int nr, int fontid, int fontsize, int xstart, int ystart, RGBA * color), void *self)
+{
+ return swf_FontExtract_DefineTextCallback(-1, 0, tag, FEDTJ_CALLBACK, callback, self);
+}
+
+int swf_FontExtract_DefineText(int id, SWFFONT * f, TAG * t, int jobs)
+{
+ return swf_FontExtract_DefineTextCallback(id, f, t, jobs, 0, 0);
+}
+
+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_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;
+
+ /* 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 {
+ 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)
+{
+ if (!font)
+ return;
+ int i, j, k;
+ int *newplace = malloc(sizeof(int) * font->numchars);
+ int *newpos;
+
+ 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 = malloc(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]];
+ }
+
+ free(newpos);
+ free(newplace);
+}
+
+void swf_FontPrepareForEditText(SWFFONT * font)
+{
+ if (!font->layout)
+ swf_FontCreateLayout(font);
+ swf_FontSort(font);
+}
+
+int swf_FontInitUsage(SWFFONT * f, FONTUSAGE * use)
+{
+ if (!use)
+ return -1;
+ use->code = malloc(sizeof(use->code[0]) * f->maxascii);
+ memset(use->code, 0, sizeof(use->code[0]) * f->maxascii);
+ return 0;
+}
+
+void swf_FontClearUsage(SWFFONT * f, FONTUSAGE * use)
+{
+ if (!use)
+ return;
+ free(use->code);
+}
+
+int swf_FontUse(SWFFONT * f, FONTUSAGE * use, U8 * s)
+{
+ if ((!use) || (!s))
+ return -1;
+ while (s[0]) {
+ use->code[s[0]] = 1;
+ s++;
+ }
+ return 0;
+}
+
+int swf_FontSetDefine(TAG * t, SWFFONT * f)
+{
+ U16 *ofs = (U16 *) malloc(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);
+ free(ofs);
+ return 0;
+}
+
+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;
+ size += l + 1;
+ }
+ return size + (font->numchars + 1) * 2;
+}
+
+int swf_FontSetDefine2(TAG * tag, SWFFONT * f)