Removed deferred Expose events handling.
[wine/multimedia.git] / tools / fnt2bdf.c
bloba90e55fb0a4b5bb469f23ce921f5fb173388f79a
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 <sys/param.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <fcntl.h>
18 #include "windows.h"
19 #include "fnt2bdf.h"
20 #include "neexe.h"
21 #include "module.h"
23 #define MAP_BEG 118
25 extern char* g_lpstrFileName;
26 extern char* g_lpstrCharSet;
28 #define FILE_ERROR 0
29 #define FILE_DLL 1
30 #define FILE_FNT 2
32 /* global options */
34 char* g_lpstrFileName = NULL;
35 char* g_lpstrCharSet = NULL;
36 char* g_lpstrInputFile = NULL;
37 int g_outputPoints = 0;
39 static char* errorDLLRead = "Unable to read Windows DLL.\n";
40 static char* errorFNTRead = "Unable to read .FNT file.\n";
41 static char* errorOpenFile = "Unable to open file.\n";
42 static char* errorMemory = "Memory allocation error.\n";
43 static char* errorFile = "Corrupt or invalid file.\n";
44 static char* errorFontData = "Unable to parse font data: Error ";
45 static char* errorEmpty = "No fonts found.\n";
47 /* info */
49 void usage(void)
51 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
52 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
53 printf(" -f basename\tbasic output filename\n");
54 printf(" -t \t\toutput files by point size instead of pixel height\n");
55 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
56 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
57 exit(-1);
60 /* convert big-endian value to the local format */
62 int return_data_value(enum data_types dtype, void * pChr)
64 int ret_val = 0;
66 switch(dtype) {
67 case (dfChar):
68 ret_val = (int) *(unsigned char *)pChr;
69 break;
71 case(dfShort):
72 ret_val = *(unsigned char *)pChr;
73 ret_val += (*((unsigned char *)pChr + 1) << 8);
74 break;
76 case(dfLong): {
77 int i;
79 for(i=3; i >= 0; i--) {
80 ret_val += *((unsigned char *)pChr + i) << (8*i);
82 break;
84 case(dfString):
85 break;
87 return ret_val;
90 int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
92 int l_nameoffset = return_data_value(dfLong, cpe_font_struct->hdr.dfFace);
93 char* lpChar;
95 if( !g_lpstrFileName )
97 if( !l_nameoffset ||
98 l_nameoffset > return_data_value(dfLong, cpe_font_struct->hdr.dfSize) + 1 )
99 return ERROR_DATA;
100 lpChar = (char*)(file_buffer + l_nameoffset);
102 else lpChar = g_lpstrFileName;
104 strcpy( name, lpChar );
106 while( (lpChar = strchr( name, ' ')) )
107 *lpChar = '-';
109 /* construct a filename from the font typeface, slant, weight, and size */
111 if( cpe_font_struct->hdr.dfItalic[0] ) strcat(name, "_i" );
112 else strcat(name, "_r" );
114 lpChar = name + strlen( name );
115 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, cpe_font_struct->hdr.dfWeight),
116 (g_outputPoints) ? return_data_value(dfShort, cpe_font_struct->hdr.dfPoints)
117 : return_data_value(dfShort, cpe_font_struct->hdr.dfPixHeight) );
118 return 0;
121 /* parse FONT resource and write .bdf file */
123 int parse_fnt_data(unsigned char* file_buffer, int length)
125 fnt_fontS cpe_font_struct;
126 int ic=0, t;
128 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
130 /* check font header */
132 t = return_data_value(dfShort, cpe_font_struct.hdr.dfVersion);
133 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
135 t = return_data_value(dfLong, cpe_font_struct.hdr.dfSize);
136 if( t > length ) return ERROR_SIZE;
137 else
139 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
141 int l_fchar = return_data_value(dfChar, cpe_font_struct.hdr.dfFirstChar),
142 l_lchar = return_data_value(dfChar, cpe_font_struct.hdr.dfLastChar);
143 int l_len = l_lchar - l_fchar + 1, l_ptr = MAP_BEG;
145 /* malloc size = (# chars) * sizeof(WinCharS) */
147 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
148 return ERROR_MEMORY;
150 /* NOW, convert them all to UNIX (lton) notation... */
152 for(ic=0; ic < l_len; ic++) {
153 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
154 l_ptr += 2; /* bump by sizeof(short) */
157 if( return_data_value(dfShort, cpe_font_struct.hdr.dfVersion) == 0x200) {
158 cpe_font_struct.dfCharTable[ic].charOffset =
159 return_data_value(dfShort, &file_buffer[l_ptr]);
160 l_ptr += 2; /* bump by sizeof(long) */
162 else { /* Windows Version 3.0 type font */
163 cpe_font_struct.dfCharTable[ic].charOffset =
164 return_data_value(dfLong, &file_buffer[l_ptr]);
165 l_ptr += 4; /* bump by sizeof(long) */
168 t = dump_bdf(&cpe_font_struct, file_buffer);
169 free( cpe_font_struct.dfCharTable );
171 return t;
174 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
176 FILE* fp;
177 int ic;
178 int l_fchar = return_data_value(dfChar, cpe_font_struct->hdr.dfFirstChar),
179 l_lchar = return_data_value(dfChar, cpe_font_struct->hdr.dfLastChar);
181 int l_len = l_lchar-l_fchar + 1,
182 l_hgt = return_data_value(dfChar, cpe_font_struct->hdr.dfPixHeight);
183 int l_ascent = return_data_value(dfShort, cpe_font_struct->hdr.dfAscent);
184 char l_filename[256];
186 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
187 return ic;
189 if((fp = fopen(l_filename, "w")) == (FILE *) 0)
191 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
192 return ERROR_FILE;
195 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
196 if (ic) return (ic);
198 /* NOW, convert all chars to UNIX (lton) notation... */
200 for(ic=0; ic < l_len; ic++) {
201 int rowidx, l_span, /* how many char-cols wide is char? */
202 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
204 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
206 fprintf(fp, "STARTCHAR %d \n", ic);
207 fprintf(fp, "ENCODING %d\n", l_fchar);
208 fprintf(fp, "SWIDTH %d %d \n",
209 cpe_font_struct->dfCharTable[ic].charWidth*1000,
212 fprintf(fp, "DWIDTH %d %d \n",
213 cpe_font_struct->dfCharTable[ic].charWidth, 0);
215 fprintf(fp, "BBX %d %d %d %d\n",
216 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
217 l_ascent - l_hgt);
219 fprintf(fp, "BITMAP\n");
220 for(rowidx=0; rowidx < l_hgt; rowidx++) {
221 switch(l_span) {
222 case(0): /* 1-7 pixels wide font */
224 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
225 break;
228 case(1): /* 8-15 pixels wide font */
230 fprintf(fp, "%02X%02X",
231 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
232 fprintf(fp, "\n");
233 break;
236 case(2): /* 16-23 pixels wide font */
238 fprintf(fp, "%02X%02X%02X",
239 file_buffer[l_idx+rowidx],
240 file_buffer[l_idx+l_hgt+rowidx],
241 file_buffer[l_idx+(2*l_hgt)+rowidx]);
242 fprintf(fp, "\n");
243 break;
246 case(3): /* 24-31 pixels wide font */
248 fprintf(fp, "%02X%02X%02X%02X",
249 file_buffer[l_idx+rowidx],
250 file_buffer[l_idx+l_hgt+rowidx],
251 file_buffer[l_idx+(2*l_hgt)+rowidx],
252 file_buffer[l_idx+(3*l_hgt)+rowidx]);
253 fprintf(fp, "\n");
254 break;
256 case(4): /* 32-39 */
258 fprintf(fp, "%02X%02X%02X%02X%02X",
259 file_buffer[l_idx+rowidx],
260 file_buffer[l_idx+l_hgt+rowidx],
261 file_buffer[l_idx+(2*l_hgt)+rowidx],
262 file_buffer[l_idx+(3*l_hgt)+rowidx],
263 file_buffer[l_idx+(4*l_hgt)+rowidx]);
264 fprintf(fp, "\n");
265 break;
267 default:
268 fclose(fp);
269 unlink(l_filename);
270 return ERROR_DATA;
273 fprintf(fp, "ENDCHAR\n");
275 l_fchar++; /* Go to next one */
277 fprintf(fp, "ENDFONT\n");
278 fclose(fp);
279 return 0;
283 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
285 int l_fchar = return_data_value(dfChar, cpe_font_struct->hdr.dfFirstChar),
286 l_lchar = return_data_value(dfChar, cpe_font_struct->hdr.dfLastChar);
287 int l_len = l_lchar - l_fchar + 1;
288 int l_nameoffset = return_data_value(dfLong, cpe_font_struct->hdr.dfFace);
289 int l_cellheight = return_data_value(dfShort, cpe_font_struct->hdr.dfPixHeight);
290 int l_ascent = return_data_value(dfShort, cpe_font_struct->hdr.dfAscent);
292 fprintf(fs, "STARTFONT 2.1\n");
294 /* Compose font name */
296 if( l_nameoffset &&
297 l_nameoffset < return_data_value(dfLong, cpe_font_struct->hdr.dfSize) )
299 int dpi, point_size;
300 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
301 short tmWeight = return_data_value(dfShort, cpe_font_struct->hdr.dfWeight);
303 while((lpChar = strchr(lpFace, '-')) )
304 *lpChar = ' ';
306 fprintf(fs, "FONT -windows-%s-", lpFace );
308 if( tmWeight == 0 ) /* weight */
309 fputs("medium-", fs);
310 else if( tmWeight <= FW_LIGHT )
311 fputs("light-", fs);
312 else if( tmWeight <= FW_MEDIUM )
313 fputs("medium-", fs);
314 else if( tmWeight <= FW_DEMIBOLD )
315 fputs("demibold-", fs);
316 else if( tmWeight <= FW_BOLD )
317 fputs("bold-", fs);
318 else fputs("black-", fs);
320 if( cpe_font_struct->hdr.dfItalic[0] ) /* slant */
321 fputs("i-", fs);
322 else fputs("r-", fs);
324 /* style */
326 if( (cpe_font_struct->hdr.dfPitchAndFamily[0] & 0xF0) == FF_SWISS )
327 fputs("normal-sans-", fs);
328 else fputs("normal--", fs); /* still can be -sans */
330 /* y extents */
332 point_size = 10 * return_data_value(dfShort, cpe_font_struct->hdr.dfPoints );
333 dpi = (l_cellheight * 720) / point_size;
335 fprintf(fs, "%d-%d-%d-%d-", l_cellheight, 10*l_cellheight, 72, 72);
336 /* point_size, dpi, dpi); */
338 /* spacing */
340 if( return_data_value(dfShort, cpe_font_struct->hdr.dfPixWidth) ) fputs("c-", fs);
341 else fputs("p-", fs);
343 /* average width */
345 fprintf( fs, "%d-", 10 * return_data_value(dfShort, cpe_font_struct->hdr.dfAvgWidth) );
347 /* charset */
349 switch( cpe_font_struct->hdr.dfCharSet[0] )
351 /* Microsoft just had to invent its own charsets! */
353 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
354 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
355 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
356 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
357 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
358 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
359 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
360 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
361 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
362 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
363 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
365 default:
366 case OEM_CHARSET:
367 if( !g_lpstrCharSet )
369 fputs("Undefined charset, use -c option.\n", stderr);
370 return ERROR_DATA;
372 fprintf(fs, "%s\n", g_lpstrCharSet);
375 else return ERROR_DATA;
377 fprintf(fs, "SIZE %d %d %d\n",
378 l_cellheight,
379 return_data_value(dfShort, cpe_font_struct->hdr.dfHorizRes),
380 return_data_value(dfShort, cpe_font_struct->hdr.dfVertRes)); /* dfVertRes[2] */
382 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
383 return_data_value(dfShort, cpe_font_struct->hdr.dfMaxWidth),
384 return_data_value(dfChar, cpe_font_struct->hdr.dfPixHeight),
385 0, l_ascent - l_cellheight );
387 fprintf(fs, "STARTPROPERTIES 4\n");
389 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
390 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
391 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
392 return_data_value(dfShort, cpe_font_struct->hdr.dfInternalLeading));
393 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, cpe_font_struct->hdr.dfDefaultChar));
395 fprintf(fs, "ENDPROPERTIES\n");
397 fprintf(fs, "CHARS %d\n", l_len);
398 return 0;
403 void parse_options(int argc, char **argv)
405 int i;
407 switch( argc )
409 case 2:
410 g_lpstrInputFile = argv[1];
411 break;
413 case 3:
414 case 4:
415 case 5:
416 case 6:
417 case 7:
418 case 8:
419 for( i = 1; i < argc - 1; i++ )
421 if( argv[i][0] != '-' ||
422 strlen(argv[i]) != 2 ) break;
424 if( argv[i][1] == 'c' )
425 g_lpstrCharSet = argv[i+1];
426 else
427 if( argv[i][1] == 'f' )
428 g_lpstrFileName = argv[i+1];
429 else
430 if( argv[i][1] == 't' )
432 g_outputPoints = 1;
433 continue;
435 else
436 usage();
438 i++;
440 if( i == argc - 1 )
442 g_lpstrInputFile = argv[i];
443 break;
445 default: usage();
450 /* read file data and return file type */
452 int get_resource_table(int fd, unsigned char** lpdata, int fsize)
454 IMAGE_DOS_HEADER mz_header;
455 IMAGE_OS2_HEADER ne_header;
456 short s, offset, size, retval;
459 lseek( fd, 0, SEEK_SET );
461 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
462 return FILE_ERROR;
464 s = return_data_value(dfShort, &mz_header.e_magic);
466 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
468 s = return_data_value(dfShort, &mz_header.e_lfanew);
469 lseek( fd, s, SEEK_SET );
471 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
472 return FILE_ERROR;
474 s = return_data_value(dfShort, &ne_header.ne_magic);
476 if( s == IMAGE_NT_SIGNATURE)
478 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
479 return FILE_ERROR;
481 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
483 s = return_data_value(dfShort, &ne_header.resource_tab_offset);
484 size = return_data_value(dfShort, &ne_header.rname_tab_offset);
486 if( size > fsize ) return FILE_ERROR;
488 size -= s;
489 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
491 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
492 retval = FILE_DLL;
494 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
496 size = return_data_value(dfLong, (char*)&mz_header+2);
498 if( size != fsize ) return FILE_ERROR;
499 offset = 0;
500 retval = FILE_FNT;
502 else return FILE_ERROR;
504 *lpdata = (unsigned char*)malloc(size);
506 if( *lpdata )
508 lseek( fd, offset, SEEK_SET );
509 if( read(fd, *lpdata, size) != size )
510 { free( *lpdata ); *lpdata = NULL; }
512 return retval;
516 /* entry point */
518 int main(int argc, char **argv)
520 unsigned char* lpdata = NULL;
521 int fd;
523 parse_options( argc, argv);
525 if( (fd = open( g_lpstrInputFile, O_RDONLY)) )
527 int i;
528 struct stat file_stat;
530 fstat( fd, &file_stat);
531 i = get_resource_table( fd, &lpdata, file_stat.st_size );
533 switch(i)
535 case FILE_DLL:
536 if( lpdata )
538 int j, count = 0;
539 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
540 NE_NAMEINFO* pFontStorage = NULL;
542 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
544 j = return_data_value(dfShort, &pTInfo->count);
545 if( i == NE_RSCTYPE_FONT )
547 count = j;
548 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
551 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
553 if( pFontStorage && count )
555 unsigned short size_shift = return_data_value(dfShort, lpdata);
556 unsigned char* lpfont = NULL;
557 unsigned offset;
558 unsigned length;
560 for( j = 0; j < count; j++, pFontStorage++ )
562 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
563 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
565 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
567 fprintf(stderr, errorMemory );
568 exit(1);
571 lseek( fd, offset, SEEK_SET );
572 if( read(fd, lpfont, length) != length )
574 fprintf(stderr, errorDLLRead );
575 exit(1);
578 if( (i = parse_fnt_data( lpfont, length )) )
580 fprintf(stderr, "%s%d\n", errorFontData, i );
581 exit(1);
584 free(lpfont); free(lpdata);
585 exit(0);
587 else
589 fprintf(stderr, errorEmpty );
590 exit(1);
592 free( lpdata );
594 else
596 fprintf(stderr, errorDLLRead);
597 exit(1);
599 break;
601 case FILE_FNT:
602 if( lpdata )
604 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
606 fprintf(stderr, "%s%d\n", errorFontData, i );
607 exit(1);
609 free( lpdata );
611 else
613 fprintf(stderr, errorFNTRead);
614 exit(1);
616 break;
618 case FILE_ERROR:
619 fprintf(stderr, errorFile );
620 exit(1);
622 close(fd);
623 exit(0);
625 else
627 fprintf(stderr, errorOpenFile );
628 exit(1);