fix advance values.
[swftools.git] / lib / example / makefonts.cc
1 /* makefonts.cc
2
3    Utility for generating the standard fonts (arial, courier, etc.) in swf-format. 
4    
5    Part of the swftools package.
6
7    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8  
9    This file is distributed under the GPL, see file COPYING for details 
10
11 */
12
13
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <math.h>
17 extern "C" {
18 #include "../rfxswf.h"
19 }
20 #include "../../pdf2swf/swfoutput.h"
21 #include "../../pdf2swf/spline.h"
22
23 #define standardEncodingSize 256
24 #define symbolEncodingSize 256
25 #define zapfDingbatsEncodingSize 256
26 #define macRomanEncodingSize 256
27
28 extern char *standardEncoding[standardEncodingSize];
29 extern char *symbolEncoding[symbolEncodingSize];
30 extern char *zapfDingbatsEncoding[zapfDingbatsEncodingSize];
31 extern char *macRomanEncoding[macRomanEncodingSize];
32
33 char*DATADIR = "/usr/local/share/swftools";
34
35 extern void drawpath(TAG*tag, T1_OUTLINE*outline, struct swfmatrix*m, int log);
36 extern void resetdrawer();
37 extern void moveto(TAG*tag, plotxy p0);
38 extern void lineto(TAG*tag, plotxy p0);
39
40 SWFFONT * t1font2swffont(int i)
41 {
42     T1_LoadFont(i);
43
44     float angle = T1_GetItalicAngle(i);
45     char*fontname = T1_GetFontName(i);
46     char*fullname = T1_GetFullName(i);
47     char*familyname = T1_GetFamilyName(i);
48     float underline = T1_GetUnderlinePosition(i);
49     BBox bbox = T1_GetFontBBox(i);
50
51     /* if "all" is given, translate the font names in something more
52        readable */
53     if(!strcmp(fullname, "Nimbus Roman No9 L Regular")) fontname = "Helvetica";
54     if(!strcmp(fullname, "Nimbus Roman No9 L Regular Italic")) fontname = "HelveticaItalic";
55     if(!strcmp(fullname, "Nimbus Roman No9 L Medium")) fontname = "HelveticaBold";
56     if(!strcmp(fullname, "Nimbus Roman No9 L Medium Italic")) fontname = "HelveticaBoldItalic";
57     if(!strcmp(fullname, "Nimbus Sans L Regular")) fontname = "Times";
58     if(!strcmp(fullname, "Nimbus Sans L Regular Italic")) fontname = "TimesItalic";
59     if(!strcmp(fullname, "Nimbus Sans L Bold")) fontname = "TimesBold";
60     if(!strcmp(fullname, "Nimbus Sans L Bold Italic")) fontname = "TimesBoldItalic";
61     if(!strcmp(fullname, "Nimbus Mono L Regular")) fontname = "Courier";
62     if(!strcmp(fullname, "Nimbus Mono L Regular Oblique")) fontname = "CourierItalic";
63     if(!strcmp(fullname, "Nimbus Mono L Bold")) fontname = "CourierBold";
64     if(!strcmp(fullname, "Nimbus Mono L Bold Oblique")) fontname = "CourierBoldItalic";
65     if(!strcmp(fullname, "Standard Symbols L")) fontname = "Symbol";
66
67     char ** encoding = standardEncoding;
68     int encodingsize = standardEncodingSize;
69
70     printf("processing \"%s\" (\"%s\")...\n", fullname, fontname);
71
72     if(strstr(fullname, "Dingbats")) {// Zapf Dingbats
73         encoding = zapfDingbatsEncoding;
74         encodingsize = zapfDingbatsEncodingSize;
75     }
76     else if(strstr(fullname, "Symbol")) {// Symbol
77         encoding = symbolEncoding;
78         encodingsize = symbolEncodingSize;
79     }
80
81     SWFFONT * wfont = (SWFFONT*)malloc(sizeof(SWFFONT));
82     SWFFont * font = new SWFFont("", i, "");
83
84     wfont->version = 2;
85     wfont->name = (U8*)strdup(fontname);
86     wfont->layout = (SWFLAYOUT*)malloc(sizeof(SWFLAYOUT));
87     memset(wfont->layout, 0, sizeof(SWFLAYOUT));
88
89     int s,num;
90     num = 0;
91     for(s=0;s<encodingsize;s++)
92     {
93         if(encoding[s]) {
94             T1_OUTLINE*outline = font->getOutline(encoding[s]);
95             if(outline && outline->link)
96                 num++;
97         }
98     }
99
100     wfont->maxascii = encodingsize;
101     wfont->numchars = num;
102     
103     wfont->style = (/*bold*/0?FONT_STYLE_BOLD:0) + (angle>0.05?FONT_STYLE_ITALIC:0);
104
105     wfont->glyph = (SWFGLYPH*)malloc(num*sizeof(SWFGLYPH));
106     memset(wfont->glyph, 0, num*sizeof(SWFGLYPH));
107     wfont->glyph2ascii = (U16*)malloc(num*sizeof(U16));
108     memset(wfont->glyph2ascii, 0, num*sizeof(U16));
109     wfont->ascii2glyph = (int*)malloc(encodingsize*sizeof(int));
110     memset(wfont->ascii2glyph, -1, encodingsize*sizeof(int));
111     wfont->layout->ascent = (U16)(underline - bbox.lly);
112     wfont->layout->descent = (U16)(bbox.ury - underline);
113     wfont->layout->leading = (U16)(wfont->layout->ascent - 
114                              wfont->layout->descent -
115                              (bbox.lly - bbox.ury));
116     wfont->layout->bounds = (SRECT*)malloc(sizeof(SRECT)*num);
117     memset(wfont->layout->bounds, 0, sizeof(SRECT)*num);
118     wfont->layout->kerningcount = 0;
119     wfont->layout->kerning = 0;
120   
121     num = 0;
122     for(s=0;s<encodingsize;s++)
123     {
124         if(encoding[s]) {
125             T1_OUTLINE*outline = font->getOutline(encoding[s]);
126             int width = font->getWidth(encoding[s]);
127             if(outline && outline->link) {
128                 int log = 0;
129                 wfont->ascii2glyph[s] = num;
130                 wfont->glyph2ascii[num] = s;
131                 swf_ShapeNew(&wfont->glyph[num].shape);
132                 SHAPE*shape = wfont->glyph[num].shape;
133                 wfont->glyph[num].advance = (int)(width/6.4); // 128/20
134                 
135                 TAG*tag = swf_InsertTag(0,ST_DEFINESHAPE);
136
137                 swfmatrix m;
138                 m.m11 = 1.0;
139                 m.m22 = 1.0;
140                 m.m21 = 0;
141                 m.m12 = 0;
142                 m.m13 = 0;
143                 m.m23 = 0;
144
145                 swf_SetU8(tag,0);
146
147                 shape->bits.fill = 1;
148                 shape->bits.line = 0;
149                 swf_ShapeSetStyle(tag,shape,0,1,0);
150                 resetdrawer();
151                 drawpath(tag, outline, &m, log);
152                 
153                 /*uncomment this to mark the glyph sizes:
154                 plotxy p1,p2; p1.x=0; p1.y=0; p2.x=width/8; p2.y=-width/8;
155                 moveto(tag, p1); lineto(tag, p2); p1.x += 2; p2.x += 2;
156                 lineto(tag, p2); lineto(tag, p1); p1.x -= 2; lineto(tag, p1);// */
157
158                 swf_ShapeSetEnd(tag);
159
160                 wfont->glyph[num].shape->bitlen = (tag->len-1)*8;
161                 wfont->glyph[num].shape->data = (U8*)malloc(tag->len-1);
162                 memcpy(wfont->glyph[num].shape->data, &tag->data[1], tag->len-1);
163                 swf_DeleteTag(tag);
164                     
165                 /* fix bounding box */
166                 SHAPE2*shape2;
167                 SRECT bbox;
168                 shape2 = swf_ShapeToShape2(shape);
169                 if(!shape2) { fprintf(stderr, "Shape parse error\n");exit(1);}
170                 bbox = swf_GetShapeBoundingBox(shape2->lines);
171                 swf_Shape2Free(shape2);
172                 wfont->layout->bounds[num] = bbox;
173                 
174                 num++;
175             }
176         }
177     }
178     return wfont;
179 }
180
181 int main(int argc, char ** argv)
182 {
183   int all=0;
184   if(argc<=1) {
185       printf("Usage: %s font.afm\n", argv[0]);
186       printf("OR:    %s all\n", argv[0]);
187       printf("\n");
188       printf("\tIf \"all\" is given instead of font names, all standard fonts\n");
189       printf("\t(Courier, Arial etc.) will be created\n");
190       return 0;
191   } else {
192       if(!strcmp(argv[1],"all"))
193           all=1;
194   }
195   //TODO: use tempnam here. Check if environment already contains a
196   //T1LIB_CONFIG.
197   putenv( "T1LIB_CONFIG=/tmp/t1lib.config.tmp");
198   FILE*fi = fopen("/tmp/t1lib.config.tmp", "wb");
199   fprintf(fi, "FONTDATABASE=/tmp/FontDataBase\n", DATADIR);
200   fprintf(fi, "ENCODING=%s/fonts:.\n", DATADIR);
201   fprintf(fi, "AFM=%s/fonts:.\n", DATADIR);
202   fprintf(fi, "TYPE1=%s/fonts:.\n", DATADIR);
203   fclose(fi);
204   fi = fopen("/tmp/FontDataBase", "wb");
205   if(all) {
206       fprintf(fi, "14\n");             
207       fprintf(fi, "n021003l.afm\n"); //fixme
208       fprintf(fi, "n021023l.afm\n");
209       fprintf(fi, "n021004l.afm\n");
210       fprintf(fi, "n021024l.afm\n");
211       fprintf(fi, "n019003l.afm\n");
212       fprintf(fi, "n019023l.afm\n");
213       fprintf(fi, "n019004l.afm\n");
214       fprintf(fi, "n019024l.afm\n");
215       fprintf(fi, "n022003l.afm\n");
216       fprintf(fi, "n022023l.afm\n");
217       fprintf(fi, "n022004l.afm\n");
218       fprintf(fi, "n022024l.afm\n");
219       fprintf(fi, "s050000l.afm\n");
220       fprintf(fi, "d050000l.afm\n");
221   } else {
222     fprintf(fi, "%d\n",argc-1);
223     int t;
224     for(t=1;t<argc;t++) {
225         fprintf(fi, "%s\n",argv[t]);
226     }
227   }
228   fclose(fi);
229   /* initialize t1lib */
230   T1_SetBitmapPad( 16);
231   if ((T1_InitLib(NO_LOGFILE)==NULL)){
232       fprintf(stderr, "Initialization of t1lib failed\n");
233       exit(1);
234   }
235   unlink("/tmp/t1lib.config.tmp");
236
237   int i,num;
238   for( i=0; i<T1_Get_no_fonts(); i++)
239 //      i = 4;
240   {
241     SWFFONT * font = t1font2swffont(i);
242     
243     char filename[128];
244     sprintf(filename, "%s.swf", font->name);
245     swf_WriteFont(font, filename);
246     swf_FontFree(font);
247   }
248 }
249
250