Don't crash on close from window manager.
[wine.git] / tools / fnt2bdf.c
blobcb854e9004364811528862800cb6d77e3935806a
1 /************************************************
3 * Extract fonts from .fnt or Windows DLL files
4 * and convert them to the .bdf format.
6 * Copyright 1994-1996 Kevin Carothers and Alex Korobka
8 */
10 #include "config.h"
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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <fcntl.h>
23 #include "fnt2bdf.h"
24 #include "module.h"
26 #define FILE_ERROR 0
27 #define FILE_DLL 1
28 #define FILE_FNT 2
30 /* global options */
32 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
33 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
35 char* g_lpstrFileName = NULL;
36 char* g_lpstrCharSet = NULL;
37 char* g_lpstrInputFile = NULL;
38 int g_outputPoints = 0;
40 static char* errorDLLRead = "Unable to read Windows DLL.\n";
41 static char* errorFNTRead = "Unable to read .FNT file.\n";
42 static char* errorOpenFile = "Unable to open file.\n";
43 static char* errorMemory = "Memory allocation error.\n";
44 static char* errorFile = "Corrupt or invalid file.\n";
45 static char* errorFontData = "Unable to parse font data: Error ";
46 static char* errorEmpty = "No fonts found.\n";
48 /* info */
50 void usage(void)
52 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
53 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
54 printf(" -f basename\tbasic output filename\n");
55 printf(" -t \t\toutput files by point size instead of pixel height\n");
56 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
57 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
58 exit(-1);
61 /* convert little-endian value to the local format */
63 int return_data_value(enum data_types dtype, void * pChr)
65 int ret_val = 0;
67 switch(dtype) {
68 case (dfChar):
69 ret_val = (int) *(unsigned char *)pChr;
70 break;
72 case(dfShort):
73 ret_val = *(unsigned char *)pChr;
74 ret_val += (*((unsigned char *)pChr + 1) << 8);
75 break;
77 case(dfLong): {
78 int i;
80 for(i=3; i >= 0; i--) {
81 ret_val += *((unsigned char *)pChr + i) << (8*i);
83 break;
85 case(dfString):
86 break;
88 return ret_val;
91 int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
93 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
94 char* lpChar;
96 if( !g_lpstrFileName )
98 if( !l_nameoffset ||
99 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
100 return ERROR_DATA;
101 lpChar = (char*)(file_buffer + l_nameoffset);
103 else lpChar = g_lpstrFileName;
105 strcpy( name, lpChar );
107 while( (lpChar = strchr( name, ' ')) )
108 *lpChar = '-';
110 /* construct a filename from the font typeface, slant, weight, and size */
112 if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
113 else strcat(name, "_r" );
115 lpChar = name + strlen( name );
116 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight),
117 (g_outputPoints) ? return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints)
118 : return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight) );
119 return 0;
122 /* parse FONT resource and write .bdf file */
124 int parse_fnt_data(unsigned char* file_buffer, int length)
126 fnt_fontS cpe_font_struct;
127 int ic=0, t;
129 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
131 /* check font header */
133 t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
134 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
136 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
137 if( t > length ) return ERROR_SIZE;
138 else
140 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
142 int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar),
143 l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar);
144 int l_len = l_lchar - l_fchar + 1;
145 int l_ptr = sizeof(fnt_hdrS);
147 /* some fields were introduced for Windows 3.x fonts */
148 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200 )
149 l_ptr -= 30;
151 /* malloc size = (# chars) * sizeof(WinCharS) */
153 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
154 return ERROR_MEMORY;
156 /* NOW, convert them all to UNIX (lton) notation... */
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) */
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) */
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) */
174 t = dump_bdf(&cpe_font_struct, file_buffer);
175 free( cpe_font_struct.dfCharTable );
177 return t;
180 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
182 FILE* fp;
183 int ic;
184 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
185 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
187 int l_len = l_lchar-l_fchar + 1,
188 l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight);
189 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
190 char l_filename[256];
192 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
193 return ic;
195 if((fp = fopen(l_filename, "w")) == (FILE *) 0)
197 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
198 return ERROR_FILE;
201 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
202 if (ic) return (ic);
204 /* NOW, convert all chars to UNIX (lton) notation... */
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;
210 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
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,
218 fprintf(fp, "DWIDTH %d %d \n",
219 cpe_font_struct->dfCharTable[ic].charWidth, 0);
221 fprintf(fp, "BBX %d %d %d %d\n",
222 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
223 l_ascent - l_hgt);
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 */
230 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
231 break;
234 case(1): /* 8-15 pixels wide font */
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;
242 case(2): /* 16-23 pixels wide font */
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;
252 case(3): /* 24-31 pixels wide font */
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;
262 case(4): /* 32-39 */
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;
273 default:
274 fclose(fp);
275 unlink(l_filename);
276 return ERROR_DATA;
279 fprintf(fp, "ENDCHAR\n");
281 l_fchar++; /* Go to next one */
283 fprintf(fp, "ENDFONT\n");
284 fclose(fp);
285 return 0;
289 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
291 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
292 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
293 int l_len = l_lchar - l_fchar + 1;
294 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
295 int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
296 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
298 fprintf(fs, "STARTFONT 2.1\n");
300 /* Compose font name */
302 if( l_nameoffset &&
303 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
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.fi.dfWeight);
309 while((lpChar = strchr(lpFace, '-')) )
310 *lpChar = ' ';
312 fprintf(fs, "FONT -windows-%s-", lpFace );
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);
326 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
327 fputs("i-", fs);
328 else fputs("r-", fs);
330 /* style */
332 if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
333 fputs("normal-sans-", fs);
334 else fputs("normal--", fs); /* still can be -sans */
336 /* y extents */
338 point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
339 dpi = (l_cellheight * 720) / point_size;
341 fprintf(fs, "%d-%d-%d-%d-", l_cellheight, 10*l_cellheight, 72, 72);
342 /* point_size, dpi, dpi); */
344 /* spacing */
346 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
347 else fputs("p-", fs);
349 /* average width */
351 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
353 /* charset */
355 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
356 else
357 switch( cpe_font_struct->hdr.fi.dfCharSet )
359 /* Microsoft just had to invent its own charsets! */
361 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
362 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
363 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
364 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
365 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
366 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
367 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
368 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
369 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
370 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
371 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
373 default:
374 case OEM_CHARSET:
375 fputs("Undefined charset, use -c option.\n", stderr);
376 return ERROR_DATA;
379 else return ERROR_DATA;
381 fprintf(fs, "SIZE %d %d %d\n",
382 l_cellheight,
383 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
384 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
386 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
387 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
388 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
389 0, l_ascent - l_cellheight );
391 fprintf(fs, "STARTPROPERTIES 4\n");
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.fi.dfInternalLeading));
397 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
399 fprintf(fs, "ENDPROPERTIES\n");
401 fprintf(fs, "CHARS %d\n", l_len);
402 return 0;
407 void parse_options(int argc, char **argv)
409 int i;
411 switch( argc )
413 case 2:
414 g_lpstrInputFile = argv[1];
415 break;
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++ )
425 if( argv[i][0] != '-' ||
426 strlen(argv[i]) != 2 ) break;
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' )
436 g_outputPoints = 1;
437 continue;
439 else
440 usage();
442 i++;
444 if( i == argc - 1 )
446 g_lpstrInputFile = argv[i];
447 break;
449 default: usage();
454 /* read file data and return file type */
456 int get_resource_table(int fd, unsigned char** lpdata, int fsize)
458 IMAGE_DOS_HEADER mz_header;
459 IMAGE_OS2_HEADER ne_header;
460 long s, offset, size;
461 int retval;
463 lseek( fd, 0, SEEK_SET );
465 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
466 return FILE_ERROR;
468 s = return_data_value(dfShort, &mz_header.e_magic);
470 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
472 s = return_data_value(dfShort, &mz_header.e_lfanew);
473 lseek( fd, s, SEEK_SET );
475 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
476 return FILE_ERROR;
478 s = return_data_value(dfShort, &ne_header.ne_magic);
480 if( s == IMAGE_NT_SIGNATURE)
482 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
483 return FILE_ERROR;
485 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
487 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
488 size = return_data_value(dfShort, &ne_header.ne_restab);
490 if( size > fsize ) return FILE_ERROR;
492 size -= s;
493 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
495 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
496 retval = FILE_DLL;
498 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
500 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
502 if( size != fsize ) return FILE_ERROR;
503 offset = 0;
504 retval = FILE_FNT;
506 else return FILE_ERROR;
508 *lpdata = (unsigned char*)malloc(size);
510 if( *lpdata )
512 lseek( fd, offset, SEEK_SET );
513 if( read(fd, *lpdata, size) != size )
514 { free( *lpdata ); *lpdata = NULL; }
516 return retval;
520 /* entry point */
522 int main(int argc, char **argv)
524 unsigned char* lpdata = NULL;
525 int fd;
527 parse_options( argc, argv);
529 if( (fd = open( g_lpstrInputFile, O_RDONLY)) )
531 int i;
532 struct stat file_stat;
534 fstat( fd, &file_stat);
535 i = get_resource_table( fd, &lpdata, file_stat.st_size );
537 switch(i)
539 case FILE_DLL:
540 if( lpdata )
542 int j, count = 0;
543 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
544 NE_NAMEINFO* pFontStorage = NULL;
546 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
548 j = return_data_value(dfShort, &pTInfo->count);
549 if( i == NE_RSCTYPE_FONT )
551 count = j;
552 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
553 break; /* found one */
556 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
558 if( pFontStorage && count )
560 unsigned short size_shift = return_data_value(dfShort, lpdata);
561 unsigned char* lpfont = NULL;
562 unsigned offset;
563 unsigned length;
565 for( j = 0; j < count; j++, pFontStorage++ )
567 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
568 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
570 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
572 fprintf(stderr, errorMemory );
573 exit(1);
576 lseek( fd, offset, SEEK_SET );
577 if( read(fd, lpfont, length) != length )
579 fprintf(stderr, errorDLLRead );
580 exit(1);
583 if( (i = parse_fnt_data( lpfont, length )) )
585 fprintf(stderr, "%s%d\n", errorFontData, i );
586 exit(1);
589 free(lpfont); free(lpdata);
590 exit(0);
592 else
594 fprintf(stderr, errorEmpty );
595 exit(1);
597 free( lpdata );
599 else
601 fprintf(stderr, errorDLLRead);
602 exit(1);
604 break;
606 case FILE_FNT:
607 if( lpdata )
609 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
611 fprintf(stderr, "%s%d\n", errorFontData, i );
612 exit(1);
614 free( lpdata );
616 else
618 fprintf(stderr, errorFNTRead);
619 exit(1);
621 break;
623 case FILE_ERROR:
624 fprintf(stderr, errorFile );
625 exit(1);
627 close(fd);
628 exit(0);
630 else
632 fprintf(stderr, errorOpenFile );
633 exit(1);