new examples: dumpfont and text
[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>='A')&&(a<='Z'))||((a>='a')&&(a<='z'))||((a>='0')&&(a<='9'))
29
30 SWF swf;
31
32 void DumpFont(SWFFONT * f,char * name)
33 { int gpos[MAX_CHAR_PER_FONT];
34   int n,i;
35
36   // Glyph Shapes Data
37
38   n = 0;
39   printf("U8 Glyphs_%s[] = {\n\t  ",name);
40   memset(&gpos,0x00,sizeof(gpos));
41   
42   for (i=0;i<MAX_CHAR_PER_FONT;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->name     = strdup(\"%s\");\n",f->name);
64   printf("  f->flags    = 0x%02x;\n\n",f->flags);
65
66   for (i=0;i<MAX_CHAR_PER_FONT;i++)
67     if (f->glyph[i].shape)
68     { printf("  addGlyph(f,%3i, 0x%02x,%4i,%3i, &Glyphs_%s[0x%04x],%4i); // %c\n",
69              i, f->codes[i], f->glyph[i].advance, f->glyph[i].gid, name, gpos[i],
70              f->glyph[i].shape->bitlen,(i!='\\')?i:0x20);
71     }
72
73   printf("  return f;\n}\n\n");
74 }
75
76 void DumpGlobal(char * funcname)
77 { printf("\nvoid %s(SWFFONT * f,int i,U16 code,U16 advance,U16 gid,U8 * data,U32 bitlen)\n",funcname);
78   printf("{ SHAPE * s;\n  U32 l = (bitlen+7)/8;\n\n");
79   printf("  if (FAILED(swf_ShapeNew(&s))) return;\n");
80   printf("  s->data = malloc(l);\n");
81   printf("  if (!s->data) { swf_ShapeFree(s); return; }\n\n");
82   printf("  f->codes[i]         = code;\n");
83   printf("  f->glyph[i].advance = advance;\n");
84   printf("  f->glyph[i].gid     = gid;\n");
85   printf("  f->glyph[i].shape   = s;\n");
86   printf("  s->bitlen           = bitlen;\n");
87   printf("  s->bits.fill        = 1;\n");
88   printf("  memcpy(s->data,data,l);\n}\n\n");
89 }
90
91
92 void fontcallback(U16 id,U8 * name)
93 { SWFFONT * font;
94
95   int f;
96   char s[500],*ss;
97   
98   swf_FontExtract(&swf,id,&font);
99   sprintf(s,"%s%s%s",name,swf_FontIsBold(font)?"_bold":"",swf_FontIsItalic(font)?"_italic":"");
100   
101   ss = s;
102   while(ss[0])
103   { if ((*ss)==0x20) (*ss)='_';
104     ss++;
105   }
106
107   DumpFont(font,s);
108   
109   swf_FontFree(font);
110 }
111
112 int main(int argc,char ** argv)
113 { int f;
114
115   if (argc>1)
116   { f = open(argv[1],O_RDONLY);
117     if (f>=0)
118     { if FAILED(swf_ReadSWF(f,&swf))
119       { fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",argv[1]);
120         close(f);
121       }
122       else
123       { char fn[200];
124         close(f);
125         sprintf(fn,"fn%04x",getpid()); // avoid name conflicts @ using multiple fonts
126         printf("#define addGlyph %s\n",fn);
127         DumpGlobal(fn);
128         swf_FontEnumerate(&swf,&fontcallback);
129         swf_FreeTags(&swf);
130         printf("#undef addGlyph\n");
131       }
132     } else fprintf(stderr,"File not found: %s\n",argv[1]);
133   }
134   else fprintf(stderr,"\nreflex SWF Font Dump Utility\n(w) 2000 by Rainer Boehme <rb@reflex-studio.de>\n\nUsage: dumpfont filename.swf\n");
135   
136   return 0;
137 }
138