Fixed resource loading.
[wine] / tools / fnt2bdf.c
1 /************************************************
2  *
3  * Extract fonts from .fnt or Windows DLL files
4  * and convert them to the .bdf format.
5  *
6  * Copyright 1994-1996 Kevin Carothers and Alex Korobka
7  *
8  */
9
10 #include "config.h"
11
12 #ifdef HAVE_SYS_PARAM_H
13 # include <sys/param.h>
14 #endif
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21
22 #include "windef.h"
23 #include "wingdi.h"
24 #include "winuser.h"
25 #include "fnt2bdf.h"
26 #include "neexe.h"
27 #include "module.h"
28
29 #define MAP_BEG 118
30
31 extern char*   g_lpstrFileName;
32 extern char*   g_lpstrCharSet;
33
34 #define FILE_ERROR      0
35 #define FILE_DLL        1
36 #define FILE_FNT        2
37
38 /* global options */
39
40 char*   g_lpstrFileName = NULL;
41 char*   g_lpstrCharSet = NULL;
42 char*   g_lpstrInputFile = NULL;
43 int     g_outputPoints = 0;
44
45 static char*    errorDLLRead = "Unable to read Windows DLL.\n";
46 static char*    errorFNTRead = "Unable to read .FNT file.\n";
47 static char*    errorOpenFile = "Unable to open file.\n";
48 static char*    errorMemory = "Memory allocation error.\n";
49 static char*    errorFile = "Corrupt or invalid file.\n";
50 static char*    errorFontData = "Unable to parse font data: Error ";
51 static char*    errorEmpty = "No fonts found.\n";
52
53 /* info */
54
55 void usage(void)
56 {
57     printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
58     printf("  -c charset\tcharset name for OEM_CHARSET fonts\n");
59     printf("  -f basename\tbasic output filename\n");
60     printf("  -t \t\toutput files by point size instead of pixel height\n");
61     printf("  input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
62     printf("\nExample:\n  fnt2bdf -c winsys vgasys.fnt\n\n");
63     exit(-1);
64 }
65
66 /* convert big-endian value to the local format */
67
68 int return_data_value(enum data_types dtype, void * pChr)
69 {
70 int   ret_val = 0;
71
72     switch(dtype) {
73         case (dfChar): 
74             ret_val = (int) *(unsigned char *)pChr;
75             break;
76             
77         case(dfShort): 
78             ret_val = *(unsigned char *)pChr;
79             ret_val += (*((unsigned char *)pChr + 1) << 8);
80             break;
81             
82         case(dfLong): {
83             int  i;
84
85             for(i=3; i >= 0; i--)  {
86                 ret_val += *((unsigned char *)pChr + i) << (8*i);
87                 }
88             break;
89             }
90                 case(dfString):
91                         break;
92         } 
93     return ret_val;
94 }
95
96 int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
97 {
98 int     l_nameoffset = return_data_value(dfLong, cpe_font_struct->hdr.dfFace);
99 char*   lpChar;
100
101   if( !g_lpstrFileName )
102   {
103     if( !l_nameoffset || 
104          l_nameoffset > return_data_value(dfLong, cpe_font_struct->hdr.dfSize) + 1 )
105          return ERROR_DATA;
106     lpChar =  (char*)(file_buffer + l_nameoffset);
107   }
108     else lpChar = g_lpstrFileName;
109
110   strcpy( name, lpChar );
111
112   while( (lpChar = strchr( name, ' ')) ) 
113          *lpChar = '-';
114
115   /* construct a filename from the font typeface, slant, weight, and size */
116
117   if( cpe_font_struct->hdr.dfItalic[0] ) strcat(name, "_i" );
118   else strcat(name, "_r" );
119
120   lpChar = name + strlen( name );
121   sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, cpe_font_struct->hdr.dfWeight),
122           (g_outputPoints) ? return_data_value(dfShort, cpe_font_struct->hdr.dfPoints)
123                            : return_data_value(dfShort, cpe_font_struct->hdr.dfPixHeight) );
124   return 0;
125 }
126
127 /* parse FONT resource and write .bdf file */
128
129 int parse_fnt_data(unsigned char* file_buffer, int length)
130 {
131   fnt_fontS     cpe_font_struct; 
132   int           ic=0, t;
133
134   memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
135
136   /* check font header */
137
138   t = return_data_value(dfShort, cpe_font_struct.hdr.dfVersion);
139   if( t != 0x300 && t != 0x200) return ERROR_VERSION;
140
141   t = return_data_value(dfLong, cpe_font_struct.hdr.dfSize);
142   if( t > length ) return ERROR_SIZE;
143   else
144   {     
145     /* set up the charWidth/charOffset  structure pairs (dfCharTable)... */
146
147     int l_fchar = return_data_value(dfChar, cpe_font_struct.hdr.dfFirstChar),
148         l_lchar = return_data_value(dfChar, cpe_font_struct.hdr.dfLastChar); 
149     int l_len   = l_lchar - l_fchar + 1, l_ptr = MAP_BEG;
150
151     /* malloc size = (# chars) * sizeof(WinCharS) */
152
153     if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL) 
154         return ERROR_MEMORY;
155
156     /* NOW, convert them all to UNIX (lton) notation... */
157
158     for(ic=0; ic < l_len; ic++) {
159         cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
160         l_ptr += 2;     /* bump by sizeof(short) */
161
162
163         if( return_data_value(dfShort, cpe_font_struct.hdr.dfVersion) == 0x200) {
164             cpe_font_struct.dfCharTable[ic].charOffset = 
165                         return_data_value(dfShort, &file_buffer[l_ptr]);
166             l_ptr += 2; /* bump by sizeof(long) */
167             }
168         else {  /*  Windows Version 3.0 type font */
169             cpe_font_struct.dfCharTable[ic].charOffset = 
170                         return_data_value(dfLong, &file_buffer[l_ptr]);
171             l_ptr += 4; /* bump by sizeof(long) */
172             }
173         }
174     t = dump_bdf(&cpe_font_struct, file_buffer);
175     free( cpe_font_struct.dfCharTable );
176   }
177   return t;
178 }
179
180 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
181 {
182   FILE*   fp;
183   int     ic;
184   int     l_fchar = return_data_value(dfChar, cpe_font_struct->hdr.dfFirstChar), 
185           l_lchar = return_data_value(dfChar, cpe_font_struct->hdr.dfLastChar); 
186
187   int     l_len = l_lchar-l_fchar + 1,
188           l_hgt = return_data_value(dfChar, cpe_font_struct->hdr.dfPixHeight); 
189   int     l_ascent = return_data_value(dfShort, cpe_font_struct->hdr.dfAscent);
190   char    l_filename[256];
191
192     if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
193         return ic;
194
195     if((fp = fopen(l_filename, "w")) == (FILE *) 0)   
196     {
197       fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
198       return ERROR_FILE;
199     }
200
201     ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
202     if (ic) return (ic);
203
204     /* NOW, convert all chars to UNIX (lton) notation... */
205
206     for(ic=0; ic < l_len; ic++) {
207         int rowidx, l_span,             /* how many char-cols wide is char? */
208             l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
209
210         l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8; 
211
212         fprintf(fp, "STARTCHAR %d  \n", ic);
213         fprintf(fp, "ENCODING %d\n",   l_fchar);
214         fprintf(fp, "SWIDTH    %d    %d \n", 
215                 cpe_font_struct->dfCharTable[ic].charWidth*1000, 
216                 0);
217
218         fprintf(fp, "DWIDTH    %d    %d \n", 
219                 cpe_font_struct->dfCharTable[ic].charWidth, 0);
220
221         fprintf(fp, "BBX  %d  %d  %d   %d\n",
222                 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0, 
223                 l_ascent - l_hgt);
224
225         fprintf(fp, "BITMAP\n");
226         for(rowidx=0; rowidx < l_hgt; rowidx++) {
227             switch(l_span) {
228                 case(0):        /* 1-7 pixels wide font */
229                     {
230                     fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
231                     break;
232                     }
233                 
234                 case(1):        /* 8-15 pixels wide font */
235                     {
236                     fprintf(fp, "%02X%02X", 
237                         (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
238                     fprintf(fp, "\n");
239                     break;
240                     }
241
242                 case(2):        /* 16-23 pixels wide font */
243                     {
244                     fprintf(fp, "%02X%02X%02X", 
245                         file_buffer[l_idx+rowidx],
246                         file_buffer[l_idx+l_hgt+rowidx],
247                         file_buffer[l_idx+(2*l_hgt)+rowidx]);
248                     fprintf(fp, "\n");
249                     break;
250                     }
251
252                 case(3):        /* 24-31 pixels wide font */
253                     {
254                     fprintf(fp, "%02X%02X%02X%02X", 
255                         file_buffer[l_idx+rowidx],
256                         file_buffer[l_idx+l_hgt+rowidx],
257                         file_buffer[l_idx+(2*l_hgt)+rowidx],
258                         file_buffer[l_idx+(3*l_hgt)+rowidx]);
259                     fprintf(fp, "\n");
260                     break;
261                     }
262                 case(4):        /* 32-39 */
263                     {
264                     fprintf(fp, "%02X%02X%02X%02X%02X",
265                         file_buffer[l_idx+rowidx],
266                         file_buffer[l_idx+l_hgt+rowidx],
267                         file_buffer[l_idx+(2*l_hgt)+rowidx],
268                         file_buffer[l_idx+(3*l_hgt)+rowidx],
269                         file_buffer[l_idx+(4*l_hgt)+rowidx]);
270                     fprintf(fp, "\n");
271                     break;
272                     }
273                 default:
274                     fclose(fp);
275                     unlink(l_filename);
276                     return ERROR_DATA;
277                 }
278             }
279         fprintf(fp, "ENDCHAR\n");
280
281         l_fchar++;      /* Go to next one */
282         }
283 fprintf(fp, "ENDFONT\n");
284 fclose(fp);
285 return 0;
286 }
287
288
289 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
290 {
291 int     l_fchar = return_data_value(dfChar, cpe_font_struct->hdr.dfFirstChar), 
292         l_lchar = return_data_value(dfChar, cpe_font_struct->hdr.dfLastChar);
293 int     l_len = l_lchar - l_fchar + 1;
294 int     l_nameoffset = return_data_value(dfLong, cpe_font_struct->hdr.dfFace);
295 int     l_cellheight = return_data_value(dfShort, cpe_font_struct->hdr.dfPixHeight);
296 int     l_ascent = return_data_value(dfShort, cpe_font_struct->hdr.dfAscent);
297
298     fprintf(fs, "STARTFONT   2.1\n");
299
300     /* Compose font name */
301
302     if( l_nameoffset && 
303         l_nameoffset < return_data_value(dfLong, cpe_font_struct->hdr.dfSize) )
304     {
305       int     dpi, point_size;
306       char*   lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
307       short   tmWeight = return_data_value(dfShort, cpe_font_struct->hdr.dfWeight);
308
309       while((lpChar = strchr(lpFace, '-')) ) 
310             *lpChar = ' ';
311
312       fprintf(fs, "FONT -windows-%s-", lpFace );
313
314       if( tmWeight == 0 )                       /* weight */
315           fputs("medium-", fs);
316       else if( tmWeight <= FW_LIGHT )
317           fputs("light-", fs);
318       else if( tmWeight <= FW_MEDIUM )
319           fputs("medium-", fs);
320       else if( tmWeight <= FW_DEMIBOLD )
321           fputs("demibold-", fs);
322       else if( tmWeight <= FW_BOLD )
323           fputs("bold-", fs);
324       else fputs("black-", fs);
325
326       if( cpe_font_struct->hdr.dfItalic[0] )    /* slant */
327           fputs("i-", fs);
328       else fputs("r-", fs);
329
330       /* style */
331
332       if( (cpe_font_struct->hdr.dfPitchAndFamily[0] & 0xF0) == FF_SWISS )
333           fputs("normal-sans-", fs);
334       else fputs("normal--", fs);       /* still can be -sans */
335
336       /* y extents */
337
338       point_size = 10 * return_data_value(dfShort, cpe_font_struct->hdr.dfPoints );
339       dpi = (l_cellheight * 720) / point_size;
340
341       fprintf(fs, "%d-%d-%d-%d-", l_cellheight, 10*l_cellheight, 72, 72);
342                                                 /* point_size, dpi, dpi); */
343
344       /* spacing */
345
346       if( return_data_value(dfShort, cpe_font_struct->hdr.dfPixWidth) ) fputs("c-", fs);
347       else fputs("p-", fs);
348         
349       /* average width */
350
351       fprintf( fs, "%d-", 10 * return_data_value(dfShort, cpe_font_struct->hdr.dfAvgWidth) );
352
353       /* charset */
354
355       switch( cpe_font_struct->hdr.dfCharSet[0] )
356       {
357         /* Microsoft just had to invent its own charsets! */
358
359         case ANSI_CHARSET:      fputs("microsoft-cp1252\n", fs); break;
360         case GREEK_CHARSET:     fputs("microsoft-cp1253\n", fs); break;
361         case TURKISH_CHARSET:   fputs("microsoft-cp1254\n", fs); break;
362         case HEBREW_CHARSET:    fputs("microsoft-cp1255\n", fs); break;
363         case ARABIC_CHARSET:    fputs("microsoft-cp1256\n", fs); break;
364         case BALTIC_CHARSET:    fputs("microsoft-cp1257\n", fs); break;
365         case RUSSIAN_CHARSET:   fputs("microsoft-cp1251\n", fs); break;
366         case EE_CHARSET:        fputs("microsoft-cp1250\n", fs); break; 
367         case SYMBOL_CHARSET:    fputs("microsoft-symbol\n", fs); break;
368         case SHIFTJIS_CHARSET:  fputs("jisx0208.1983-0\n", fs); break;
369         case DEFAULT_CHARSET:   fputs("iso8859-1\n", fs); break;
370
371         default:
372         case OEM_CHARSET: 
373                 if( !g_lpstrCharSet ) 
374                 { 
375                     fputs("Undefined charset, use -c option.\n", stderr); 
376                     return ERROR_DATA; 
377                 }
378                 fprintf(fs, "%s\n", g_lpstrCharSet);
379       }
380     }
381     else return ERROR_DATA;
382
383     fprintf(fs, "SIZE  %d  %d   %d\n",  
384         l_cellheight,
385         return_data_value(dfShort, cpe_font_struct->hdr.dfHorizRes),
386         return_data_value(dfShort, cpe_font_struct->hdr.dfVertRes));   /* dfVertRes[2] */
387
388     fprintf(fs, "FONTBOUNDINGBOX %d  %d  %d  %d\n",
389         return_data_value(dfShort, cpe_font_struct->hdr.dfMaxWidth),
390         return_data_value(dfChar, cpe_font_struct->hdr.dfPixHeight),
391         0, l_ascent - l_cellheight );
392
393     fprintf(fs, "STARTPROPERTIES  4\n");
394
395     fprintf(fs, "FONT_ASCENT %d\n", l_ascent );                       /*  dfAscent[2] */
396     fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
397     fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
398                                    return_data_value(dfShort, cpe_font_struct->hdr.dfInternalLeading));
399     fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, cpe_font_struct->hdr.dfDefaultChar));
400
401     fprintf(fs, "ENDPROPERTIES\n");
402
403     fprintf(fs, "CHARS  %d\n",  l_len);
404     return 0;
405 }
406
407
408
409 void parse_options(int argc, char **argv)
410 {
411   int i;
412
413   switch( argc )
414   {
415     case 2:
416          g_lpstrInputFile = argv[1];
417          break;
418
419     case 3:
420     case 4:
421     case 5:
422     case 6:
423     case 7:
424     case 8:
425          for( i = 1; i < argc - 1; i++ )
426          {
427            if( argv[i][0] != '-' ||
428                strlen(argv[i]) != 2 ) break;
429
430            if( argv[i][1] == 'c' ) 
431                g_lpstrCharSet = argv[i+1];
432            else 
433            if( argv[i][1] == 'f' ) 
434                g_lpstrFileName = argv[i+1];
435            else
436            if( argv[i][1] == 't' )
437            {
438               g_outputPoints = 1;
439               continue;
440            }
441            else
442            usage();
443
444            i++;
445          } 
446          if( i == argc - 1 )
447          {
448            g_lpstrInputFile = argv[i];
449            break;
450          }
451     default: usage();
452   }
453     
454 }
455
456 /* read file data and return file type */
457
458 int get_resource_table(int fd, unsigned char** lpdata, int fsize)
459 {
460   IMAGE_DOS_HEADER mz_header;
461   IMAGE_OS2_HEADER ne_header;
462   short              s, offset, size, retval;
463
464  
465   lseek( fd, 0, SEEK_SET );
466
467   if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) ) 
468       return FILE_ERROR;
469
470   s = return_data_value(dfShort, &mz_header.e_magic);
471
472   if( s == IMAGE_DOS_SIGNATURE)         /* looks like .dll file so far... */
473   {
474     s = return_data_value(dfShort, &mz_header.e_lfanew);
475     lseek( fd, s, SEEK_SET );
476
477     if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
478         return FILE_ERROR;
479
480     s = return_data_value(dfShort, &ne_header.ne_magic);
481
482     if( s == IMAGE_NT_SIGNATURE)
483     {
484        fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
485        return FILE_ERROR; 
486     }
487     else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
488
489     s = return_data_value(dfShort, &ne_header.ne_rsrctab);
490     size = return_data_value(dfShort, &ne_header.ne_restab);
491
492     if( size > fsize ) return FILE_ERROR;
493
494     size -= s;
495     offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
496
497     if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
498     retval = FILE_DLL;
499   }
500   else if( s == 0x300 || s == 0x200 )           /* maybe .fnt ? */
501   {
502     size = return_data_value(dfLong, (char*)&mz_header+2);
503
504     if( size != fsize ) return FILE_ERROR;
505     offset  = 0;
506     retval = FILE_FNT; 
507   }
508   else return FILE_ERROR;
509
510   *lpdata = (unsigned char*)malloc(size);
511
512   if( *lpdata )
513   {
514     lseek( fd, offset, SEEK_SET );
515     if( read(fd, *lpdata, size) != size )
516            { free( *lpdata ); *lpdata = NULL; }
517   }
518   return retval;
519 }
520
521
522 /* entry point */
523
524 int main(int argc, char **argv)
525 {
526   unsigned char* lpdata = NULL;
527   int            fd;
528
529   parse_options( argc, argv);
530
531   if( (fd = open( g_lpstrInputFile, O_RDONLY)) ) 
532   {
533     int    i;
534     struct stat file_stat;
535
536     fstat( fd, &file_stat);
537     i = get_resource_table( fd, &lpdata, file_stat.st_size );
538
539     switch(i)
540     {
541         case FILE_DLL:
542              if( lpdata )
543              {
544                int          j, count = 0;
545                NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
546                NE_NAMEINFO* pFontStorage = NULL;
547
548                while( (i = return_data_value(dfShort, &pTInfo->type_id)) ) 
549                {
550                   j = return_data_value(dfShort, &pTInfo->count);
551                   if( i == NE_RSCTYPE_FONT )
552                   {
553                     count = j;
554                     pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
555                   }
556
557                   pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
558                }
559                if( pFontStorage && count )
560                {
561                  unsigned short         size_shift = return_data_value(dfShort, lpdata);
562                  unsigned char*         lpfont = NULL;
563                  unsigned               offset;
564                  unsigned               length;
565
566                  for( j = 0; j < count; j++, pFontStorage++ )
567                  {
568                     length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
569                     offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
570                     
571                     if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
572                     {
573                         fprintf(stderr, errorMemory );
574                         exit(1);
575                     }
576
577                     lseek( fd, offset, SEEK_SET );
578                     if( read(fd, lpfont, length) != length )
579                     {
580                         fprintf(stderr, errorDLLRead );
581                         exit(1);
582                     }
583
584                     if( (i = parse_fnt_data( lpfont, length )) )
585                     {
586                         fprintf(stderr, "%s%d\n", errorFontData, i );
587                         exit(1);
588                     }
589                  }
590                  free(lpfont); free(lpdata);
591                  exit(0);
592                }
593                else
594                {
595                  fprintf(stderr, errorEmpty );
596                  exit(1);
597                }
598                free( lpdata );
599              }
600              else
601              {
602                fprintf(stderr, errorDLLRead);
603                exit(1);
604              }
605              break;
606
607         case FILE_FNT:
608              if( lpdata )
609              {
610                if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
611                {
612                    fprintf(stderr, "%s%d\n", errorFontData, i );
613                    exit(1);
614                }
615                free( lpdata );
616              }
617              else
618              {
619                fprintf(stderr, errorFNTRead);
620                exit(1);
621              }
622              break;
623
624         case FILE_ERROR:
625              fprintf(stderr, errorFile );
626              exit(1);
627     }
628     close(fd);
629     exit(0);
630   }
631   else
632   {
633     fprintf(stderr, errorOpenFile );
634     exit(1);
635   }
636 }