3 Example for font handling
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
11 Usage: ./dumpfont filename.swf > myfont.c
13 Part of the swftools package.
15 Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
17 This file is distributed under the GPL, see file COPYING for details
24 #include "../rfxswf.h"
26 #define PRINTABLE(a) (((a>0x20)&&(a<0xff)&&(a!='\\'))?a:0x20)
30 void DumpFont(SWFFONT * f,char * name)
32 int*gpos = malloc(sizeof(int*)*f->numchars);
33 memset(gpos,0,sizeof(int*)*f->numchars);
38 printf("U8 Glyphs_%s[] = {\n\t ",name);
40 for (i=0;i<f->numchars;i++)
41 if (f->glyph[i].shape)
42 { SHAPE * s = f->glyph[i].shape;
43 int j,l = (s->bitlen+7)/8;
47 { printf("0x%02x, ",s->data[j]);
48 if ((n&7)==7) printf("\n\t ");
56 printf("\nSWFFONT * Font_%s(U16 id)\n",name);
57 printf("{ SWFFONT * f;\n int i;\n\n");
58 printf(" if (!(f=malloc(sizeof(SWFFONT)))) return NULL;\n");
59 printf(" memset(f,0x00,sizeof(SWFFONT));\n");
60 printf(" f->id = id;\n");
61 printf(" f->version = %d;\n", f->version);
62 printf(" f->name = strdup(\"%s\");\n",f->name);
63 printf(" f->style = 0x%02x;\n",f->style);
64 printf(" f->encoding = 0x%02x;\n",f->encoding);
65 printf(" f->numchars = %d;\n",f->numchars);
66 printf(" f->maxascii = %d;\n",f->maxascii);
67 printf(" f->glyph = (SWFGLYPH*)malloc(sizeof(SWFGLYPH)*%d);\n",f->numchars);
68 printf(" f->glyph2ascii = (U16*)malloc(sizeof(U16)*%d);\n",f->numchars);
69 printf(" f->ascii2glyph = (int*)malloc(sizeof(int)*%d);\n",f->maxascii);
70 printf(" memset(f->ascii2glyph, -1, sizeof(int)*%d);\n\n", f->maxascii);
72 printf(" f->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));\n");
73 printf(" f->layout->ascent = %d;\n", f->layout->ascent);
74 printf(" f->layout->descent = %d;\n", f->layout->descent);
75 printf(" f->layout->leading = %d;\n", f->layout->leading);
76 printf(" f->layout->kerningcount = 0;\n");
77 printf(" f->layout->kerning = 0;\n");
78 printf(" f->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*%d);\n", f->numchars);
79 printf(" memset(f->layout->bounds, 0, sizeof(SRECT)*%d);\n\n", f->numchars);
82 for (i=0;i<f->numchars;i++)
83 if (f->glyph[i].shape)
85 printf(" addGlyph(f,%3i, 0x%03x,%4i, &Glyphs_%s[0x%04x], %4i, ",
86 i, f->glyph2ascii[i], f->glyph[i].advance, name, gpos[i],
87 f->glyph[i].shape->bitlen);
88 if(f->layout && f->layout->bounds) {
89 printf("%d, %d, %d, %d);",
90 f->layout->bounds[i].xmin,
91 f->layout->bounds[i].ymin,
92 f->layout->bounds[i].xmax,
93 f->layout->bounds[i].ymax);
95 printf("/* bbox not set */ 0, 0, 0, 0);");
97 printf(" // %c\n", PRINTABLE(f->glyph2ascii[i]));
100 printf(" return f;\n}\n\n");
104 void DumpGlobal(char * funcname)
105 { 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);
106 printf("{ SHAPE * s;\n U32 l = (bitlen+7)/8;\n\n");
107 printf(" if (FAILED(swf_ShapeNew(&s))) return;\n");
108 printf(" s->data = malloc(l);\n");
109 printf(" if (!s->data) { swf_ShapeFree(s); return; }\n\n");
110 printf(" f->glyph2ascii[i] = ascii;\n");
111 printf(" f->ascii2glyph[ascii] = i;\n");
112 printf(" f->glyph[i].advance = advance;\n");
113 printf(" f->glyph[i].shape = s;\n");
114 printf(" s->bitlen = bitlen;\n");
115 printf(" s->bits.fill = 1;\n");
116 printf(" if(f->layout && f->layout->bounds)\n");
117 printf(" { f->layout->bounds[i].xmin = xmin;\n");
118 printf(" f->layout->bounds[i].ymin = ymin;\n");
119 printf(" f->layout->bounds[i].xmax = xmax;\n");
120 printf(" f->layout->bounds[i].ymax = ymax;\n");
122 printf(" memcpy(s->data,data,l);\n}\n\n");
126 void fontcallback(U16 id,U8 * name)
132 swf_FontExtract(&swf,id,&font);
133 sprintf(s,"%s%s%s",name,swf_FontIsBold(font)?"_bold":"",swf_FontIsItalic(font)?"_italic":"");
138 if(!((*ss>='a' && *ss<='z') ||
139 (*ss>='A' && *ss<='Z') ||
140 (*ss>='0' && *ss<='9' && ss!=s) ||
151 int main(int argc,char ** argv)
155 { f = open(argv[1],O_RDONLY);
157 { if FAILED(swf_ReadSWF(f,&swf))
158 { fprintf(stderr,"%s is not a valid SWF file or contains errors.\n",argv[1]);
164 sprintf(fn,"fn%04x",getpid()); // avoid name conflicts @ using multiple fonts
165 printf("#define addGlyph %s\n",fn);
167 swf_FontEnumerate(&swf,&fontcallback);
169 printf("#undef addGlyph\n");
171 } else fprintf(stderr,"File not found: %s\n",argv[1]);
173 else fprintf(stderr,"\nreflex SWF Font Dump Utility\n(w) 2000 by Rainer Boehme <rb@reflex-studio.de>\n\nUsage: dumpfont filename.swf\n");