5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
10 This file is distributed under the GPL, see file COPYING for details
14 #define TF_TEXTCONTROL 0x80
15 #define TF_HASFONT 0x08
16 #define TF_HASCOLOR 0x04
17 #define TF_HASYOFFSET 0x02
18 #define TF_HASXOFFSET 0x01
20 #define FF_WIDECODES 0x01
22 #define FF_ITALIC 0x04
24 #define FF_SHIFTJIS 0x10
25 #define FF_UNICODE 0x20
28 #define FF2_ITALIC 0x02
29 #define FF2_WIDECODES 0x04
30 #define FF2_WIDEOFFSETS 0x08
32 #define FF2_UNICODE 0x20
33 #define FF2_SHIFTJIS 0x40
34 #define FF2_LAYOUT 0x80
36 int swf_FontIsItalic(SWFFONT * f) { return f->version==2?f->flags&FF2_ITALIC:f->flags&FF_ITALIC; }
37 int swf_FontIsBold(SWFFONT * f) { return f->version==2?f->flags&FF2_BOLD:f->flags&FF_BOLD; }
39 static const int WRITEFONTID = 0x4e46; // font id for WriteFont and ReadFont
41 int swf_FontEnumerate(SWF * swf,void (*FontCallback) (U16,U8*))
49 { if (swf_GetTagID(t)==ST_DEFINEFONTINFO)
73 int swf_FontExtract_DefineFont(int id,SWFFONT * f,TAG * t)
90 f->glyph = malloc(sizeof(SWFGLYPH)*n);
91 memset(f->glyph, 0, sizeof(SWFGLYPH)*n);
93 for (i=1;i<n;i++) swf_GetU16(t);
94 for (i=0;i<n;i++) swf_GetSimpleShape(t,&f->glyph[i].shape);
101 int swf_FontExtract_DefineFontInfo(int id,SWFFONT * f,TAG * t)
108 // DefineFont2 doesn't have FontInfo fields
109 fprintf(stderr, "fixme: FontInfo field for DefineFont2 encountered\n");
115 { U8 l = swf_GetU8(t);
119 { if (f->name) free(f->name);
120 f->name = (U8*)malloc(l+1);
122 { swf_GetBlock(t,f->name,l);
126 { swf_RestoreTagPos(t);
130 f->flags = swf_GetU8(t);
132 f->glyph2ascii = (U16*)malloc(sizeof(U16)*f->numchars);
134 for(i=0; i < f->numchars; i++) {
135 f->glyph2ascii[i] = ((f->flags&FF_WIDECODES)?swf_GetU16(t):swf_GetU8(t));
136 if(f->glyph2ascii[i] > maxcode)
137 maxcode = f->glyph2ascii[i];
142 f->maxascii = maxcode;
143 f->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
144 memset(f->ascii2glyph, -1, sizeof(int)*maxcode);
146 for(i = 0; i < f->numchars; i++)
147 f->ascii2glyph[f->glyph2ascii[i]] = i;
150 swf_RestoreTagPos(t);
154 int swf_FontExtract_DefineFont2(int id,SWFFONT * font,TAG * tag)
158 U8 flags1,flags2,namelen;
160 font->id = swf_GetU16(tag);
161 flags1 = swf_GetU8(tag);
162 flags2 = swf_GetU8(tag); //reserved flags
163 namelen = swf_GetU8(tag);
164 font->flags = flags1;
165 font->name = (U8*)malloc(namelen+1);
166 font->name[namelen]=0;
167 swf_GetBlock(tag, font->name, namelen);
169 glyphcount = swf_GetU16(tag);
170 font->numchars = glyphcount;
172 font->glyph = (SWFGLYPH*)malloc(sizeof(SWFGLYPH)*glyphcount);
173 memset(font->glyph, 0, sizeof(SWFGLYPH)*glyphcount);
174 font->glyph2ascii = (U16*)malloc(sizeof(U16)*glyphcount);
175 memset(font->glyph2ascii, 0, sizeof(U16)*glyphcount);
177 if(flags1&8) { // wide offsets
178 for(t=0;t<glyphcount;t++)
179 swf_GetU32(tag); //offset[t]
180 swf_GetU32(tag); // fontcodeoffset
182 for(t=0;t<glyphcount;t++)
183 swf_GetU16(tag); //offset[t]
184 swf_GetU16(tag); // fontcodeoffset
186 for(t=0;t<glyphcount;t++)
187 swf_GetSimpleShape(tag,&(font->glyph[t].shape));
190 for(t=0;t<glyphcount;t++) {
192 if(flags1&4) // wide codes
193 code = swf_GetU16(tag);
195 code = swf_GetU8(tag);
196 font->glyph2ascii[t] = code;
203 font->maxascii = maxcode;
204 font->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
205 memset(font->ascii2glyph, -1, sizeof(int)*maxcode);
206 for(t=0;t<glyphcount;t++)
208 font->ascii2glyph[font->glyph2ascii[t]] = t;
211 if(flags1&128) { // has layout
213 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
214 font->layout->ascent=swf_GetU16(tag);
215 font->layout->descent=swf_GetU16(tag);
216 font->layout->leading=swf_GetU16(tag);
217 for(t=0;t<glyphcount;t++) {
218 S16 advance = swf_GetS16(tag);
219 font->glyph[t].advance = advance;
221 font->layout->bounds = malloc(glyphcount*sizeof(SRECT));
222 for(t=0;t<glyphcount;t++) {
223 swf_ResetReadBits(tag);
224 swf_GetRect(tag, font->layout->bounds);
226 kerningcount = swf_GetU16(tag);
227 font->layout->kerningcount = kerningcount;
228 font->layout->kerning = (SWFKERNING*)malloc(sizeof(SWFKERNING)*kerningcount);
230 font->layout->kerning =
231 malloc(sizeof(*font->layout->kerning)* kerningcount);
232 for(t=0;t<kerningcount;t++)
234 if(flags1&4) { // wide codes
235 font->layout->kerning[t].char1 = swf_GetU16(tag);
236 font->layout->kerning[t].char2 = swf_GetU16(tag);
238 font->layout->kerning[t].char1 = swf_GetU16(tag);
239 font->layout->kerning[t].char2 = swf_GetU16(tag);
241 font->layout->kerning[t].adjustment = swf_GetS16(tag);
249 #define FEDTJ_PRINT 0x01
250 #define FEDTJ_MODIFY 0x02
252 int swf_FontExtract_DefineText(int id,SWFFONT * f,TAG * t,int jobs)
256 U8 gbits, abits, flags;
267 gbits = swf_GetU8(t);
268 abits = swf_GetU8(t);
270 flags = swf_GetU8(t);
273 { if (flags&TF_TEXTCONTROL)
274 { if (flags&TF_HASFONT) fid = swf_GetU16(t);
275 if (flags&TF_HASCOLOR)
276 { swf_GetU8(t); // rgb
279 if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_GetU8(t);
281 if (flags&TF_HASXOFFSET) swf_GetS16(t);
282 if (flags&TF_HASYOFFSET) swf_GetS16(t);
283 if (flags&TF_HASFONT) swf_GetU16(t);
287 for (i=0;i<flags;i++)
290 glyph = swf_GetBits(t,gbits);
291 adv = swf_GetBits(t,abits);
292 if (id==fid) // mitlesen ?
293 { int code = f->glyph2ascii[glyph];
294 if (jobs&FEDTJ_PRINT) printf("%c",code);
295 if (jobs&FEDTJ_MODIFY)
296 /*if (!f->glyph[code].advance)*/ f->glyph[code].advance = adv;
299 if ((id==fid)&&(jobs&FEDTJ_PRINT)) printf("\n");
301 flags = swf_GetU8(t);
304 swf_RestoreTagPos(t);
308 int swf_FontExtract(SWF * swf,int id,SWFFONT * * font)
312 if ((!swf)||(!font)) return -1;
314 f = (SWFFONT *)malloc(sizeof(SWFFONT)); font[0] = f;
317 memset(f,0x00,sizeof(SWFFONT));
323 switch (swf_GetTagID(t))
324 { case ST_DEFINEFONT:
325 nid = swf_FontExtract_DefineFont(id,f,t);
329 nid = swf_FontExtract_DefineFont2(id,f,t);
332 case ST_DEFINEFONTINFO:
333 nid = swf_FontExtract_DefineFontInfo(id,f,t);
338 nid = swf_FontExtract_DefineText(id,f,t,f->layout?0:FEDTJ_MODIFY);
347 int swf_FontSetID(SWFFONT * f,U16 id) { if (!f) return -1; f->id = id; return 0; }
349 int swf_FontReduce(SWFFONT * f,FONTUSAGE * use)
351 if ((!f)||(!use)) return -1;
354 for (i=0;i<f->numchars;i++)
355 if (f->glyph[i].shape)
356 { if (f->glyph2ascii[i]<MAX_CHAR_PER_FONT &&
357 use->code[f->glyph2ascii[i]])
358 { f->ascii2glyph[f->glyph2ascii[i]] = j;
359 f->glyph2ascii[j] = f->glyph2ascii[i];
360 f->glyph[j] = f->glyph[i];
364 { swf_ShapeFree(f->glyph[i].shape);
365 f->ascii2glyph[f->glyph2ascii[i]] = -1;
366 f->glyph2ascii[i] = 0;
367 f->glyph[i].shape = 0;
368 f->glyph[i].advance = 0;
370 } else f->ascii2glyph[f->glyph2ascii[i]] = -1;
377 int swf_FontInitUsage(FONTUSAGE * use)
378 { if (!use) return -1;
379 memset(use->code,0,sizeof(use->code[0])*MAX_CHAR_PER_FONT);
383 int swf_FontUse(FONTUSAGE * use,U8 * s)
384 { if ((!use)||(!s)) return -1;
386 { use->code[s[0]] = 1;
392 int swf_FontSetDefine(TAG * t,SWFFONT * f)
393 { U16*ofs = (U16*)malloc(f->numchars*2);
396 if ((!t)||(!f)) return -1;
397 swf_ResetWriteBits(t);
401 for (i=0;i<f->numchars;i++)
402 if (f->glyph[i].shape)
404 p+=swf_SetSimpleShape(NULL,f->glyph[i].shape);
407 for (i=0;i<j;i++) swf_SetU16(t,ofs[i]+j*2);
409 for (i=0;i<f->numchars;i++)
410 if (f->glyph[i].shape)
411 swf_SetSimpleShape(t,f->glyph[i].shape);
413 swf_ResetWriteBits(t);
418 int swf_FontSetInfo(TAG * t,SWFFONT * f)
421 if ((!t)||(!f)) return -1;
422 swf_ResetWriteBits(t);
424 l = strlen(f->name); if (l>255) l = 255;
426 swf_SetBlock(t,f->name,l);
429 swf_SetU8(t,(f->flags&0xfe)|wide);
431 for (i=0;i<f->numchars;i++) {
432 if (f->glyph[i].shape)
433 wide?swf_SetU16(t,f->glyph2ascii[i]):
434 swf_SetU8(t,f->glyph2ascii[i]);
440 int swf_FontExport(int handle,SWFFONT * f)
447 if (write(handle,f,sizeof(SWFFONT))!=sizeof(SWFFONT)) return -1;
450 { U16 ln = strlen(f->name);
453 { if (write(handle,&ln,2)!=2) return -1;
454 if (write(handle,f->name,ln)!=ln) return -1;
459 { l+=sizeof(SWFLAYOUT);
461 if (write(handle,f->layout,sizeof(SWFLAYOUT))!=sizeof(SWFLAYOUT)) return -1;
462 /* new kerning struct. hope commenting this out doesn't break things
463 if (f->layout->kerning.data)
464 { l+=f->layout->kerning.count*4;
466 if (write(handle,f->layout->kerning.data,f->layout->kerning.count*4)!=f->layout->kerning.count*4) return -1;
470 for (i=0;i<f->numchars;i++)
471 { if (f->glyph[i].shape)
472 { int ll = swf_ShapeExport(handle,f->glyph[i].shape);
481 int swf_FontImport(int handle,SWFFONT * * font)
486 if ((!font)||(handle<0)) return -1;
488 f = (SWFFONT *)malloc(sizeof(SWFFONT)); font[0] = f;
491 memset(f,0x00,sizeof(SWFFONT));
493 if (read(handle,f,sizeof(SWFFONT))!=sizeof(SWFFONT)) goto fehler;
495 layout = (f->layout)?1:0; // avoid illegal free()
501 if (read(handle,&ln,2)!=2) goto fehler;
502 f->name = (U8*)malloc(ln+1);
503 if (!f->name) goto fehler;
504 if (read(handle,f->name,ln)!=ln) goto fehler;
509 { f->layout = (SWFLAYOUT *)malloc(sizeof(SWFLAYOUT));
510 if (!f->layout) goto fehler;
511 if (read(handle,f->layout,sizeof(SWFLAYOUT))!=sizeof(SWFLAYOUT)) goto fehler;
512 /* new kerning struct. hope commenting this out doesn't break things
513 if (f->layout->kerning.data)
514 { int l = f->layout->kerning.count*4;
515 f->layout->kerning.data = (U8*)malloc(l);
516 if (!f->layout->kerning.data) goto fehler;
517 if (read(handle,f->layout->kerning.data,l)!=l) goto fehler;
521 for (i=0;i<f->numchars;i++)
522 { if (f->glyph[i].shape)
523 { if (swf_ShapeImport(handle,&f->glyph[i].shape)<0) goto fehler;
532 if (f) for (;i<MAX_CHAR_PER_FONT;i++) f->glyph[i].shape = NULL;
538 int swf_TextPrintDefineText(TAG * t,SWFFONT * f)
539 { int id = swf_GetTagID(t);
540 if ((id==ST_DEFINETEXT)||(id==ST_DEFINETEXT2)) swf_FontExtract_DefineText(f->id,f,t,FEDTJ_PRINT);
545 void swf_LayoutFree(SWFLAYOUT * l)
547 { if (l->kerning) free(l->kerning);
549 if (l->bounds) free(l->bounds);
555 void swf_FontFree(SWFFONT * f)
559 if (f->name) free(f->name);
560 if (f->layout) swf_LayoutFree(f->layout);
566 for (i=0;i<f->numchars;i++)
567 if (f->glyph[i].shape)
568 { swf_ShapeFree(f->glyph[i].shape);
569 f->glyph[i].shape = NULL;
575 free(f->ascii2glyph);
576 f->ascii2glyph = NULL;
579 free(f->glyph2ascii);
580 f->glyph2ascii = NULL;
586 int swf_TextSetInfoRecord(TAG * t,SWFFONT * font,U16 size,RGBA * color,S16 dx,S16 dy)
590 flags = TF_TEXTCONTROL|(font?TF_HASFONT:0)|(color?TF_HASCOLOR:0)|(dx?TF_HASXOFFSET:0)|(dy?TF_HASYOFFSET:0);
593 if (font) swf_SetU16(t,font->id);
595 { if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_SetRGBA(t,color);
596 else swf_SetRGB(t,color);
598 if (dx) swf_SetS16(t,dx);
599 if (dy) swf_SetS16(t,dy);
600 if (font) swf_SetU16(t,size);
605 int swf_TextCountBits(SWFFONT * font,U8 * s,int scale,U8 * gbits,U8 * abits)
607 if ((!s)||(!font)||((!gbits)&&(!abits))||(!font->ascii2glyph)) return -1;
612 int glyph = font->ascii2glyph[s[0]];
614 g = swf_CountBits(glyph,g);
615 a = swf_CountBits((((U32)font->glyph[glyph].advance)*scale)/100,a);
620 if (gbits) gbits[0] = (U8)g;
621 if (abits) abits[0] = (U8)a;
626 int swf_TextSetCharRecord(TAG * t,SWFFONT * font,U8 * s,int scale,U8 gbits,U8 abits)
629 if ((!t)||(!font)||(!s)||(!font->ascii2glyph)) return -1;
632 if (l>0x7f) l = 0x7f;
637 int g = font->ascii2glyph[s[i]];
639 swf_SetBits(t,g,gbits);
640 swf_SetBits(t,(((U32)font->glyph[g].advance)*scale)/100,abits);
644 swf_ResetWriteBits(t);
648 U32 swf_TextGetWidth(SWFFONT * font,U8 * s,int scale)
654 int g = font->ascii2glyph[*s];
656 res += font->glyph[g].advance;
659 if (scale) res = (res*scale)/100;
664 void swf_WriteFont(SWFFONT*font, char* filename, int useDefineFont2)
672 fprintf(stderr, "DefineFont2 is not yet supported!\n");
676 font->id = WRITEFONTID; //"FN"
678 memset(&swf,0x00,sizeof(SWF));
681 swf.frameRate = 0x4000;
682 swf.movieSize.xmax = 20*640;
683 swf.movieSize.ymax = 20*480;
686 /* if we use DefineFont1 to store the characters,
687 we have to build a textfield to store the
688 advance values. While at it, we can also
689 make the whole .swf viewable */
691 t = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
697 t = swf_InsertTag(t,ST_DEFINEFONT);
701 t = swf_InsertTag(NULL,ST_DEFINEFONT);
705 swf_FontSetDefine(t,font);
707 t = swf_InsertTag(t,ST_DEFINEFONTINFO);
708 swf_FontSetInfo(t,font);
715 int ymax = textscale * 20;
722 int g = font->ascii2glyph[s];
725 if(font->glyph[g].advance*textscale/100 > xmax)
726 xmax = font->glyph[g].advance*textscale/100;
729 swf.movieSize.xmax = xmax*20;
730 swf.movieSize.ymax = ymax;
732 t = swf_InsertTag(t,ST_DEFINETEXT);
734 swf_SetU16(t,font->id+1); // ID
738 r.xmax = swf.movieSize.xmax*20;
739 r.ymax = swf.movieSize.ymax;
743 swf_SetMatrix(t,NULL);
745 abits = swf_CountBits(xmax*16, 0);
756 int c=0,lastx=-1, firstx=0;
758 int g = font->ascii2glyph[y*16+x];
759 if(g>=0 && font->glyph[g].shape) {
766 swf_TextSetInfoRecord(t,font,textscale,&rgb,lastx+1,textscale*y);
769 int g = font->ascii2glyph[y*16+x];
770 if(g>=0 && font->glyph[g].shape) {
771 if(lastx != x*xmax) {
772 swf_TextSetInfoRecord(t,0,0,0,x*xmax+1,0);
775 swf_SetBits(t, g, gbits);
776 swf_SetBits(t, font->glyph[g].advance, abits);
777 lastx = x*xmax+font->glyph[g].advance;
778 swf_ResetWriteBits(t);
785 t = swf_InsertTag(t,ST_PLACEOBJECT2);
787 swf_ObjectPlace(t,font->id+1,1,NULL,NULL,NULL);
789 t = swf_InsertTag(t,ST_SHOWFRAME);
792 t = swf_InsertTag(t,ST_END);
794 f = open(filename, O_RDWR|O_CREAT|O_TRUNC,0644);
795 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed in writeFont().\n");
801 SWFFONT* swf_ReadFont(char* filename)
807 f = open(filename,O_RDONLY);
809 if (f<0 || swf_ReadSWF(f,&swf)<0)
810 { fprintf(stderr,"%s is not a valid SWF font file or contains errors.\n",filename);
817 if(swf_FontExtract(&swf, WRITEFONTID, &font) < 0)