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