96520e98fa3d5b0b9e5eaba16aa4de93a8c74301
[swftools.git] / pdf2swf / font2swf.cc
1 /* makefonts.cc
2
3    Utility for converting Type 1 fonts to SWF.
4    
5    Part of the swftools package.
6
7    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8  
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <math.h>
27 #include "../lib/rfxswf.h"
28 #include "../lib/args.h"
29 #include "swfoutput.h"
30 #include "spline.h"
31 #include "iso_encodings.c"
32
33 static char * filenames[256];
34 static int filenum;
35 static char * destfilename = "output.swf";
36 int all=0;
37 int verbose=0;
38
39 struct options_t options[] =
40 {
41  {"o","output"},
42  {"v","verbose"},
43  {"V","version"},
44  {0,0}
45 };
46 int args_callback_option(char*name,char*val)
47 {
48     if(!strcmp(name, "V")) {
49         printf("font2swf - part of %s %s\n", PACKAGE, VERSION);
50         exit(0);
51     }
52     else if(!strcmp(name, "o")) {
53         destfilename = val;
54         return 1;
55     }
56     else if(!strcmp(name, "v")) {
57         verbose ++;
58         return 0;
59     }
60     else {
61         printf("Unknown option: -%s\n", name);
62         exit(1);
63     }
64     return 0;
65 }
66 int args_callback_longoption(char*name,char*val)
67 {
68     return args_long2shortoption(options, name, val);
69 }
70 void args_callback_usage(char*name)
71 {    
72     printf("Usage: %s file.afm [...]\n", name);
73     printf("   OR: %s all\n", name);
74     printf("\n");
75     printf("\tIf \"all\" is given instead of font names, all standard fonts\n");
76     printf("\t(Courier, Arial etc.) will be created\n");
77     printf("\n");
78     printf("Options:\n");
79     printf("\n");
80     printf("\t-h , --help\t\t Print help and exit\n");
81     printf("\t-o , --output filename\t set output filename\n");
82     printf("\t-v , --verbose\t\t Be more verbose\n");
83     printf("\t-V , --version\t\t Print program version and exit\n");
84 }
85 int args_callback_command(char*name,char*val)
86 {
87     if(!strcmp(name, "all"))
88         all = 1;
89     else {
90         filenames[filenum++] = strdup(name);
91     }
92     return 0;
93 }
94
95
96 #define standardEncodingSize 256
97 #define symbolEncodingSize 256
98 #define zapfDingbatsEncodingSize 256
99 #define macRomanEncodingSize 256
100 #define winAnsiEncodingSize 256
101
102 extern char *winAnsiEncoding[winAnsiEncodingSize];
103 extern char *standardEncoding[standardEncodingSize];
104 extern char *symbolEncoding[symbolEncodingSize];
105 extern char *zapfDingbatsEncoding[zapfDingbatsEncodingSize];
106 extern char *macRomanEncoding[macRomanEncodingSize];
107
108 extern void drawpath(TAG*tag, T1_OUTLINE*outline, struct swfmatrix*m, int log);
109 extern void resetdrawer();
110 extern void moveto(TAG*tag, plotxy p0);
111 extern void lineto(TAG*tag, plotxy p0);
112
113 SWFFONT * t1font2swffont(int i)
114 {
115     T1_LoadFont(i);
116
117     float angle = T1_GetItalicAngle(i);
118     char*fontname = T1_GetFontName(i);
119     char*fullname = T1_GetFullName(i);
120     char*familyname = T1_GetFamilyName(i);
121     float underline = T1_GetUnderlinePosition(i);
122     BBox bbox = T1_GetFontBBox(i);
123
124     /* if "all" is given, translate the font names in something more
125        readable */
126     if(!strcmp(fullname, "Nimbus Roman No9 L Regular")) fontname = "Helvetica";
127     if(!strcmp(fullname, "Nimbus Roman No9 L Regular Italic")) fontname = "HelveticaItalic";
128     if(!strcmp(fullname, "Nimbus Roman No9 L Medium")) fontname = "HelveticaBold";
129     if(!strcmp(fullname, "Nimbus Roman No9 L Medium Italic")) fontname = "HelveticaBoldItalic";
130     if(!strcmp(fullname, "Nimbus Sans L Regular")) fontname = "Times";
131     if(!strcmp(fullname, "Nimbus Sans L Regular Italic")) fontname = "TimesItalic";
132     if(!strcmp(fullname, "Nimbus Sans L Bold")) fontname = "TimesBold";
133     if(!strcmp(fullname, "Nimbus Sans L Bold Italic")) fontname = "TimesBoldItalic";
134     if(!strcmp(fullname, "Nimbus Mono L Regular")) fontname = "Courier";
135     if(!strcmp(fullname, "Nimbus Mono L Regular Oblique")) fontname = "CourierItalic";
136     if(!strcmp(fullname, "Nimbus Mono L Bold")) fontname = "CourierBold";
137     if(!strcmp(fullname, "Nimbus Mono L Bold Oblique")) fontname = "CourierBoldItalic";
138     if(!strcmp(fullname, "Standard Symbols L")) fontname = "Symbol";
139
140     char ** encoding = winAnsiEncoding;
141     int encodingsize = winAnsiEncodingSize;
142
143     printf("processing \"%s\" (\"%s\")...\n", fullname, fontname);
144
145     if(strstr(fullname, "Dingbats")) {// Zapf Dingbats
146         encoding = zapfDingbatsEncoding;
147         encodingsize = zapfDingbatsEncodingSize;
148     }
149     else if(strstr(fullname, "Symbol")) {// Symbol
150         encoding = symbolEncoding;
151         encodingsize = symbolEncodingSize;
152     }
153
154     SWFFONT * wfont = (SWFFONT*)malloc(sizeof(SWFFONT));
155     SWFFont * font = new SWFFont("", i, "");
156
157     memset(wfont, 0, sizeof(SWFFONT));
158
159     wfont->version = 2;
160     wfont->name = (U8*)strdup(fontname);
161     wfont->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
162     memset(wfont->layout, 0, sizeof(SWFLAYOUT));
163
164     int s,num;
165     num = 0;
166     for(s=0;s<encodingsize;s++)
167     {
168         char charinfont=0;
169         if(encoding[s]) {
170             T1_OUTLINE*outline = font->getOutline(encoding[s], 0);
171             if(outline) {num++;charinfont=1;}
172         }
173         /*if(charinfont) printf("x");
174         else printf(".");
175         if((s&15) == 15)
176             printf("\n");*/
177     }
178
179     wfont->maxascii = encodingsize;
180     wfont->numchars = num;
181     
182     wfont->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
183
184     wfont->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
185     memset(wfont->glyph, 0, num*sizeof(SWFGLYPH));
186     wfont->glyph2ascii = (U16*)malloc(num*sizeof(U16));
187     memset(wfont->glyph2ascii, 0, num*sizeof(U16));
188     wfont->ascii2glyph = (int*)malloc(encodingsize*sizeof(int));
189     memset(wfont->ascii2glyph, -1, encodingsize*sizeof(int));
190     wfont->layout->ascent = (U16)(underline - bbox.lly);
191     wfont->layout->descent = (U16)(bbox.ury - underline);
192     wfont->layout->leading = (U16)(wfont->layout->ascent - 
193                              wfont->layout->descent -
194                              (bbox.lly - bbox.ury));
195     wfont->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
196     memset(wfont->layout->bounds, 0, sizeof(SRECT)*num);
197     wfont->layout->kerningcount = 0;
198     wfont->layout->kerning = 0;
199   
200     num = 0;
201     for(s=0;s<encodingsize;s++)
202     {
203         if(encoding[s]) {
204             T1_OUTLINE*outline = font->getOutline(encoding[s],0);
205             int width = font->getWidth(encoding[s]);
206
207             if(outline) {
208                 int log = 0;
209                 wfont->ascii2glyph[s] = num;
210                 wfont->glyph2ascii[num] = s;
211                 swf_ShapeNew(&wfont->glyph[num].shape);
212                 SHAPE*shape = wfont->glyph[num].shape;
213                 int firstx = outline->dest.x/0xffff;
214                 
215                 TAG*tag = swf_InsertTag(0,ST_DEFINESHAPE);
216
217                 swfmatrix m;
218                 m.m11 = 1.0;
219                 m.m22 = 1.0;
220                 m.m21 = 0;
221                 m.m12 = 0;
222                 m.m13 = 0;
223                 m.m23 = 0;
224
225                 swf_SetU8(tag,0);
226
227                 shape->bits.fill = 1;
228                 shape->bits.line = 0;
229                 swf_ShapeSetStyle(tag,shape,0,1,0);
230                 resetdrawer();
231
232                 drawpath(tag, outline, &m, log);
233
234                 /*uncomment this to mark the glyph sizes:
235                 plotxy p1,p2; p1.x=0; p1.y=0; p2.x=width/8; p2.y=-width/8;
236                 moveto(tag, p1); lineto(tag, p2); p1.x += 2; p2.x += 2;
237                 lineto(tag, p2); lineto(tag, p1); p1.x -= 2; lineto(tag, p1);// */
238
239                 swf_ShapeSetEnd(tag);
240
241                 wfont->glyph[num].shape->bitlen = (tag->len-1)*8;
242                 wfont->glyph[num].shape->data = (U8*)malloc(tag->len-1);
243                 memcpy(wfont->glyph[num].shape->data, &tag->data[1], tag->len-1);
244                 swf_DeleteTag(tag);
245
246                 /* fix bounding box */
247                 SHAPE2*shape2;
248                 SRECT bbox;
249                 shape2 = swf_ShapeToShape2(shape);
250                 if(!shape2) { fprintf(stderr, "Shape parse error\n");exit(1);}
251                 bbox = swf_GetShapeBoundingBox(shape2);
252                 swf_Shape2Free(shape2);
253                 wfont->layout->bounds[num] = bbox;
254                 //wfont->glyph[num].advance = (int)(width/6.4); // 128/20
255                 wfont->glyph[num].advance = bbox.xmax/20;
256                 if(!wfont->glyph[num].advance) {
257                     wfont->glyph[num].advance = firstx;
258                 }
259                 
260                 num++;
261             }
262         }
263     }
264     return wfont;
265 }
266
267 int main(int argc, char ** argv)
268 {
269   char cwd[128];
270   getcwd(cwd, 128);
271   processargs(argc, argv);
272   if(!all && !filenum) {
273       fprintf(stderr, "You must supply a filename.\n");
274       exit(1);
275   }
276   //TODO: use tempnam here. Check if environment already contains a
277   //T1LIB_CONFIG.
278   putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
279   FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
280   if(all) {
281       fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
282       fprintf(fi, "ENCODING=%s/fonts:.\n", SWFTOOLS_DATADIR);
283       fprintf(fi, "AFM=%s/fonts:.\n", SWFTOOLS_DATADIR);
284       fprintf(fi, "TYPE1=%s/fonts:.\n", SWFTOOLS_DATADIR);
285   } else {
286       fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n");
287       fprintf(fi, "ENCODING=%s:.\n", cwd);
288       fprintf(fi, "AFM=%s:.\n", cwd);
289       fprintf(fi, "TYPE1=%s:.\n", cwd);
290   }
291   fclose(fi);
292   fi = fopen("/tmp/FontDataBase", "wb");
293   if(all) {
294       fprintf(fi, "14\n");             
295       fprintf(fi, "%s/fonts/n021003l.afm\n", SWFTOOLS_DATADIR);
296       fprintf(fi, "%s/fonts/n021023l.afm\n", SWFTOOLS_DATADIR);
297       fprintf(fi, "%s/fonts/n021004l.afm\n", SWFTOOLS_DATADIR);
298       fprintf(fi, "%s/fonts/n021024l.afm\n", SWFTOOLS_DATADIR);
299       fprintf(fi, "%s/fonts/n019003l.afm\n", SWFTOOLS_DATADIR);
300       fprintf(fi, "%s/fonts/n019023l.afm\n", SWFTOOLS_DATADIR);
301       fprintf(fi, "%s/fonts/n019004l.afm\n", SWFTOOLS_DATADIR);
302       fprintf(fi, "%s/fonts/n019024l.afm\n", SWFTOOLS_DATADIR);
303       fprintf(fi, "%s/fonts/n022003l.afm\n", SWFTOOLS_DATADIR);
304       fprintf(fi, "%s/fonts/n022023l.afm\n", SWFTOOLS_DATADIR);
305       fprintf(fi, "%s/fonts/n022004l.afm\n", SWFTOOLS_DATADIR);
306       fprintf(fi, "%s/fonts/n022024l.afm\n", SWFTOOLS_DATADIR);
307       fprintf(fi, "%s/fonts/s050000l.afm\n", SWFTOOLS_DATADIR);
308       fprintf(fi, "%s/fonts/d050000l.afm\n", SWFTOOLS_DATADIR);
309   } else {
310     fprintf(fi, "%d\n", filenum);
311     int t;
312     for(t=0;t<filenum;t++) {
313         fprintf(fi, "%s/%s\n", cwd, filenames[t]);
314     }
315   }
316   fclose(fi);
317   /* initialize t1lib */
318   T1_SetBitmapPad( 16);
319   if ((T1_InitLib(NO_LOGFILE)==NULL)){
320       fprintf(stderr, "Initialization of t1lib failed\n");
321       exit(1);
322   }
323   unlink("/tmp/t1lib.config.tmp");
324
325   int i,num;
326   for( i=0; i<T1_Get_no_fonts(); i++)
327   {
328     SWFFONT * font = t1font2swffont(i);
329     
330     char filename[128];
331     sprintf(filename, "%s.swf", font->name);
332     swf_WriteFont(font, filename);
333     swf_FontFree(font);
334   }
335   unlink("/tmp/FontDataBase");
336 }
337
338