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 ||
50 swf_GetTagID(t)==ST_DEFINEFONT2)
60 if(swf_GetTagID(t) == ST_DEFINEFONT2)
76 int swf_FontExtract_DefineFont(int id,SWFFONT * f,TAG * t)
93 f->glyph = malloc(sizeof(SWFGLYPH)*n);
94 memset(f->glyph, 0, sizeof(SWFGLYPH)*n);
96 for (i=1;i<n;i++) swf_GetU16(t);
97 for (i=0;i<n;i++) swf_GetSimpleShape(t,&f->glyph[i].shape);
100 swf_RestoreTagPos(t);
104 int swf_FontExtract_DefineFontInfo(int id,SWFFONT * f,TAG * t)
113 { U8 l = swf_GetU8(t);
117 // DefineFont2 doesn't have FontInfo fields
118 fprintf(stderr, "fixme: FontInfo field for DefineFont2 encountered\n");
123 { if (f->name) free(f->name);
124 f->name = (U8*)malloc(l+1);
126 { swf_GetBlock(t,f->name,l);
130 { swf_RestoreTagPos(t);
134 f->flags = swf_GetU8(t);
136 f->glyph2ascii = (U16*)malloc(sizeof(U16)*f->numchars);
138 for(i=0; i < f->numchars; i++) {
139 f->glyph2ascii[i] = ((f->flags&FF_WIDECODES)?swf_GetU16(t):swf_GetU8(t));
140 if(f->glyph2ascii[i] > maxcode)
141 maxcode = f->glyph2ascii[i];
146 f->maxascii = maxcode;
147 f->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
148 memset(f->ascii2glyph, -1, sizeof(int)*maxcode);
150 for(i = 0; i < f->numchars; i++)
151 f->ascii2glyph[f->glyph2ascii[i]] = i;
154 swf_RestoreTagPos(t);
158 int swf_FontExtract_DefineFont2(int id,SWFFONT * font,TAG * tag)
163 U8 flags1,flags2,namelen;
165 swf_SetTagPos(tag,0);
167 fid = swf_GetU16(tag);
171 flags1 = swf_GetU8(tag);
172 flags2 = swf_GetU8(tag); //reserved flags
173 namelen = swf_GetU8(tag);
174 font->flags = flags1;
175 font->name = (U8*)malloc(namelen+1);
176 font->name[namelen]=0;
177 swf_GetBlock(tag, font->name, namelen);
179 glyphcount = swf_GetU16(tag);
180 font->numchars = glyphcount;
182 font->glyph = (SWFGLYPH*)malloc(sizeof(SWFGLYPH)*glyphcount);
183 memset(font->glyph, 0, sizeof(SWFGLYPH)*glyphcount);
184 font->glyph2ascii = (U16*)malloc(sizeof(U16)*glyphcount);
185 memset(font->glyph2ascii, 0, sizeof(U16)*glyphcount);
187 if(flags1&8) { // wide offsets
188 for(t=0;t<glyphcount;t++)
189 swf_GetU32(tag); //offset[t]
190 swf_GetU32(tag); // fontcodeoffset
192 for(t=0;t<glyphcount;t++)
193 swf_GetU16(tag); //offset[t]
194 swf_GetU16(tag); // fontcodeoffset
196 for(t=0;t<glyphcount;t++)
197 swf_GetSimpleShape(tag,&(font->glyph[t].shape));
200 for(t=0;t<glyphcount;t++) {
202 if(flags1&4) // wide codes
203 code = swf_GetU16(tag);
205 code = swf_GetU8(tag);
206 font->glyph2ascii[t] = code;
213 font->maxascii = maxcode;
214 font->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
215 memset(font->ascii2glyph, -1, sizeof(int)*maxcode);
216 for(t=0;t<glyphcount;t++)
218 font->ascii2glyph[font->glyph2ascii[t]] = t;
221 if(flags1&128) { // has layout
223 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
224 font->layout->ascent=swf_GetU16(tag);
225 font->layout->descent=swf_GetU16(tag);
226 font->layout->leading=swf_GetU16(tag);
227 for(t=0;t<glyphcount;t++) {
228 S16 advance = swf_GetS16(tag);
229 font->glyph[t].advance = advance;
231 font->layout->bounds = malloc(glyphcount*sizeof(SRECT));
232 for(t=0;t<glyphcount;t++) {
233 swf_ResetReadBits(tag);
234 swf_GetRect(tag, font->layout->bounds);
236 kerningcount = swf_GetU16(tag);
237 font->layout->kerningcount = kerningcount;
238 font->layout->kerning = (SWFKERNING*)malloc(sizeof(SWFKERNING)*kerningcount);
240 font->layout->kerning =
241 malloc(sizeof(*font->layout->kerning)* kerningcount);
242 for(t=0;t<kerningcount;t++)
244 if(flags1&4) { // wide codes
245 font->layout->kerning[t].char1 = swf_GetU16(tag);
246 font->layout->kerning[t].char2 = swf_GetU16(tag);
248 font->layout->kerning[t].char1 = swf_GetU8(tag);
249 font->layout->kerning[t].char2 = swf_GetU8(tag);
251 font->layout->kerning[t].adjustment = swf_GetS16(tag);
255 swf_RestoreTagPos(t);
260 #define FEDTJ_PRINT 0x01
261 #define FEDTJ_MODIFY 0x02
262 #define FEDTJ_CALLBACK 0x04
264 int swf_FontExtract_DefineTextCallback(int id,SWFFONT * f,TAG * t,int jobs,
265 void(*callback)(int*chars, int nr, int fontid))
269 U8 gbits, abits, flags;
280 gbits = swf_GetU8(t);
281 abits = swf_GetU8(t);
283 flags = swf_GetU8(t);
286 { if (flags&TF_TEXTCONTROL)
287 { if (flags&TF_HASFONT) fid = swf_GetU16(t);
288 if (flags&TF_HASCOLOR)
289 { swf_GetU8(t); // rgb
292 if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_GetU8(t);
294 if (flags&TF_HASXOFFSET) swf_GetS16(t);
295 if (flags&TF_HASYOFFSET) swf_GetS16(t);
296 if (flags&TF_HASFONT) swf_GetU16(t);
301 for (i=0;i<flags;i++)
304 glyph = swf_GetBits(t,gbits);
305 adv = swf_GetBits(t,abits);
306 if (id==fid) // mitlesen ?
307 if (jobs&FEDTJ_PRINT) {
308 { int code = f->glyph2ascii[glyph];
311 if (jobs&FEDTJ_MODIFY)
312 /*if (!f->glyph[code].advance)*/ f->glyph[glyph].advance = adv;
316 if ((id==fid)&&(jobs&FEDTJ_PRINT)) printf("\n");
317 if (jobs&FEDTJ_CALLBACK)
318 callback(buf, flags, fid);
320 flags = swf_GetU8(t);
323 swf_RestoreTagPos(t);
327 int swf_FontExtract_DefineText(int id,SWFFONT * f,TAG * t,int jobs)
329 return swf_FontExtract_DefineTextCallback(id,f,t,jobs,0);
332 int swf_FontExtract(SWF * swf,int id,SWFFONT * * font)
336 if ((!swf)||(!font)) return -1;
338 f = (SWFFONT *)malloc(sizeof(SWFFONT)); font[0] = f;
341 memset(f,0x00,sizeof(SWFFONT));
347 switch (swf_GetTagID(t))
348 { case ST_DEFINEFONT:
349 nid = swf_FontExtract_DefineFont(id,f,t);
353 nid = swf_FontExtract_DefineFont2(id,f,t);
356 case ST_DEFINEFONTINFO:
357 nid = swf_FontExtract_DefineFontInfo(id,f,t);
362 nid = swf_FontExtract_DefineText(id,f,t,f->layout?0:FEDTJ_MODIFY);
371 int swf_FontSetID(SWFFONT * f,U16 id) { if (!f) return -1; f->id = id; return 0; }
373 int swf_FontReduce(SWFFONT * f,FONTUSAGE * use)
375 if ((!f)||(!use)) return -1;
378 for (i=0;i<f->numchars;i++)
379 if (f->glyph[i].shape)
380 { if (f->glyph2ascii[i]<MAX_CHAR_PER_FONT &&
381 use->code[f->glyph2ascii[i]])
382 { f->ascii2glyph[f->glyph2ascii[i]] = j;
383 f->glyph2ascii[j] = f->glyph2ascii[i];
384 f->glyph[j] = f->glyph[i];
388 { swf_ShapeFree(f->glyph[i].shape);
389 f->ascii2glyph[f->glyph2ascii[i]] = -1;
390 f->glyph2ascii[i] = 0;
391 f->glyph[i].shape = NULL;
392 f->glyph[i].advance = 0;
394 } else f->ascii2glyph[f->glyph2ascii[i]] = -1;
401 int swf_FontInitUsage(FONTUSAGE * use)
402 { if (!use) return -1;
403 memset(use->code,0,sizeof(use->code[0])*MAX_CHAR_PER_FONT);
407 int swf_FontUse(FONTUSAGE * use,U8 * s)
408 { if ((!use)||(!s)) return -1;
410 { use->code[s[0]] = 1;
416 int swf_FontSetDefine(TAG * t,SWFFONT * f)
417 { U16*ofs = (U16*)malloc(f->numchars*2);
420 if ((!t)||(!f)) return -1;
421 swf_ResetWriteBits(t);
425 for (i=0;i<f->numchars;i++)
426 if (f->glyph[i].shape)
428 p+=swf_SetSimpleShape(NULL,f->glyph[i].shape);
431 for (i=0;i<j;i++) swf_SetU16(t,ofs[i]+j*2);
433 for (i=0;i<f->numchars;i++)
434 if (f->glyph[i].shape)
435 swf_SetSimpleShape(t,f->glyph[i].shape);
437 swf_ResetWriteBits(t);
442 int swf_FontSetInfo(TAG * t,SWFFONT * f)
445 if ((!t)||(!f)) return -1;
446 swf_ResetWriteBits(t);
448 l = strlen(f->name); if (l>255) l = 255;
450 swf_SetBlock(t,f->name,l);
453 swf_SetU8(t,(f->flags&0xfe)|wide);
455 for (i=0;i<f->numchars;i++) {
456 if (f->glyph[i].shape)
457 wide?swf_SetU16(t,f->glyph2ascii[i]):
458 swf_SetU8(t,f->glyph2ascii[i]);
464 int swf_FontExport(int handle,SWFFONT * f)
471 if (write(handle,f,sizeof(SWFFONT))!=sizeof(SWFFONT)) return -1;
474 { U16 ln = strlen(f->name);
477 { if (write(handle,&ln,2)!=2) return -1;
478 if (write(handle,f->name,ln)!=ln) return -1;
483 { l+=sizeof(SWFLAYOUT);
485 if (write(handle,f->layout,sizeof(SWFLAYOUT))!=sizeof(SWFLAYOUT)) return -1;
486 /* new kerning struct. hope commenting this out doesn't break things
487 if (f->layout->kerning.data)
488 { l+=f->layout->kerning.count*4;
490 if (write(handle,f->layout->kerning.data,f->layout->kerning.count*4)!=f->layout->kerning.count*4) return -1;
494 for (i=0;i<f->numchars;i++)
495 { if (f->glyph[i].shape)
496 { int ll = swf_ShapeExport(handle,f->glyph[i].shape);
505 int swf_FontImport(int handle,SWFFONT * * font)
510 if ((!font)||(handle<0)) return -1;
512 f = (SWFFONT *)malloc(sizeof(SWFFONT)); font[0] = f;
515 memset(f,0x00,sizeof(SWFFONT));
517 if (read(handle,f,sizeof(SWFFONT))!=sizeof(SWFFONT)) goto fehler;
519 layout = (f->layout)?1:0; // avoid illegal free()
525 if (read(handle,&ln,2)!=2) goto fehler;
526 f->name = (U8*)malloc(ln+1);
527 if (!f->name) goto fehler;
528 if (read(handle,f->name,ln)!=ln) goto fehler;
533 { f->layout = (SWFLAYOUT *)malloc(sizeof(SWFLAYOUT));
534 if (!f->layout) goto fehler;
535 if (read(handle,f->layout,sizeof(SWFLAYOUT))!=sizeof(SWFLAYOUT)) goto fehler;
536 /* new kerning struct. hope commenting this out doesn't break things
537 if (f->layout->kerning.data)
538 { int l = f->layout->kerning.count*4;
539 f->layout->kerning.data = (U8*)malloc(l);
540 if (!f->layout->kerning.data) goto fehler;
541 if (read(handle,f->layout->kerning.data,l)!=l) goto fehler;
545 for (i=0;i<f->numchars;i++)
546 { if (f->glyph[i].shape)
547 { if (swf_ShapeImport(handle,&f->glyph[i].shape)<0) goto fehler;
556 if (f) for (;i<MAX_CHAR_PER_FONT;i++) f->glyph[i].shape = NULL;
562 int swf_TextPrintDefineText(TAG * t,SWFFONT * f)
563 { int id = swf_GetTagID(t);
564 if ((id==ST_DEFINETEXT)||(id==ST_DEFINETEXT2)) swf_FontExtract_DefineText(f->id,f,t,FEDTJ_PRINT);
569 void swf_LayoutFree(SWFLAYOUT * l)
571 { if (l->kerning) free(l->kerning);
573 if (l->bounds) free(l->bounds);
579 void swf_FontFree(SWFFONT * f)
583 if (f->name) free(f->name);
584 if (f->layout) swf_LayoutFree(f->layout);
590 for (i=0;i<f->numchars;i++)
591 if (f->glyph[i].shape)
592 { swf_ShapeFree(f->glyph[i].shape);
593 f->glyph[i].shape = NULL;
599 free(f->ascii2glyph);
600 f->ascii2glyph = NULL;
603 free(f->glyph2ascii);
604 f->glyph2ascii = NULL;
610 int swf_TextSetInfoRecord(TAG * t,SWFFONT * font,U16 size,RGBA * color,S16 dx,S16 dy)
614 flags = TF_TEXTCONTROL|(font?TF_HASFONT:0)|(color?TF_HASCOLOR:0)|(dx?TF_HASXOFFSET:0)|(dy?TF_HASYOFFSET:0);
617 if (font) swf_SetU16(t,font->id);
619 { if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_SetRGBA(t,color);
620 else swf_SetRGB(t,color);
622 if (dx) swf_SetS16(t,dx);
623 if (dy) swf_SetS16(t,dy);
624 if (font) swf_SetU16(t,size);
629 int swf_TextCountBits(SWFFONT * font,U8 * s,int scale,U8 * gbits,U8 * abits)
631 if ((!s)||(!font)||((!gbits)&&(!abits))||(!font->ascii2glyph)) return -1;
636 int glyph = font->ascii2glyph[s[0]];
638 g = swf_CountBits(glyph,g);
639 a = swf_CountBits((((U32)font->glyph[glyph].advance)*scale)/100,a);
644 if (gbits) gbits[0] = (U8)g;
645 if (abits) abits[0] = (U8)a;
650 int swf_TextSetCharRecord(TAG * t,SWFFONT * font,U8 * s,int scale,U8 gbits,U8 abits)
653 if ((!t)||(!font)||(!s)||(!font->ascii2glyph)) return -1;
656 if (l>0x7f) l = 0x7f;
661 int g = font->ascii2glyph[s[i]];
663 swf_SetBits(t,g,gbits);
664 swf_SetBits(t,(((U32)font->glyph[g].advance)*scale)/100,abits);
668 swf_ResetWriteBits(t);
672 U32 swf_TextGetWidth(SWFFONT * font,U8 * s,int scale)
678 int g = font->ascii2glyph[*s];
680 res += font->glyph[g].advance;
683 if (scale) res = (res*scale)/100;
688 SWFFONT* swf_ReadFont(char* filename)
694 f = open(filename,O_RDONLY);
696 if (f<0 || swf_ReadSWF(f,&swf)<0)
697 { fprintf(stderr,"%s is not a valid SWF font file or contains errors.\n",filename);
704 if(swf_FontExtract(&swf, WRITEFONTID, &font) < 0)
711 void swf_WriteFont(SWFFONT*font, char* filename)
717 int useDefineFont2 = 1;
720 //fprintf(stderr, "DefineFont2 is not yet supported!\n");
724 font->id = WRITEFONTID; //"FN"
726 memset(&swf,0x00,sizeof(SWF));
729 swf.frameRate = 0x4000;
732 /* if we use DefineFont1 to store the characters,
733 we have to build a textfield to store the
734 advance values. While at it, we can also
735 make the whole .swf viewable */
737 t = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
743 t = swf_InsertTag(t,ST_DEFINEFONT);
747 t = swf_InsertTag(NULL,ST_DEFINEFONT);
751 swf_FontSetDefine(t,font);
753 t = swf_InsertTag(t,ST_DEFINEFONTINFO);
754 swf_FontSetInfo(t,font);
757 { int textscale = 400;
760 int ymax = textscale * 2 * (font->maxascii/16+1);
762 char text[MAX_CHAR_PER_FONT+1];
764 text[MAX_CHAR_PER_FONT]=0;
765 for(s=0;s<font->maxascii;s++)
767 int g = font->ascii2glyph[s];
770 if(font->glyph[g].advance*textscale/200 > xmax)
771 xmax = font->glyph[g].advance*textscale/200;
774 swf.movieSize.xmax = xmax*20;
775 swf.movieSize.ymax = ymax;
777 t = swf_InsertTag(t,ST_DEFINETEXT);
779 swf_SetU16(t,font->id+1); // ID
783 r.xmax = swf.movieSize.xmax*20;
784 r.ymax = swf.movieSize.ymax;
788 swf_SetMatrix(t,NULL);
790 abits = swf_CountBits(xmax*16, 0);
799 for(y=0;y<=((font->maxascii-1)/16);y++)
801 int c=0,lastx=-1, firstx=0;
803 int g = (y*16+x<font->maxascii)?font->ascii2glyph[y*16+x]:-1;
804 if(g>=0 && font->glyph[g].shape) {
811 swf_TextSetInfoRecord(t,font,textscale,&rgb,lastx+1,textscale*y*2);
814 int g = (y*16+x<font->maxascii)?font->ascii2glyph[y*16+x]:-1;
815 if(g>=0 && font->glyph[g].shape) {
816 if(lastx != x*xmax) {
817 swf_TextSetInfoRecord(t,0,0,0,x*xmax+1,0);
820 swf_SetBits(t, g, gbits);
821 swf_SetBits(t, font->glyph[g].advance, abits);
822 lastx = x*xmax+font->glyph[g].advance;
823 swf_ResetWriteBits(t);
831 t = swf_InsertTag(t,ST_PLACEOBJECT2);
833 swf_ObjectPlace(t,font->id+1,1,NULL,NULL,NULL);
835 t = swf_InsertTag(t,ST_SHOWFRAME);
838 t = swf_InsertTag(t,ST_END);
840 f = open(filename, O_RDWR|O_CREAT|O_TRUNC,0644);
841 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed in writeFont().\n");
848 void swf_SetEditText(TAG*tag, U16 flags, SRECT r, char*text, RGBA*color,
849 int maxlength, U16 font, U16 height, EditTextLayout*layout, char*variable)
852 swf_ResetWriteBits(tag);
854 flags &= ~(ET_HASTEXT|ET_HASTEXTCOLOR|ET_HASMAXLENGTH|ET_HASFONT|ET_HASLAYOUT);
855 if(text) flags |= ET_HASTEXT;
856 if(color) flags |= ET_HASTEXTCOLOR;
857 if(maxlength) flags |= ET_HASMAXLENGTH;
858 if(font) flags |= ET_HASFONT;
859 if(layout) flags |= ET_HASLAYOUT;
861 swf_SetBits(tag, flags, 16);
863 if(flags & ET_HASFONT) {
864 swf_SetU16(tag, font); //font
865 swf_SetU16(tag, height); //fontheight
867 if(flags & ET_HASTEXTCOLOR) {
868 swf_SetRGBA(tag, color);
870 if(flags & ET_HASMAXLENGTH) {
871 swf_SetU16(tag, maxlength); //maxlength
873 if(flags & ET_HASLAYOUT) {
874 swf_SetU8(tag,layout->align); //align
875 swf_SetU16(tag,layout->leftmargin); //left margin
876 swf_SetU16(tag,layout->rightmargin); //right margin
877 swf_SetU16(tag,layout->indent); //indent
878 swf_SetU16(tag,layout->leading); //leading
880 swf_SetString(tag, variable);
881 if(flags & ET_HASTEXT)
882 swf_SetString(tag,text);