minor fixes: swf_SetBits(), cleanup shape struct, fixed dumpfont
[swftools.git] / lib / example / dumpfont.c
1 /* dumpfont.c
2
3    Example for font handling
4
5    This tool dumps font information into C files
6    which can be included to rfxswf applications.
7    This is an easy way for developers to use
8    fonts in applications without dealing with
9    external files.
10
11    Usage: ./dumpfont filename.swf > myfont.c
12
13    Limits: does not parse ST_DEFINEFONT2 
14    
15    Part of the swftools package.
16
17    Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
18  
19    This file is distributed under the GPL, see file COPYING for details 
20
21 */
22
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include "../rfxswf.h"
27
28 #define PRINTABLE(a) (((a>0x20)&&(a<0xff)&&(a!='\\'))?a:0x20)
29
30 SWF swf;
31
32 void DumpFont(SWFFONT * f,char * name)
33 { int n,i;
34   int*gpos = malloc(sizeof(int*)*f->numchars);
35   memset(gpos,0,sizeof(int*)*f->numchars);
36
37   // Glyph Shapes Data
38
39   n = 0;
40   printf("U8 Glyphs_%s[] = {\n\t  ",name);
41   
42   for (i=0;i<f->numchars;i++)
43     if (f->glyph[i].shape)
44     { SHAPE * s = f->glyph[i].shape;
45       int j,l = (s->bitlen+7)/8;
46       gpos[i] = n;
47
48       for (j=0;j<l;j++)
49       { printf("0x%02x, ",s->data[j]);
50         if ((n&7)==7) printf("\n\t  ");
51         n++;
52       }
53     }
54   printf("0x00 };\n");
55   
56   // SWFFONT function
57
58   printf("\nSWFFONT * Font_%s(U16 id)\n",name);
59   printf("{ SWFFONT * f;\n  int i;\n\n");
60   printf("  if (!(f=malloc(sizeof(SWFFONT)))) return NULL;\n");
61   printf("  memset(f,0x00,sizeof(SWFFONT));\n");
62   printf("  f->id       = id;\n");
63   printf("  f->version  = %d;\n", f->version);
64   printf("  f->name     = strdup(\"%s\");\n",f->name);
65   printf("  f->flags    = 0x%02x;\n",f->flags);
66   printf("  f->numchars = %d;\n",f->numchars);
67   printf("  f->maxascii = %d;\n",f->maxascii);
68   printf("  f->glyph    = (SWFGLYPH*)malloc(sizeof(SWFGLYPH)*%d);\n",f->numchars);
69   printf("  f->glyph2ascii = (U16*)malloc(sizeof(U16)*%d);\n",f->numchars);
70   printf("  f->ascii2glyph = (int*)malloc(sizeof(int)*%d);\n",f->maxascii);
71   printf("  memset(f->ascii2glyph, -1, sizeof(int)*%d);\n\n", f->maxascii);
72
73   for (i=0;i<f->numchars;i++)
74     if (f->glyph[i].shape)
75     { printf("  addGlyph(f,%3i, 0x%02x,%4i, &Glyphs_%s[0x%04x],%4i); // %c\n",
76              i, f->glyph2ascii[i], f->glyph[i].advance, name, gpos[i],
77              f->glyph[i].shape->bitlen,PRINTABLE(f->glyph2ascii[i]));
78     }
79
80   printf("  return f;\n}\n\n");
81   free(gpos);
82 }
83
84 void DumpGlobal(char * funcname)
85 { printf("\nvoid %s(SWFFONT * f,int i,U16 ascii,U16 advance,U8 * data,U32 bitlen)\n",funcname);
86   printf("{ SHAPE * s;\n  U32 l = (bitlen+7)/8;\n\n");
87   printf("  if (FAILED(swf_ShapeNew(&s))) return;\n");
88   printf("  s->data = malloc(l);\n");
89   printf("  if (!s->data) { swf_ShapeFree(s); return; }\n\n");
90   printf("  f->glyph2ascii[i]     = ascii;\n");
91   printf("  f->ascii2glyph[ascii] = i;\n");
92   printf("  f->glyph[i].advance   = advance;\n");
93   printf("  f->glyph[i].shape     = s;\n");
94   printf("  s->bitlen             = bitlen;\n");
95   printf("  s->bits.fill          = 1;\n");
96   printf("  memcpy(s->data,data,l);\n}\n\n");
97 }
98
99
100 void fontcallback(U16 id,U8 * name)
101 { SWFFONT * font;
102
103   int f;
104   char s[500],*ss;
105   
106   swf_FontExtract(&swf,id,&font);
107   sprintf(s,"%s%s%s",name,swf_FontIsBold(font)?"_bold":"",swf_FontIsItalic(font)?"_italic":"");
108   
109   ss = s;
110   while(*ss)
111   { 
112     if(!((*ss>='a' && *ss<='z') ||
113          (*ss>='A' && *ss<='Z') ||
114          (*ss>='0' && *ss<='9' && ss!=s) ||
115          (*ss=='_')))
116                 *ss = '_';
117     ss++;
118   }
119
120   DumpFont(font,s);
121   
122   swf_FontFree(font);
123 }
124
125 int main(int argc,char ** argv)
126 { int f;
127
128   if (argc>1)
129   { f = open(argv[1],O_RDONLY);
130     if (f>=0)
131     { if FAILED(swf_ReadSWF(f,&swf))
132       { fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",argv[1]);
133         close(f);
134       }
135       else
136       { char fn[200];
137         close(f);
138         sprintf(fn,"fn%04x",getpid()); // avoid name conflicts @ using multiple fonts
139         printf("#define addGlyph %s\n",fn);
140         DumpGlobal(fn);
141         swf_FontEnumerate(&swf,&fontcallback);
142         swf_FreeTags(&swf);
143         printf("#undef addGlyph\n");
144       }
145     } else fprintf(stderr,"File not found: %s\n",argv[1]);
146   }
147   else fprintf(stderr,"\nreflex SWF Font Dump Utility\n(w) 2000 by Rainer Boehme <rb@reflex-studio.de>\n\nUsage: dumpfont filename.swf\n");
148   
149   return 0;
150 }
151