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 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.
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.
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 */
34 #include "../rfxswf.h"
36 #define PRINTABLE(a) (((a>0x20)&&(a<0xff)&&(a!='\\'))?a:0x20)
40 void DumpFont(SWFFONT
* f
,char * name
)
42 int*gpos
= malloc(sizeof(int*)*f
->numchars
);
43 memset(gpos
,0,sizeof(int*)*f
->numchars
);
48 printf("U8 Glyphs_%s[] = {\n\t ",name
);
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;
57 { printf("0x%02x, ",s
->data
[j
]);
58 if ((n
&7)==7) printf("\n\t ");
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
);
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
);
92 for (i
=0;i
<f
->numchars
;i
++)
93 if (f
->glyph
[i
].shape
)
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
);
105 printf("/* bbox not set */ 0, 0, 0, 0);");
107 printf(" // %c\n", PRINTABLE(f
->glyph2ascii
[i
]));
110 printf(" return f;\n}\n\n");
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");
132 printf(" memcpy(s->data,data,l);\n}\n\n");
136 void fontcallback(U16 id
,U8
* name
,void*data
)
142 swf_FontExtract(&swf
,id
,&font
);
143 sprintf(s
,"%s%s%s",name
,swf_FontIsBold(font
)?"_bold":"",swf_FontIsItalic(font
)?"_italic":"");
148 if(!((*ss
>='a' && *ss
<='z') ||
149 (*ss
>='A' && *ss
<='Z') ||
150 (*ss
>='0' && *ss
<='9' && ss
!=s
) ||
161 int main(int argc
,char ** argv
)
165 { f
= open(argv
[1],O_RDONLY
);
167 { if FAILED(swf_ReadSWF(f
,&swf
))
168 { fprintf(stderr
,"%s is not a valid SWF file or contains errors.\n",argv
[1]);
174 sprintf(fn
,"fn%04x",getpid()); // avoid name conflicts @ using multiple fonts
175 printf("#define addGlyph %s\n",fn
);
177 swf_FontEnumerate(&swf
,&fontcallback
,0);
179 printf("#undef addGlyph\n");
181 } else fprintf(stderr
,"File not found: %s\n",argv
[1]);
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");