3 Utility for converting Type 1 fonts to SWF.
5 Part of the swftools package.
7 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
9 This file is distributed under the GPL, see file COPYING for details
18 #include "../lib/rfxswf.h"
19 #include "../lib/args.h"
20 #include "swfoutput.h"
23 static char * filenames[256];
25 static char * destfilename = "output.swf";
29 struct options_t options[] =
36 int args_callback_option(char*name,char*val)
38 if(!strcmp(name, "V")) {
39 printf("font2swf - part of %s %s\n", PACKAGE, VERSION);
42 else if(!strcmp(name, "o")) {
46 else if(!strcmp(name, "v")) {
51 printf("Unknown option: -%s\n", name);
56 int args_callback_longoption(char*name,char*val)
58 return args_long2shortoption(options, name, val);
60 void args_callback_usage(char*name)
62 printf("Usage: %s file.afm [...]\n", name);
63 printf(" OR: %s all\n", name);
65 printf("\tIf \"all\" is given instead of font names, all standard fonts\n");
66 printf("\t(Courier, Arial etc.) will be created\n");
70 printf("\t-h , --help\t\t Print help and exit\n");
71 printf("\t-o , --output filename\t set output filename\n");
72 printf("\t-V , --version\t\t Print program version and exit\n");
74 int args_callback_command(char*name,char*val)
76 if(!strcmp(name, "all"))
79 filenames[filenum++] = strdup(name);
85 #define standardEncodingSize 256
86 #define symbolEncodingSize 256
87 #define zapfDingbatsEncodingSize 256
88 #define macRomanEncodingSize 256
90 extern char *standardEncoding[standardEncodingSize];
91 extern char *symbolEncoding[symbolEncodingSize];
92 extern char *zapfDingbatsEncoding[zapfDingbatsEncodingSize];
93 extern char *macRomanEncoding[macRomanEncodingSize];
95 extern void drawpath(TAG*tag, T1_OUTLINE*outline, struct swfmatrix*m, int log);
96 extern void resetdrawer();
97 extern void moveto(TAG*tag, plotxy p0);
98 extern void lineto(TAG*tag, plotxy p0);
100 SWFFONT * t1font2swffont(int i)
104 float angle = T1_GetItalicAngle(i);
105 char*fontname = T1_GetFontName(i);
106 char*fullname = T1_GetFullName(i);
107 char*familyname = T1_GetFamilyName(i);
108 float underline = T1_GetUnderlinePosition(i);
109 BBox bbox = T1_GetFontBBox(i);
111 /* if "all" is given, translate the font names in something more
113 if(!strcmp(fullname, "Nimbus Roman No9 L Regular")) fontname = "Helvetica";
114 if(!strcmp(fullname, "Nimbus Roman No9 L Regular Italic")) fontname = "HelveticaItalic";
115 if(!strcmp(fullname, "Nimbus Roman No9 L Medium")) fontname = "HelveticaBold";
116 if(!strcmp(fullname, "Nimbus Roman No9 L Medium Italic")) fontname = "HelveticaBoldItalic";
117 if(!strcmp(fullname, "Nimbus Sans L Regular")) fontname = "Times";
118 if(!strcmp(fullname, "Nimbus Sans L Regular Italic")) fontname = "TimesItalic";
119 if(!strcmp(fullname, "Nimbus Sans L Bold")) fontname = "TimesBold";
120 if(!strcmp(fullname, "Nimbus Sans L Bold Italic")) fontname = "TimesBoldItalic";
121 if(!strcmp(fullname, "Nimbus Mono L Regular")) fontname = "Courier";
122 if(!strcmp(fullname, "Nimbus Mono L Regular Oblique")) fontname = "CourierItalic";
123 if(!strcmp(fullname, "Nimbus Mono L Bold")) fontname = "CourierBold";
124 if(!strcmp(fullname, "Nimbus Mono L Bold Oblique")) fontname = "CourierBoldItalic";
125 if(!strcmp(fullname, "Standard Symbols L")) fontname = "Symbol";
127 char ** encoding = standardEncoding;
128 int encodingsize = standardEncodingSize;
130 printf("processing \"%s\" (\"%s\")...\n", fullname, fontname);
132 if(strstr(fullname, "Dingbats")) {// Zapf Dingbats
133 encoding = zapfDingbatsEncoding;
134 encodingsize = zapfDingbatsEncodingSize;
136 else if(strstr(fullname, "Symbol")) {// Symbol
137 encoding = symbolEncoding;
138 encodingsize = symbolEncodingSize;
141 SWFFONT * wfont = (SWFFONT*)malloc(sizeof(SWFFONT));
142 SWFFont * font = new SWFFont("", i, "");
144 memset(wfont, 0, sizeof(SWFFONT));
147 wfont->name = (U8*)strdup(fontname);
148 wfont->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
149 memset(wfont->layout, 0, sizeof(SWFLAYOUT));
153 for(s=0;s<encodingsize;s++)
156 T1_OUTLINE*outline = font->getOutline(encoding[s], 0);
161 wfont->maxascii = encodingsize;
162 wfont->numchars = num;
164 wfont->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
166 wfont->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
167 memset(wfont->glyph, 0, num*sizeof(SWFGLYPH));
168 wfont->glyph2ascii = (U16*)malloc(num*sizeof(U16));
169 memset(wfont->glyph2ascii, 0, num*sizeof(U16));
170 wfont->ascii2glyph = (int*)malloc(encodingsize*sizeof(int));
171 memset(wfont->ascii2glyph, -1, encodingsize*sizeof(int));
172 wfont->layout->ascent = (U16)(underline - bbox.lly);
173 wfont->layout->descent = (U16)(bbox.ury - underline);
174 wfont->layout->leading = (U16)(wfont->layout->ascent -
175 wfont->layout->descent -
176 (bbox.lly - bbox.ury));
177 wfont->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
178 memset(wfont->layout->bounds, 0, sizeof(SRECT)*num);
179 wfont->layout->kerningcount = 0;
180 wfont->layout->kerning = 0;
183 for(s=0;s<encodingsize;s++)
186 T1_OUTLINE*outline = font->getOutline(encoding[s],0);
187 int width = font->getWidth(encoding[s]);
191 wfont->ascii2glyph[s] = num;
192 wfont->glyph2ascii[num] = s;
193 swf_ShapeNew(&wfont->glyph[num].shape);
194 SHAPE*shape = wfont->glyph[num].shape;
195 int firstx = outline->dest.x/0xffff;
197 TAG*tag = swf_InsertTag(0,ST_DEFINESHAPE);
209 shape->bits.fill = 1;
210 shape->bits.line = 0;
211 swf_ShapeSetStyle(tag,shape,0,1,0);
214 drawpath(tag, outline, &m, log);
216 /*uncomment this to mark the glyph sizes:
217 plotxy p1,p2; p1.x=0; p1.y=0; p2.x=width/8; p2.y=-width/8;
218 moveto(tag, p1); lineto(tag, p2); p1.x += 2; p2.x += 2;
219 lineto(tag, p2); lineto(tag, p1); p1.x -= 2; lineto(tag, p1);// */
221 swf_ShapeSetEnd(tag);
223 wfont->glyph[num].shape->bitlen = (tag->len-1)*8;
224 wfont->glyph[num].shape->data = (U8*)malloc(tag->len-1);
225 memcpy(wfont->glyph[num].shape->data, &tag->data[1], tag->len-1);
228 /* fix bounding box */
231 shape2 = swf_ShapeToShape2(shape);
232 if(!shape2) { fprintf(stderr, "Shape parse error\n");exit(1);}
233 bbox = swf_GetShapeBoundingBox(shape2->lines);
234 swf_Shape2Free(shape2);
235 wfont->layout->bounds[num] = bbox;
236 //wfont->glyph[num].advance = (int)(width/6.4); // 128/20
237 wfont->glyph[num].advance = bbox.xmax/20;
238 if(!wfont->glyph[num].advance) {
239 wfont->glyph[num].advance = firstx;
249 int main(int argc, char ** argv)
253 processargs(argc, argv);
254 //TODO: use tempnam here. Check if environment already contains a
256 putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
257 FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
259 fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
260 fprintf(fi, "ENCODING=%s/fonts:.\n", SWFTOOLS_DATADIR);
261 fprintf(fi, "AFM=%s/fonts:.\n", SWFTOOLS_DATADIR);
262 fprintf(fi, "TYPE1=%s/fonts:.\n", SWFTOOLS_DATADIR);
264 fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
265 fprintf(fi, "ENCODING=%s:.\n", cwd);
266 fprintf(fi, "AFM=%s:.\n", cwd);
267 fprintf(fi, "TYPE1=%s:.\n", cwd);
270 fi = fopen("/tmp/FontDataBase", "wb");
273 fprintf(fi, "%s/fonts/n021003l.afm\n", SWFTOOLS_DATADIR);
274 fprintf(fi, "%s/fonts/n021023l.afm\n", SWFTOOLS_DATADIR);
275 fprintf(fi, "%s/fonts/n021004l.afm\n", SWFTOOLS_DATADIR);
276 fprintf(fi, "%s/fonts/n021024l.afm\n", SWFTOOLS_DATADIR);
277 fprintf(fi, "%s/fonts/n019003l.afm\n", SWFTOOLS_DATADIR);
278 fprintf(fi, "%s/fonts/n019023l.afm\n", SWFTOOLS_DATADIR);
279 fprintf(fi, "%s/fonts/n019004l.afm\n", SWFTOOLS_DATADIR);
280 fprintf(fi, "%s/fonts/n019024l.afm\n", SWFTOOLS_DATADIR);
281 fprintf(fi, "%s/fonts/n022003l.afm\n", SWFTOOLS_DATADIR);
282 fprintf(fi, "%s/fonts/n022023l.afm\n", SWFTOOLS_DATADIR);
283 fprintf(fi, "%s/fonts/n022004l.afm\n", SWFTOOLS_DATADIR);
284 fprintf(fi, "%s/fonts/n022024l.afm\n", SWFTOOLS_DATADIR);
285 fprintf(fi, "%s/fonts/s050000l.afm\n", SWFTOOLS_DATADIR);
286 fprintf(fi, "%s/fonts/d050000l.afm\n", SWFTOOLS_DATADIR);
288 fprintf(fi, "%d\n", filenum);
290 for(t=0;t<filenum;t++) {
291 fprintf(fi, "%s/%s\n", cwd, filenames[t]);
295 /* initialize t1lib */
296 T1_SetBitmapPad( 16);
297 if ((T1_InitLib(NO_LOGFILE)==NULL)){
298 fprintf(stderr, "Initialization of t1lib failed\n");
301 unlink("/tmp/t1lib.config.tmp");
304 for( i=0; i<T1_Get_no_fonts(); i++)
306 SWFFONT * font = t1font2swffont(i);
309 sprintf(filename, "%s.swf", font->name);
310 swf_WriteFont(font, filename);
313 unlink("/tmp/FontDataBase");