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)
111 // DefineFont2 doesn't have FontInfo fields
112 fprintf(stderr, "fixme: FontInfo field for DefineFont2 encountered\n");
118 { U8 l = swf_GetU8(t);
122 { if (f->name) free(f->name);
123 f->name = (U8*)malloc(l+1);
125 { swf_GetBlock(t,f->name,l);
129 { swf_RestoreTagPos(t);
133 f->flags = swf_GetU8(t);
135 f->glyph2ascii = (U16*)malloc(sizeof(U16)*f->numchars);
137 for(i=0; i < f->numchars; i++) {
138 f->glyph2ascii[i] = ((f->flags&FF_WIDECODES)?swf_GetU16(t):swf_GetU8(t));
139 if(f->glyph2ascii[i] > maxcode)
140 maxcode = f->glyph2ascii[i];
145 f->maxascii = maxcode;
146 f->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
147 memset(f->ascii2glyph, -1, sizeof(int)*maxcode);
149 for(i = 0; i < f->numchars; i++)
150 f->ascii2glyph[f->glyph2ascii[i]] = i;
153 swf_RestoreTagPos(t);
157 int swf_FontExtract_DefineFont2(int id,SWFFONT * font,TAG * tag)
161 U8 flags1,flags2,namelen;
163 font->id = swf_GetU16(tag);
164 flags1 = swf_GetU8(tag);
165 flags2 = swf_GetU8(tag); //reserved flags
166 namelen = swf_GetU8(tag);
167 font->flags = flags1;
168 font->name = (U8*)malloc(namelen+1);
169 font->name[namelen]=0;
170 swf_GetBlock(tag, font->name, namelen);
172 glyphcount = swf_GetU16(tag);
173 font->numchars = glyphcount;
175 font->glyph = (SWFGLYPH*)malloc(sizeof(SWFGLYPH)*glyphcount);
176 memset(font->glyph, 0, sizeof(SWFGLYPH)*glyphcount);
177 font->glyph2ascii = (U16*)malloc(sizeof(U16)*glyphcount);
178 memset(font->glyph2ascii, 0, sizeof(U16)*glyphcount);
180 if(flags1&8) { // wide offsets
181 for(t=0;t<glyphcount;t++)
182 swf_GetU32(tag); //offset[t]
183 swf_GetU32(tag); // fontcodeoffset
185 for(t=0;t<glyphcount;t++)
186 swf_GetU16(tag); //offset[t]
187 swf_GetU16(tag); // fontcodeoffset
189 for(t=0;t<glyphcount;t++)
190 swf_GetSimpleShape(tag,&(font->glyph[t].shape));
193 for(t=0;t<glyphcount;t++) {
195 if(flags1&4) // wide codes
196 code = swf_GetU16(tag);
198 code = swf_GetU8(tag);
199 font->glyph2ascii[t] = code;
206 font->maxascii = maxcode;
207 font->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
208 memset(font->ascii2glyph, -1, sizeof(int)*maxcode);
209 for(t=0;t<glyphcount;t++)
211 font->ascii2glyph[font->glyph2ascii[t]] = t;
214 if(flags1&128) { // has layout
216 font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
217 font->layout->ascent=swf_GetU16(tag);
218 font->layout->descent=swf_GetU16(tag);
219 font->layout->leading=swf_GetU16(tag);
220 for(t=0;t<glyphcount;t++) {
221 S16 advance = swf_GetS16(tag);
222 font->glyph[t].advance = advance;
224 font->layout->bounds = malloc(glyphcount*sizeof(SRECT));
225 for(t=0;t<glyphcount;t++) {
226 swf_ResetReadBits(tag);
227 swf_GetRect(tag, font->layout->bounds);
229 kerningcount = swf_GetU16(tag);
230 font->layout->kerningcount = kerningcount;
231 font->layout->kerning = (SWFKERNING*)malloc(sizeof(SWFKERNING)*kerningcount);
233 font->layout->kerning =
234 malloc(sizeof(*font->layout->kerning)* kerningcount);
235 for(t=0;t<kerningcount;t++)
237 if(flags1&4) { // wide codes
238 font->layout->kerning[t].char1 = swf_GetU16(tag);
239 font->layout->kerning[t].char2 = swf_GetU16(tag);
241 font->layout->kerning[t].char1 = swf_GetU16(tag);
242 font->layout->kerning[t].char2 = swf_GetU16(tag);
244 font->layout->kerning[t].adjustment = swf_GetS16(tag);
252 #define FEDTJ_PRINT 0x01
253 #define FEDTJ_MODIFY 0x02
255 int swf_FontExtract_DefineText(int id,SWFFONT * f,TAG * t,int jobs)
259 U8 gbits, abits, flags;
270 gbits = swf_GetU8(t);
271 abits = swf_GetU8(t);
273 flags = swf_GetU8(t);
276 { if (flags&TF_TEXTCONTROL)
277 { if (flags&TF_HASFONT) fid = swf_GetU16(t);
278 if (flags&TF_HASCOLOR)
279 { swf_GetU8(t); // rgb
282 if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_GetU8(t);
284 if (flags&TF_HASXOFFSET) swf_GetS16(t);
285 if (flags&TF_HASYOFFSET) swf_GetS16(t);
286 if (flags&TF_HASFONT) swf_GetU16(t);
290 for (i=0;i<flags;i++)
293 glyph = swf_GetBits(t,gbits);
294 adv = swf_GetBits(t,abits);
295 if (id==fid) // mitlesen ?
296 { int code = f->glyph2ascii[glyph];
297 if (jobs&FEDTJ_PRINT) printf("%c",code);
298 if (jobs&FEDTJ_MODIFY)
299 /*if (!f->glyph[code].advance)*/ f->glyph[glyph].advance = adv;
302 if ((id==fid)&&(jobs&FEDTJ_PRINT)) printf("\n");
304 flags = swf_GetU8(t);
307 swf_RestoreTagPos(t);
311 int swf_FontExtract(SWF * swf,int id,SWFFONT * * font)
315 if ((!swf)||(!font)) return -1;
317 f = (SWFFONT *)malloc(sizeof(SWFFONT)); font[0] = f;
320 memset(f,0x00,sizeof(SWFFONT));
326 switch (swf_GetTagID(t))
327 { case ST_DEFINEFONT:
328 nid = swf_FontExtract_DefineFont(id,f,t);
332 nid = swf_FontExtract_DefineFont2(id,f,t);
335 case ST_DEFINEFONTINFO:
336 nid = swf_FontExtract_DefineFontInfo(id,f,t);
341 nid = swf_FontExtract_DefineText(id,f,t,f->layout?0:FEDTJ_MODIFY);
350 int swf_FontSetID(SWFFONT * f,U16 id) { if (!f) return -1; f->id = id; return 0; }
352 int swf_FontReduce(SWFFONT * f,FONTUSAGE * use)
354 if ((!f)||(!use)) return -1;
357 for (i=0;i<f->numchars;i++)
358 if (f->glyph[i].shape)
359 { if (f->glyph2ascii[i]<MAX_CHAR_PER_FONT &&
360 use->code[f->glyph2ascii[i]])
361 { f->ascii2glyph[f->glyph2ascii[i]] = j;
362 f->glyph2ascii[j] = f->glyph2ascii[i];
363 f->glyph[j] = f->glyph[i];
367 { swf_ShapeFree(f->glyph[i].shape);
368 f->ascii2glyph[f->glyph2ascii[i]] = -1;
369 f->glyph2ascii[i] = 0;
370 f->glyph[i].shape = 0;
371 f->glyph[i].advance = 0;
373 } else f->ascii2glyph[f->glyph2ascii[i]] = -1;
380 int swf_FontInitUsage(FONTUSAGE * use)
381 { if (!use) return -1;
382 memset(use->code,0,sizeof(use->code[0])*MAX_CHAR_PER_FONT);
386 int swf_FontUse(FONTUSAGE * use,U8 * s)
387 { if ((!use)||(!s)) return -1;
389 { use->code[s[0]] = 1;
395 int swf_FontSetDefine(TAG * t,SWFFONT * f)
396 { U16*ofs = (U16*)malloc(f->numchars*2);
399 if ((!t)||(!f)) return -1;
400 swf_ResetWriteBits(t);
404 for (i=0;i<f->numchars;i++)
405 if (f->glyph[i].shape)
407 p+=swf_SetSimpleShape(NULL,f->glyph[i].shape);
410 for (i=0;i<j;i++) swf_SetU16(t,ofs[i]+j*2);
412 for (i=0;i<f->numchars;i++)
413 if (f->glyph[i].shape)
414 swf_SetSimpleShape(t,f->glyph[i].shape);
416 swf_ResetWriteBits(t);
421 int swf_FontSetInfo(TAG * t,SWFFONT * f)
424 if ((!t)||(!f)) return -1;
425 swf_ResetWriteBits(t);
427 l = strlen(f->name); if (l>255) l = 255;
429 swf_SetBlock(t,f->name,l);
432 swf_SetU8(t,(f->flags&0xfe)|wide);
434 for (i=0;i<f->numchars;i++) {
435 if (f->glyph[i].shape)
436 wide?swf_SetU16(t,f->glyph2ascii[i]):
437 swf_SetU8(t,f->glyph2ascii[i]);
443 int swf_FontExport(int handle,SWFFONT * f)
450 if (write(handle,f,sizeof(SWFFONT))!=sizeof(SWFFONT)) return -1;
453 { U16 ln = strlen(f->name);
456 { if (write(handle,&ln,2)!=2) return -1;
457 if (write(handle,f->name,ln)!=ln) return -1;
462 { l+=sizeof(SWFLAYOUT);
464 if (write(handle,f->layout,sizeof(SWFLAYOUT))!=sizeof(SWFLAYOUT)) return -1;
465 /* new kerning struct. hope commenting this out doesn't break things
466 if (f->layout->kerning.data)
467 { l+=f->layout->kerning.count*4;
469 if (write(handle,f->layout->kerning.data,f->layout->kerning.count*4)!=f->layout->kerning.count*4) return -1;
473 for (i=0;i<f->numchars;i++)
474 { if (f->glyph[i].shape)
475 { int ll = swf_ShapeExport(handle,f->glyph[i].shape);
484 int swf_FontImport(int handle,SWFFONT * * font)
489 if ((!font)||(handle<0)) return -1;
491 f = (SWFFONT *)malloc(sizeof(SWFFONT)); font[0] = f;
494 memset(f,0x00,sizeof(SWFFONT));
496 if (read(handle,f,sizeof(SWFFONT))!=sizeof(SWFFONT)) goto fehler;
498 layout = (f->layout)?1:0; // avoid illegal free()
504 if (read(handle,&ln,2)!=2) goto fehler;
505 f->name = (U8*)malloc(ln+1);
506 if (!f->name) goto fehler;
507 if (read(handle,f->name,ln)!=ln) goto fehler;
512 { f->layout = (SWFLAYOUT *)malloc(sizeof(SWFLAYOUT));
513 if (!f->layout) goto fehler;
514 if (read(handle,f->layout,sizeof(SWFLAYOUT))!=sizeof(SWFLAYOUT)) goto fehler;
515 /* new kerning struct. hope commenting this out doesn't break things
516 if (f->layout->kerning.data)
517 { int l = f->layout->kerning.count*4;
518 f->layout->kerning.data = (U8*)malloc(l);
519 if (!f->layout->kerning.data) goto fehler;
520 if (read(handle,f->layout->kerning.data,l)!=l) goto fehler;
524 for (i=0;i<f->numchars;i++)
525 { if (f->glyph[i].shape)
526 { if (swf_ShapeImport(handle,&f->glyph[i].shape)<0) goto fehler;
535 if (f) for (;i<MAX_CHAR_PER_FONT;i++) f->glyph[i].shape = NULL;
541 int swf_TextPrintDefineText(TAG * t,SWFFONT * f)
542 { int id = swf_GetTagID(t);
543 if ((id==ST_DEFINETEXT)||(id==ST_DEFINETEXT2)) swf_FontExtract_DefineText(f->id,f,t,FEDTJ_PRINT);
548 void swf_LayoutFree(SWFLAYOUT * l)
550 { if (l->kerning) free(l->kerning);
552 if (l->bounds) free(l->bounds);
558 void swf_FontFree(SWFFONT * f)
562 if (f->name) free(f->name);
563 if (f->layout) swf_LayoutFree(f->layout);
569 for (i=0;i<f->numchars;i++)
570 if (f->glyph[i].shape)
571 { swf_ShapeFree(f->glyph[i].shape);
572 f->glyph[i].shape = NULL;
578 free(f->ascii2glyph);
579 f->ascii2glyph = NULL;
582 free(f->glyph2ascii);
583 f->glyph2ascii = NULL;
589 int swf_TextSetInfoRecord(TAG * t,SWFFONT * font,U16 size,RGBA * color,S16 dx,S16 dy)
593 flags = TF_TEXTCONTROL|(font?TF_HASFONT:0)|(color?TF_HASCOLOR:0)|(dx?TF_HASXOFFSET:0)|(dy?TF_HASYOFFSET:0);
596 if (font) swf_SetU16(t,font->id);
598 { if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_SetRGBA(t,color);
599 else swf_SetRGB(t,color);
601 if (dx) swf_SetS16(t,dx);
602 if (dy) swf_SetS16(t,dy);
603 if (font) swf_SetU16(t,size);
608 int swf_TextCountBits(SWFFONT * font,U8 * s,int scale,U8 * gbits,U8 * abits)
610 if ((!s)||(!font)||((!gbits)&&(!abits))||(!font->ascii2glyph)) return -1;
615 int glyph = font->ascii2glyph[s[0]];
617 g = swf_CountBits(glyph,g);
618 a = swf_CountBits((((U32)font->glyph[glyph].advance)*scale)/100,a);
623 if (gbits) gbits[0] = (U8)g;
624 if (abits) abits[0] = (U8)a;
629 int swf_TextSetCharRecord(TAG * t,SWFFONT * font,U8 * s,int scale,U8 gbits,U8 abits)
632 if ((!t)||(!font)||(!s)||(!font->ascii2glyph)) return -1;
635 if (l>0x7f) l = 0x7f;
640 int g = font->ascii2glyph[s[i]];
642 swf_SetBits(t,g,gbits);
643 swf_SetBits(t,(((U32)font->glyph[g].advance)*scale)/100,abits);
647 swf_ResetWriteBits(t);
651 U32 swf_TextGetWidth(SWFFONT * font,U8 * s,int scale)
657 int g = font->ascii2glyph[*s];
659 res += font->glyph[g].advance;
662 if (scale) res = (res*scale)/100;
667 void swf_WriteFont(SWFFONT*font, char* filename, int useDefineFont2)
675 fprintf(stderr, "DefineFont2 is not yet supported!\n");
679 font->id = WRITEFONTID; //"FN"
681 memset(&swf,0x00,sizeof(SWF));
684 swf.frameRate = 0x4000;
685 swf.movieSize.xmax = 20*640;
686 swf.movieSize.ymax = 20*480;
689 /* if we use DefineFont1 to store the characters,
690 we have to build a textfield to store the
691 advance values. While at it, we can also
692 make the whole .swf viewable */
694 t = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
700 t = swf_InsertTag(t,ST_DEFINEFONT);
704 t = swf_InsertTag(NULL,ST_DEFINEFONT);
708 swf_FontSetDefine(t,font);
710 t = swf_InsertTag(t,ST_DEFINEFONTINFO);
711 swf_FontSetInfo(t,font);
718 int ymax = textscale * 20;
725 int g = font->ascii2glyph[s];
728 if(font->glyph[g].advance*textscale/100 > xmax)
729 xmax = font->glyph[g].advance*textscale/100;
732 swf.movieSize.xmax = xmax*20;
733 swf.movieSize.ymax = ymax;
735 t = swf_InsertTag(t,ST_DEFINETEXT);
737 swf_SetU16(t,font->id+1); // ID
741 r.xmax = swf.movieSize.xmax*20;
742 r.ymax = swf.movieSize.ymax;
746 swf_SetMatrix(t,NULL);
748 abits = swf_CountBits(xmax*16, 0);
759 int c=0,lastx=-1, firstx=0;
761 int g = font->ascii2glyph[y*16+x];
762 if(g>=0 && font->glyph[g].shape) {
769 swf_TextSetInfoRecord(t,font,textscale,&rgb,lastx+1,textscale*y);
772 int g = font->ascii2glyph[y*16+x];
773 if(g>=0 && font->glyph[g].shape) {
774 if(lastx != x*xmax) {
775 swf_TextSetInfoRecord(t,0,0,0,x*xmax+1,0);
778 swf_SetBits(t, g, gbits);
779 swf_SetBits(t, font->glyph[g].advance, abits);
780 lastx = x*xmax+font->glyph[g].advance;
781 swf_ResetWriteBits(t);
788 t = swf_InsertTag(t,ST_PLACEOBJECT2);
790 swf_ObjectPlace(t,font->id+1,1,NULL,NULL,NULL);
792 t = swf_InsertTag(t,ST_SHOWFRAME);
795 t = swf_InsertTag(t,ST_END);
797 f = open(filename, O_RDWR|O_CREAT|O_TRUNC,0644);
798 if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed in writeFont().\n");
804 SWFFONT* swf_ReadFont(char* filename)
810 f = open(filename,O_RDONLY);
812 if (f<0 || swf_ReadSWF(f,&swf)<0)
813 { fprintf(stderr,"%s is not a valid SWF font file or contains errors.\n",filename);
820 if(swf_FontExtract(&swf, WRITEFONTID, &font) < 0)