Generated from configure.in
[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    Part of the swftools package.
14
15    Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
16  
17    This program is free software; you can redistribute it and/or modify
18    it under the terms of the GNU General Public License as published by
19    the Free Software Foundation; either version 2 of the License, or
20    (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25    GNU General Public License for more details.
26
27    You should have received a copy of the GNU General Public License
28    along with this program; if not, write to the Free Software
29    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
30
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include "../rfxswf.h"
35
36 #define PRINTABLE(a) (((a>0x20)&&(a<0xff)&&(a!='\\'))?a:0x20)
37
38 SWF swf;
39
40 void DumpFont(SWFFONT * f,char * name)
41 { int n,i;
42   int*gpos = malloc(sizeof(int*)*f->numchars);
43   memset(gpos,0,sizeof(int*)*f->numchars);
44
45   // Glyph Shapes Data
46
47   n = 0;
48   printf("U8 Glyphs_%s[] = {\n\t  ",name);
49   
50   for (i=0;i<f->numchars;i++)
51     if (f->glyph[i].shape)
52     { SHAPE * s = f->glyph[i].shape;
53       int j,l = (s->bitlen+7)/8;
54       gpos[i] = n;
55
56       for (j=0;j<l;j++)
57       { printf("0x%02x, ",s->data[j]);
58         if ((n&7)==7) printf("\n\t  ");
59         n++;
60       }
61     }
62   printf("0x00 };\n");
63   
64   // SWFFONT function
65
66   printf("\nSWFFONT * Font_%s(U16 id)\n",name);
67   printf("{ SWFFONT * f;\n  int i;\n\n");
68   printf("  if (!(f=malloc(sizeof(SWFFONT)))) return NULL;\n");
69   printf("  memset(f,0x00,sizeof(SWFFONT));\n");
70   printf("  f->id       = id;\n");
71   printf("  f->version  = %d;\n", f->version);
72   printf("  f->name     = strdup(\"%s\");\n",f->name);
73   printf("  f->style    = 0x%02x;\n",f->style);
74   printf("  f->encoding = 0x%02x;\n",f->encoding);
75   printf("  f->numchars = %d;\n",f->numchars);
76   printf("  f->maxascii = %d;\n",f->maxascii);
77   printf("  f->glyph    = (SWFGLYPH*)malloc(sizeof(SWFGLYPH)*%d);\n",f->numchars);
78   printf("  f->glyph2ascii = (U16*)malloc(sizeof(U16)*%d);\n",f->numchars);
79   printf("  f->ascii2glyph = (int*)malloc(sizeof(int)*%d);\n",f->maxascii);
80   printf("  memset(f->ascii2glyph, -1, sizeof(int)*%d);\n\n", f->maxascii);
81   if(f->layout) {
82       printf("  f->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));\n");
83       printf("  f->layout->ascent = %d;\n", f->layout->ascent);
84       printf("  f->layout->descent = %d;\n", f->layout->descent);
85       printf("  f->layout->leading = %d;\n", f->layout->leading);
86       printf("  f->layout->kerningcount = 0;\n");
87       printf("  f->layout->kerning = 0;\n");
88       printf("  f->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*%d);\n", f->numchars);
89       printf("  memset(f->layout->bounds, 0, sizeof(SRECT)*%d);\n\n", f->numchars);
90   }
91
92   for (i=0;i<f->numchars;i++)
93     if (f->glyph[i].shape)
94     { 
95         printf("  addGlyph(f,%3i, 0x%03x,%4i, &Glyphs_%s[0x%04x], %4i, ",
96                i, f->glyph2ascii[i], f->glyph[i].advance, name, gpos[i],
97                f->glyph[i].shape->bitlen);
98         if(f->layout && f->layout->bounds) {
99             printf("%d, %d, %d, %d);",
100                     f->layout->bounds[i].xmin,
101                     f->layout->bounds[i].ymin,
102                     f->layout->bounds[i].xmax,
103                     f->layout->bounds[i].ymax);
104         } else {
105             printf("/* bbox not set */ 0, 0, 0, 0);");
106         }
107         printf(" // %c\n", PRINTABLE(f->glyph2ascii[i]));
108     }
109
110   printf("  return f;\n}\n\n");
111   free(gpos);
112 }
113
114 void DumpGlobal(char * funcname)
115 { printf("\nvoid %s(SWFFONT * f,int i,U16 ascii,U16 advance,U8 * data,U32 bitlen,int xmin,int ymin,int xmax, int ymax)\n",funcname);
116   printf("{ SHAPE * s;\n  U32 l = (bitlen+7)/8;\n\n");
117   printf("  if (FAILED(swf_ShapeNew(&s))) return;\n");
118   printf("  s->data = malloc(l);\n");
119   printf("  if (!s->data) { swf_ShapeFree(s); return; }\n\n");
120   printf("  f->glyph2ascii[i]     = ascii;\n");
121   printf("  f->ascii2glyph[ascii] = i;\n");
122   printf("  f->glyph[i].advance   = advance;\n");
123   printf("  f->glyph[i].shape     = s;\n");
124   printf("  s->bitlen             = bitlen;\n");
125   printf("  s->bits.fill          = 1;\n");
126   printf("  if(f->layout && f->layout->bounds)\n");
127   printf("  {  f->layout->bounds[i].xmin = xmin;\n");
128   printf("     f->layout->bounds[i].ymin = ymin;\n");
129   printf("     f->layout->bounds[i].xmax = xmax;\n");
130   printf("     f->layout->bounds[i].ymax = ymax;\n");
131   printf("  }\n");
132   printf("  memcpy(s->data,data,l);\n}\n\n");
133 }
134
135
136 void fontcallback(U16 id,U8 * name)
137 { SWFFONT * font;
138
139   int f;
140   char s[500],*ss;
141   
142   swf_FontExtract(&swf,id,&font);
143   sprintf(s,"%s%s%s",name,swf_FontIsBold(font)?"_bold":"",swf_FontIsItalic(font)?"_italic":"");
144   
145   ss = s;
146   while(*ss)
147   { 
148     if(!((*ss>='a' && *ss<='z') ||
149          (*ss>='A' && *ss<='Z') ||
150          (*ss>='0' && *ss<='9' && ss!=s) ||
151          (*ss=='_')))
152                 *ss = '_';
153     ss++;
154   }
155
156   DumpFont(font,s);
157   
158   swf_FontFree(font);
159 }
160
161 int main(int argc,char ** argv)
162 { int f;
163
164   if (argc>1)
165   { f = open(argv[1],O_RDONLY);
166     if (f>=0)
167     { if FAILED(swf_ReadSWF(f,&swf))
168       { fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",argv[1]);
169         close(f);
170       }
171       else
172       { char fn[200];
173         close(f);
174         sprintf(fn,"fn%04x",getpid()); // avoid name conflicts @ using multiple fonts
175         printf("#define addGlyph %s\n",fn);
176         DumpGlobal(fn);
177         swf_FontEnumerate(&swf,&fontcallback);
178         swf_FreeTags(&swf);
179         printf("#undef addGlyph\n");
180       }
181     } else fprintf(stderr,"File not found: %s\n",argv[1]);
182   }
183   else fprintf(stderr,"\nreflex SWF Font Dump Utility\n(w) 2000 by Rainer Boehme <rb@reflex-studio.de>\n\nUsage: dumpfont filename.swf\n");
184   
185   return 0;
186 }
187