minor fixes: new font structure in dumpfont.c, commented obsolete functions, ...
[swftools.git] / lib / modules / swftext.c
1 /* swftext.c
2
3    Text and font routines
4       
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
9  
10    This file is distributed under the GPL, see file COPYING for details 
11
12 */
13
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
19
20 #define FF_WIDECODES    0x01
21 #define FF_BOLD         0x02
22 #define FF_ITALIC       0x04
23 #define FF_ANSI         0x08
24 #define FF_SHIFTJIS     0x10
25 #define FF_UNICODE      0x20
26
27 #define FF2_BOLD         0x01
28 #define FF2_ITALIC       0x02
29 #define FF2_WIDECODES    0x04
30 #define FF2_WIDEOFFSETS  0x08
31 #define FF2_ANSI         0x10
32 #define FF2_UNICODE      0x20
33 #define FF2_SHIFTJIS     0x40
34 #define FF2_LAYOUT       0x80
35
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; }
38
39 static const int WRITEFONTID = 0x4e46; // font id for WriteFont and ReadFont
40
41 int swf_FontEnumerate(SWF * swf,void (*FontCallback) (U16,U8*))
42 { int n;
43   TAG * t;
44   if (!swf) return -1;
45   t = swf->firstTag;
46   n = 0;
47
48   while (t)
49   { if (swf_GetTagID(t)==ST_DEFINEFONTINFO ||
50           swf_GetTagID(t)==ST_DEFINEFONT2)
51     { n++;
52       if (FontCallback)
53       { U16 id;
54         int l;
55         U8 s[257];
56         swf_SaveTagPos(t);
57         swf_SetTagPos(t,0);
58         
59         id  = swf_GetU16(t);
60         if(swf_GetTagID(t) == ST_DEFINEFONT2)
61             swf_GetU16(t);
62         l   = swf_GetU8(t);
63         swf_GetBlock(t,s,l);
64         s[l] = 0;
65
66         (FontCallback)(id,s); 
67       
68         swf_RestoreTagPos(t);
69       }
70     }
71     t = swf_NextTag(t);
72   }
73   return n;
74 }
75
76 int swf_FontExtract_DefineFont(int id,SWFFONT * f,TAG * t)
77 { U16 fid;
78   swf_SaveTagPos(t);
79   swf_SetTagPos(t,0);
80
81   fid = swf_GetU16(t);
82   if ((!id)||(id==fid))
83   { U16 of,*ofs;
84     int n,i;
85       
86     id = fid;
87     f->version = 1;
88     f->id = fid;
89
90     of = swf_GetU16(t);
91     n = of/2;
92     f->numchars = n;
93     f->glyph = malloc(sizeof(SWFGLYPH)*n);
94     memset(f->glyph, 0, sizeof(SWFGLYPH)*n);
95
96     for (i=1;i<n;i++) swf_GetU16(t);
97     for (i=0;i<n;i++) swf_GetSimpleShape(t,&f->glyph[i].shape);
98   }
99
100   swf_RestoreTagPos(t);
101   return id;
102 }
103
104 int swf_FontExtract_DefineFontInfo(int id,SWFFONT * f,TAG * t)
105 { U16 fid;
106   U16 maxcode;
107   swf_SaveTagPos(t);
108   swf_SetTagPos(t,0);
109
110   if(f->version>1) {
111       // DefineFont2 doesn't have FontInfo fields
112       fprintf(stderr, "fixme: FontInfo field for DefineFont2 encountered\n");
113       return -1;
114   }
115
116   fid = swf_GetU16(t);
117   if (fid==id)
118   { U8 l = swf_GetU8(t);
119     int i;
120     
121     if (l)
122     { if (f->name) free(f->name);
123       f->name = (U8*)malloc(l+1);
124       if (f->name)
125       { swf_GetBlock(t,f->name,l);
126         f->name[l] = 0;
127       }
128       else
129       { swf_RestoreTagPos(t);
130         return -1;
131       }
132     }
133     f->flags = swf_GetU8(t);
134
135     f->glyph2ascii = (U16*)malloc(sizeof(U16)*f->numchars);
136     maxcode = 0;
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];
141     }
142     maxcode++;
143     if(maxcode<256)
144         maxcode=256;
145     f->maxascii = maxcode;
146     f->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
147     memset(f->ascii2glyph, -1, sizeof(int)*maxcode);
148      
149     for(i = 0; i < f->numchars; i++)
150       f->ascii2glyph[f->glyph2ascii[i]] = i;
151   }
152
153   swf_RestoreTagPos(t);
154   return id;
155 }
156
157 int swf_FontExtract_DefineFont2(int id,SWFFONT * font,TAG * tag)
158 {
159     int t, glyphcount;
160     int maxcode;
161     int fid;
162     U8 flags1,flags2,namelen;
163     swf_SaveTagPos(tag);
164     swf_SetTagPos(tag,0);
165     font->version=2;
166     fid = swf_GetU16(tag);
167     if(id && id!=fid)
168         return;
169     font->id = fid;
170     flags1 = swf_GetU8(tag);
171     flags2 = swf_GetU8(tag); //reserved flags
172     namelen = swf_GetU8(tag);
173     font->flags = flags1;
174     font->name = (U8*)malloc(namelen+1);
175     font->name[namelen]=0;
176     swf_GetBlock(tag, font->name, namelen);
177     font->version = 2;
178     glyphcount = swf_GetU16(tag);
179     font->numchars = glyphcount;
180     
181     font->glyph = (SWFGLYPH*)malloc(sizeof(SWFGLYPH)*glyphcount);
182     memset(font->glyph, 0, sizeof(SWFGLYPH)*glyphcount);
183     font->glyph2ascii = (U16*)malloc(sizeof(U16)*glyphcount);
184     memset(font->glyph2ascii, 0, sizeof(U16)*glyphcount);
185
186     if(flags1&8) { // wide offsets
187         for(t=0;t<glyphcount;t++)
188             swf_GetU32(tag); //offset[t]
189         swf_GetU32(tag); // fontcodeoffset
190     } else {
191         for(t=0;t<glyphcount;t++)
192             swf_GetU16(tag); //offset[t]
193         swf_GetU16(tag); // fontcodeoffset
194     }
195     for(t=0;t<glyphcount;t++)
196         swf_GetSimpleShape(tag,&(font->glyph[t].shape));
197
198     maxcode = 0;
199     for(t=0;t<glyphcount;t++) {
200         int code;
201         if(flags1&4) // wide codes
202             code = swf_GetU16(tag);
203         else
204             code = swf_GetU8(tag);
205         font->glyph2ascii[t] = code;
206         if(code > maxcode)
207             maxcode = code;
208     }
209     maxcode++;
210     if(maxcode<256)
211         maxcode=256;
212     font->maxascii = maxcode;
213     font->ascii2glyph = (int*)malloc(sizeof(int)*maxcode);
214     memset(font->ascii2glyph, -1, sizeof(int)*maxcode);
215     for(t=0;t<glyphcount;t++) 
216     {
217         font->ascii2glyph[font->glyph2ascii[t]] = t;
218     }
219
220     if(flags1&128) { // has layout
221         U16 kerningcount;
222         font->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
223         font->layout->ascent=swf_GetU16(tag);
224         font->layout->descent=swf_GetU16(tag);
225         font->layout->leading=swf_GetU16(tag);
226         for(t=0;t<glyphcount;t++) {
227             S16 advance = swf_GetS16(tag);
228             font->glyph[t].advance = advance;
229         }
230         font->layout->bounds = malloc(glyphcount*sizeof(SRECT));
231         for(t=0;t<glyphcount;t++) {
232             swf_ResetReadBits(tag);
233             swf_GetRect(tag, font->layout->bounds);
234         }
235         kerningcount = swf_GetU16(tag);
236         font->layout->kerningcount = kerningcount;
237         font->layout->kerning = (SWFKERNING*)malloc(sizeof(SWFKERNING)*kerningcount);
238         if(kerningcount) {
239             font->layout->kerning = 
240                 malloc(sizeof(*font->layout->kerning)* kerningcount);
241             for(t=0;t<kerningcount;t++)
242             {
243                 if(flags1&4) { // wide codes
244                     font->layout->kerning[t].char1 = swf_GetU16(tag);
245                     font->layout->kerning[t].char2 = swf_GetU16(tag);
246                 } else {
247                     font->layout->kerning[t].char1 = swf_GetU8(tag);
248                     font->layout->kerning[t].char2 = swf_GetU8(tag);
249                 }
250                 font->layout->kerning[t].adjustment = swf_GetS16(tag);
251             }
252         }
253     }
254     swf_RestoreTagPos(t);
255     return font->id;
256 }
257
258
259 #define FEDTJ_PRINT  0x01
260 #define FEDTJ_MODIFY 0x02
261
262 int swf_FontExtract_DefineText(int id,SWFFONT * f,TAG * t,int jobs)
263 { U16    cid;
264   SRECT  r;
265   MATRIX m;
266   U8     gbits, abits, flags;
267   int    fid;
268
269   fid = 0;
270
271   swf_SaveTagPos(t);
272   swf_SetTagPos(t,0);
273
274   cid = swf_GetU16(t);
275   swf_GetRect(t,&r);
276   swf_GetMatrix(t,&m);
277   gbits = swf_GetU8(t);
278   abits = swf_GetU8(t);
279
280   flags = swf_GetU8(t);
281   
282   while(flags)
283   { if (flags&TF_TEXTCONTROL)
284     { if (flags&TF_HASFONT) fid = swf_GetU16(t);
285       if (flags&TF_HASCOLOR)
286       { swf_GetU8(t); // rgb
287         swf_GetU8(t);
288         swf_GetU8(t);
289         if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_GetU8(t);
290       }
291       if (flags&TF_HASXOFFSET) swf_GetS16(t);
292       if (flags&TF_HASYOFFSET) swf_GetS16(t);
293       if (flags&TF_HASFONT) swf_GetU16(t);
294     }
295     else
296     { int i;
297       for (i=0;i<flags;i++)
298       { int glyph;
299         int adv;
300         glyph = swf_GetBits(t,gbits);
301         adv = swf_GetBits(t,abits);
302         if (id==fid)                    // mitlesen ?
303         { int code = f->glyph2ascii[glyph];
304           if (jobs&FEDTJ_PRINT) printf("%c",code);
305           if (jobs&FEDTJ_MODIFY)
306             /*if (!f->glyph[code].advance)*/ f->glyph[glyph].advance = adv;
307         }
308       }
309       if ((id==fid)&&(jobs&FEDTJ_PRINT)) printf("\n");
310     }
311     flags = swf_GetU8(t);
312   }
313   
314   swf_RestoreTagPos(t);
315   return id;
316 }  
317
318 int swf_FontExtract(SWF * swf,int id,SWFFONT * * font)
319 { TAG * t;
320   SWFFONT * f;
321     
322   if ((!swf)||(!font)) return -1;
323
324   f = (SWFFONT *)malloc(sizeof(SWFFONT)); font[0] = f;
325   if (!f) return -1;
326   
327   memset(f,0x00,sizeof(SWFFONT));
328
329   t = swf->firstTag;
330
331   while (t)
332   { int nid = 0;
333     switch (swf_GetTagID(t))
334     { case ST_DEFINEFONT:
335         nid = swf_FontExtract_DefineFont(id,f,t);
336         break;
337     
338       case ST_DEFINEFONT2:
339         nid = swf_FontExtract_DefineFont2(id,f,t);
340         break;
341         
342       case ST_DEFINEFONTINFO:
343         nid = swf_FontExtract_DefineFontInfo(id,f,t);
344         break;
345         
346       case ST_DEFINETEXT:
347       case ST_DEFINETEXT2:
348         nid = swf_FontExtract_DefineText(id,f,t,f->layout?0:FEDTJ_MODIFY);
349         break;
350     }
351     if (nid>0) id = nid;
352     t = swf_NextTag(t);
353   }
354   return 0;
355 }
356
357 int swf_FontSetID(SWFFONT * f,U16 id) { if (!f) return -1; f->id = id; return 0; }
358
359 int swf_FontReduce(SWFFONT * f,FONTUSAGE * use)
360 { int i,j,num;
361   if ((!f)||(!use)) return -1;
362
363   j = 0;
364   for (i=0;i<f->numchars;i++)
365     if (f->glyph[i].shape)
366     { if (f->glyph2ascii[i]<MAX_CHAR_PER_FONT && 
367             use->code[f->glyph2ascii[i]])
368       { f->ascii2glyph[f->glyph2ascii[i]] = j;
369         f->glyph2ascii[j] = f->glyph2ascii[i];
370         f->glyph[j] = f->glyph[i];
371         j++;
372       }
373       else
374       { swf_ShapeFree(f->glyph[i].shape);
375         f->ascii2glyph[f->glyph2ascii[i]] = -1;
376         f->glyph2ascii[i] = 0;
377         f->glyph[i].shape   = NULL;
378         f->glyph[i].advance = 0;
379       }
380     } else f->ascii2glyph[f->glyph2ascii[i]] = -1;
381
382   f->numchars = j;
383     
384   return j;
385 }
386
387 int swf_FontInitUsage(FONTUSAGE * use)
388 { if (!use) return -1;
389   memset(use->code,0,sizeof(use->code[0])*MAX_CHAR_PER_FONT);
390   return 0;
391 }
392
393 int swf_FontUse(FONTUSAGE * use,U8 * s)
394 { if ((!use)||(!s)) return -1;
395   while (s[0])
396   { use->code[s[0]] = 1;
397     s++;
398   }
399   return 0;  
400 }
401
402 int swf_FontSetDefine(TAG * t,SWFFONT * f)
403 { U16*ofs = (U16*)malloc(f->numchars*2);
404   int p,i,j;
405     
406   if ((!t)||(!f)) return -1;
407   swf_ResetWriteBits(t);
408   swf_SetU16(t,f->id);
409
410   p = 0; j = 0;
411   for (i=0;i<f->numchars;i++)
412     if (f->glyph[i].shape)
413     { ofs[j++] = p;
414       p+=swf_SetSimpleShape(NULL,f->glyph[i].shape);
415     }
416
417   for (i=0;i<j;i++) swf_SetU16(t,ofs[i]+j*2);
418   
419   for (i=0;i<f->numchars;i++)
420     if (f->glyph[i].shape)
421       swf_SetSimpleShape(t,f->glyph[i].shape);
422   
423   swf_ResetWriteBits(t);
424   free(ofs);
425   return 0;
426 }
427
428 int swf_FontSetInfo(TAG * t,SWFFONT * f)
429 { int l,i;
430   U8 wide=0;
431   if ((!t)||(!f)) return -1;
432   swf_ResetWriteBits(t);
433   swf_SetU16(t,f->id);
434   l = strlen(f->name); if (l>255) l = 255;
435   swf_SetU8(t,l);
436   swf_SetBlock(t,f->name,l);
437   if(f->numchars>=256)
438       wide=1;
439   swf_SetU8(t,(f->flags&0xfe)|wide);
440
441   for (i=0;i<f->numchars;i++) {
442     if (f->glyph[i].shape)
443       wide?swf_SetU16(t,f->glyph2ascii[i]):
444            swf_SetU8(t,f->glyph2ascii[i]);
445   }
446   
447   return 0;
448 }
449
450 int swf_FontExport(int handle,SWFFONT * f)
451 { int l;
452   int i;
453   if (!f) return 0;
454
455   l = sizeof(SWFFONT);
456   if (handle>=0)
457     if (write(handle,f,sizeof(SWFFONT))!=sizeof(SWFFONT)) return -1;
458
459   if (f->name)
460   { U16 ln = strlen(f->name);
461     l+=2+ln;
462     if (handle>=0)
463     { if (write(handle,&ln,2)!=2) return -1;
464       if (write(handle,f->name,ln)!=ln) return -1;
465     }
466   }
467
468   if (f->layout)
469   { l+=sizeof(SWFLAYOUT);
470     if (handle>=0)
471       if (write(handle,f->layout,sizeof(SWFLAYOUT))!=sizeof(SWFLAYOUT)) return -1;
472 /*    new kerning struct. hope commenting this out doesn't break things
473       if (f->layout->kerning.data)
474     { l+=f->layout->kerning.count*4;
475       if (handle>=0)
476         if (write(handle,f->layout->kerning.data,f->layout->kerning.count*4)!=f->layout->kerning.count*4) return -1;
477     }*/
478   }
479
480   for (i=0;i<f->numchars;i++)
481   { if (f->glyph[i].shape)
482     { int ll = swf_ShapeExport(handle,f->glyph[i].shape);
483       if (ll<0) return -1;
484       l+=ll;
485     }  
486   }
487
488   return l;
489 }
490
491 int swf_FontImport(int handle,SWFFONT * * font)
492 { SWFFONT * f;
493   int layout;
494   int i = 0;
495
496   if ((!font)||(handle<0)) return -1;
497
498   f = (SWFFONT *)malloc(sizeof(SWFFONT)); font[0] = f;
499   if (!f) return -1;
500
501   memset(f,0x00,sizeof(SWFFONT));
502   
503   if (read(handle,f,sizeof(SWFFONT))!=sizeof(SWFFONT)) goto fehler;
504
505   layout = (f->layout)?1:0;             // avoid illegal free()
506   f->layout = NULL;
507
508   if (f->name)
509   { U16 ln;
510     f->name = NULL;
511     if (read(handle,&ln,2)!=2) goto fehler;
512     f->name = (U8*)malloc(ln+1);
513     if (!f->name) goto fehler;
514     if (read(handle,f->name,ln)!=ln) goto fehler;
515     f->name[ln] = 0;
516   }
517
518   if (f->layout)
519   { f->layout = (SWFLAYOUT *)malloc(sizeof(SWFLAYOUT));
520     if (!f->layout) goto fehler;
521     if (read(handle,f->layout,sizeof(SWFLAYOUT))!=sizeof(SWFLAYOUT)) goto fehler;
522     /* new kerning struct. hope commenting this out doesn't break things
523     if (f->layout->kerning.data)
524     { int l = f->layout->kerning.count*4;
525       f->layout->kerning.data = (U8*)malloc(l);
526       if (!f->layout->kerning.data) goto fehler;
527       if (read(handle,f->layout->kerning.data,l)!=l) goto fehler;
528     } */
529   }
530
531   for (i=0;i<f->numchars;i++)
532   { if (f->glyph[i].shape)
533     { if (swf_ShapeImport(handle,&f->glyph[i].shape)<0) goto fehler;
534     }
535   }
536
537   f->id = 0;
538   
539   return 0;
540   
541 fehler:
542   if (f) for (;i<MAX_CHAR_PER_FONT;i++) f->glyph[i].shape = NULL;
543   swf_FontFree(f);
544   font[0] = NULL;
545   return -1;
546 }
547
548 int swf_TextPrintDefineText(TAG * t,SWFFONT * f)
549 { int id = swf_GetTagID(t);
550   if ((id==ST_DEFINETEXT)||(id==ST_DEFINETEXT2)) swf_FontExtract_DefineText(f->id,f,t,FEDTJ_PRINT);
551     else return -1;
552   return 0;
553 }
554
555 void swf_LayoutFree(SWFLAYOUT * l)
556 { if (l)
557   { if (l->kerning) free(l->kerning);
558     l->kerning = NULL;
559     if (l->bounds) free(l->bounds);
560     l->bounds = NULL;
561   }
562   free(l);
563 }
564
565 void swf_FontFree(SWFFONT * f)
566 { if (f)
567   { int i;
568       
569     if (f->name) free(f->name);
570     if (f->layout) swf_LayoutFree(f->layout);
571
572     f->name = NULL;
573     f->layout = NULL;
574
575     if(f->glyph) {
576       for (i=0;i<f->numchars;i++)
577         if (f->glyph[i].shape)
578         { swf_ShapeFree(f->glyph[i].shape);
579           f->glyph[i].shape = NULL;
580         }
581       free(f->glyph);
582       f->glyph = NULL;
583     }
584     if(f->ascii2glyph) {
585       free(f->ascii2glyph);
586       f->ascii2glyph = NULL;
587     }
588     if(f->glyph2ascii) {
589       free(f->glyph2ascii);
590       f->glyph2ascii = NULL;
591     }
592   }
593   free(f);
594 }
595
596 int swf_TextSetInfoRecord(TAG * t,SWFFONT * font,U16 size,RGBA * color,S16 dx,S16 dy)
597 { U8 flags;
598   if (!t) return -1;
599
600   flags = TF_TEXTCONTROL|(font?TF_HASFONT:0)|(color?TF_HASCOLOR:0)|(dx?TF_HASXOFFSET:0)|(dy?TF_HASYOFFSET:0);
601
602   swf_SetU8(t,flags);
603   if (font) swf_SetU16(t,font->id);
604   if (color)
605   { if (swf_GetTagID(t)==ST_DEFINETEXT2) swf_SetRGBA(t,color);
606     else swf_SetRGB(t,color);
607   }
608   if (dx) swf_SetS16(t,dx);
609   if (dy) swf_SetS16(t,dy);
610   if (font) swf_SetU16(t,size);
611   
612   return 0;
613 }
614
615 int swf_TextCountBits(SWFFONT * font,U8 * s,int scale,U8 * gbits,U8 * abits)
616 { U16 g,a;
617   if ((!s)||(!font)||((!gbits)&&(!abits))||(!font->ascii2glyph)) return -1;
618   g = a = 0;
619
620   while(s[0])
621   { 
622     int glyph = font->ascii2glyph[s[0]];
623     if(glyph>=0) {
624        g = swf_CountBits(glyph,g);
625        a = swf_CountBits((((U32)font->glyph[glyph].advance)*scale)/100,a);
626     }
627     s++;
628   }
629
630   if (gbits) gbits[0] = (U8)g;
631   if (abits) abits[0] = (U8)a;
632
633   return 0;
634 }
635
636 int swf_TextSetCharRecord(TAG * t,SWFFONT * font,U8 * s,int scale,U8 gbits,U8 abits)
637 { int l,i;
638     
639   if ((!t)||(!font)||(!s)||(!font->ascii2glyph)) return -1;
640
641   l = strlen(s);
642   if (l>0x7f) l = 0x7f;
643   swf_SetU8(t,l);
644
645   for (i=0;i<l;i++)
646   { 
647     int g = font->ascii2glyph[s[i]];
648     if(g>=0) {
649       swf_SetBits(t,g,gbits);
650       swf_SetBits(t,(((U32)font->glyph[g].advance)*scale)/100,abits);
651     }
652   }
653
654   swf_ResetWriteBits(t);
655   return 0;
656 }
657
658 U32 swf_TextGetWidth(SWFFONT * font,U8 * s,int scale)
659 { U32 res = 0;
660
661   if (font&&s)
662   { while (s[0])
663     { 
664       int g = font->ascii2glyph[*s];
665       if(g>=0)
666         res += font->glyph[g].advance;
667       s++;
668     }
669     if (scale) res = (res*scale)/100;
670   }
671   return res;
672 }
673
674 void swf_WriteFont(SWFFONT*font, char* filename, int useDefineFont2)
675 { SWF swf;
676   TAG * t;
677   SRECT r;
678   RGBA rgb;
679   int f;
680
681   if(useDefineFont2) {
682       fprintf(stderr, "DefineFont2 is not yet supported!\n");
683       useDefineFont2 = 0;
684   }
685
686   font->id = WRITEFONTID; //"FN"
687
688   memset(&swf,0x00,sizeof(SWF));
689
690   swf.fileVersion    = 4;
691   swf.frameRate      = 0x4000;
692   swf.movieSize.xmax = 20*640;
693   swf.movieSize.ymax = 20*480;
694
695   if(!useDefineFont2)
696   /* if we use DefineFont1 to store the characters,
697      we have to build a textfield to store the
698      advance values. While at it, we can also
699      make the whole .swf viewable */
700   {
701       t = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
702       swf.firstTag = t;
703             rgb.r = 0xff;
704             rgb.g = 0xff;
705             rgb.b = 0xff;
706             swf_SetRGB(t,&rgb);
707       t = swf_InsertTag(t,ST_DEFINEFONT);
708   }
709   else
710   {
711       t = swf_InsertTag(NULL,ST_DEFINEFONT);
712       swf.firstTag = t;
713   }
714
715         swf_FontSetDefine(t,font);
716   
717   t = swf_InsertTag(t,ST_DEFINEFONTINFO);
718         swf_FontSetInfo(t,font);
719
720   if(!useDefineFont2)
721   {     int textscale = 400;
722         int s;
723         int xmax = 0;
724         int ymax = textscale * 20;
725         U8 gbits,abits;
726         char text[257];
727         int x,y;
728         text[256]=0;
729         for(s=0;s<256;s++)
730         {
731             int g = font->ascii2glyph[s];
732             text[s] = s;
733             if(g>=0) {
734                if(font->glyph[g].advance*textscale/100 > xmax)
735                    xmax = font->glyph[g].advance*textscale/100;
736             }
737         }
738         swf.movieSize.xmax = xmax*20;
739         swf.movieSize.ymax = ymax;
740
741         t = swf_InsertTag(t,ST_DEFINETEXT);
742
743             swf_SetU16(t,font->id+1);            // ID
744
745             r.xmin = 0;
746             r.ymin = 0;
747             r.xmax = swf.movieSize.xmax*20;
748             r.ymax = swf.movieSize.ymax;
749             
750             swf_SetRect(t,&r);
751
752             swf_SetMatrix(t,NULL);
753
754             abits = swf_CountBits(xmax*16, 0);
755             gbits = 8;
756             
757             swf_SetU8(t,gbits);
758             swf_SetU8(t,abits);
759
760             rgb.r = 0x00;
761             rgb.g = 0x00;
762             rgb.b = 0x00;
763             for(y=0;y<16;y++)
764             {
765                 int c=0,lastx=-1, firstx=0;
766                 for(x=0;x<16;x++) {
767                     int g = font->ascii2glyph[y*16+x];
768                     if(g>=0 && font->glyph[g].shape) {
769                         c++;
770                         if(lastx<0) 
771                             lastx = x*xmax;
772                     }
773                 }
774                 if(c) {
775                   swf_TextSetInfoRecord(t,font,textscale,&rgb,lastx+1,textscale*y);
776                   for(x=0;x<16;x++)
777                   {
778                       int g = font->ascii2glyph[y*16+x];
779                       if(g>=0 && font->glyph[g].shape) {
780                         if(lastx != x*xmax) {
781                             swf_TextSetInfoRecord(t,0,0,0,x*xmax+1,0);
782                         }
783                         swf_SetU8(t,1);
784                         swf_SetBits(t, g, gbits);
785                         swf_SetBits(t, font->glyph[g].advance, abits);
786                         lastx = x*xmax+font->glyph[g].advance;
787                         swf_ResetWriteBits(t);
788                       }
789                   }
790                 } 
791             }
792             swf_SetU8(t,0);
793
794         
795         t = swf_InsertTag(t,ST_PLACEOBJECT2);
796
797             swf_ObjectPlace(t,font->id+1,1,NULL,NULL,NULL);
798      
799         t = swf_InsertTag(t,ST_SHOWFRAME);
800   }
801   
802   t = swf_InsertTag(t,ST_END);
803
804   f = open(filename, O_RDWR|O_CREAT|O_TRUNC,0644);
805   if FAILED(swf_WriteSWF(f,&swf)) fprintf(stderr,"WriteSWF() failed in writeFont().\n");
806   close(f);
807
808   swf_FreeTags(&swf);
809 }
810
811 SWFFONT* swf_ReadFont(char* filename)
812 {
813   int f;
814   SWF swf;
815   if(!filename)
816       return 0;
817   f = open(filename,O_RDONLY);
818   
819   if (f<0 || swf_ReadSWF(f,&swf)<0)
820   { fprintf(stderr,"%s is not a valid SWF font file or contains errors.\n",filename);
821     close(f);
822     return 0;
823   }
824   else
825   { SWFFONT*font;
826     close(f);
827     if(swf_FontExtract(&swf, WRITEFONTID, &font) < 0)
828        return 0;
829     swf_FreeTags(&swf);
830     return font;
831   }
832 }
833