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
12 #ifdef HAVE_SYS_PARAM_H
13 # include <sys/param.h>
15 #include <sys/types.h>
33 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
34 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
36 char* g_lpstrFileName = NULL;
37 char* g_lpstrCharSet = NULL;
38 char* g_lpstrInputFile = NULL;
39 int g_outputPoints = 0;
41 static char* errorDLLRead = "Unable to read Windows DLL.\n";
42 static char* errorFNTRead = "Unable to read .FNT file.\n";
43 static char* errorOpenFile = "Unable to open file.\n";
44 static char* errorMemory = "Memory allocation error.\n";
45 static char* errorFile = "Corrupt or invalid file.\n";
46 static char* errorFontData = "Unable to parse font data: Error ";
47 static char* errorEmpty = "No fonts found.\n";
53 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
54 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
55 printf(" -f basename\tbasic output filename\n");
56 printf(" -t \t\toutput files by point size instead of pixel height\n");
57 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
58 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
62 /* convert little-endian value to the local format */
64 int return_data_value(enum data_types dtype, void * pChr)
70 ret_val = (int) *(unsigned char *)pChr;
74 ret_val = *(unsigned char *)pChr;
75 ret_val += (*((unsigned char *)pChr + 1) << 8);
81 for(i=3; i >= 0; i--) {
82 ret_val += *((unsigned char *)pChr + i) << (8*i);
92 int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
94 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
97 if( !g_lpstrFileName )
100 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
102 lpChar = (char*)(file_buffer + l_nameoffset);
104 else lpChar = g_lpstrFileName;
106 strcpy( name, lpChar );
108 while( (lpChar = strchr( name, ' ')) )
111 /* construct a filename from the font typeface, slant, weight, and size */
113 if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
114 else strcat(name, "_r" );
116 lpChar = name + strlen( name );
117 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight),
118 (g_outputPoints) ? return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints)
119 : return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight) );
123 /* parse FONT resource and write .bdf file */
125 int parse_fnt_data(unsigned char* file_buffer, int length)
127 fnt_fontS cpe_font_struct;
130 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
132 /* check font header */
134 t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
135 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
137 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
138 if( t > length ) return ERROR_SIZE;
141 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
143 int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar),
144 l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar);
145 int l_len = l_lchar - l_fchar + 1;
146 int l_ptr = sizeof(fnt_hdrS);
148 /* some fields were introduced for Windows 3.x fonts */
149 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200 )
152 /* malloc size = (# chars) * sizeof(WinCharS) */
154 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
157 /* NOW, convert them all to UNIX (lton) notation... */
159 for(ic=0; ic < l_len; ic++) {
160 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
161 l_ptr += 2; /* bump by sizeof(short) */
164 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200) {
165 cpe_font_struct.dfCharTable[ic].charOffset =
166 return_data_value(dfShort, &file_buffer[l_ptr]);
167 l_ptr += 2; /* bump by sizeof(long) */
169 else { /* Windows Version 3.0 type font */
170 cpe_font_struct.dfCharTable[ic].charOffset =
171 return_data_value(dfLong, &file_buffer[l_ptr]);
172 l_ptr += 4; /* bump by sizeof(long) */
175 t = dump_bdf(&cpe_font_struct, file_buffer);
176 free( cpe_font_struct.dfCharTable );
181 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
185 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
186 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
188 int l_len = l_lchar-l_fchar + 1,
189 l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight);
190 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
191 char l_filename[256];
193 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
196 if((fp = fopen(l_filename, "w")) == (FILE *) 0)
198 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
202 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
205 /* NOW, convert all chars to UNIX (lton) notation... */
207 for(ic=0; ic < l_len; ic++) {
208 int rowidx, l_span, /* how many char-cols wide is char? */
209 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
211 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
213 fprintf(fp, "STARTCHAR %d \n", ic);
214 fprintf(fp, "ENCODING %d\n", l_fchar);
215 fprintf(fp, "SWIDTH %d %d \n",
216 cpe_font_struct->dfCharTable[ic].charWidth*1000,
219 fprintf(fp, "DWIDTH %d %d \n",
220 cpe_font_struct->dfCharTable[ic].charWidth, 0);
222 fprintf(fp, "BBX %d %d %d %d\n",
223 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
226 fprintf(fp, "BITMAP\n");
227 for(rowidx=0; rowidx < l_hgt; rowidx++) {
229 case(0): /* 1-7 pixels wide font */
231 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
235 case(1): /* 8-15 pixels wide font */
237 fprintf(fp, "%02X%02X",
238 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
243 case(2): /* 16-23 pixels wide font */
245 fprintf(fp, "%02X%02X%02X",
246 file_buffer[l_idx+rowidx],
247 file_buffer[l_idx+l_hgt+rowidx],
248 file_buffer[l_idx+(2*l_hgt)+rowidx]);
253 case(3): /* 24-31 pixels wide font */
255 fprintf(fp, "%02X%02X%02X%02X",
256 file_buffer[l_idx+rowidx],
257 file_buffer[l_idx+l_hgt+rowidx],
258 file_buffer[l_idx+(2*l_hgt)+rowidx],
259 file_buffer[l_idx+(3*l_hgt)+rowidx]);
265 fprintf(fp, "%02X%02X%02X%02X%02X",
266 file_buffer[l_idx+rowidx],
267 file_buffer[l_idx+l_hgt+rowidx],
268 file_buffer[l_idx+(2*l_hgt)+rowidx],
269 file_buffer[l_idx+(3*l_hgt)+rowidx],
270 file_buffer[l_idx+(4*l_hgt)+rowidx]);
280 fprintf(fp, "ENDCHAR\n");
282 l_fchar++; /* Go to next one */
284 fprintf(fp, "ENDFONT\n");
290 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
292 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
293 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
294 int l_len = l_lchar - l_fchar + 1;
295 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
296 int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
297 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
299 fprintf(fs, "STARTFONT 2.1\n");
301 /* Compose font name */
304 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
307 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
308 short tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight);
310 while((lpChar = strchr(lpFace, '-')) )
313 fprintf(fs, "FONT -windows-%s-", lpFace );
315 if( tmWeight == 0 ) /* weight */
316 fputs("medium-", fs);
317 else if( tmWeight <= FW_LIGHT )
319 else if( tmWeight <= FW_MEDIUM )
320 fputs("medium-", fs);
321 else if( tmWeight <= FW_DEMIBOLD )
322 fputs("demibold-", fs);
323 else if( tmWeight <= FW_BOLD )
325 else fputs("black-", fs);
327 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
329 else fputs("r-", fs);
333 if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
334 fputs("normal-sans-", fs);
335 else fputs("normal--", fs); /* still can be -sans */
339 point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
340 dpi = (l_cellheight * 720) / point_size;
342 fprintf(fs, "%d-%d-%d-%d-", l_cellheight, 10*l_cellheight, 72, 72);
343 /* point_size, dpi, dpi); */
347 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
348 else fputs("p-", fs);
352 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
356 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
358 switch( cpe_font_struct->hdr.fi.dfCharSet )
360 /* Microsoft just had to invent its own charsets! */
362 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
363 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
364 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
365 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
366 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
367 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
368 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
369 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
370 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
371 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
372 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
376 fputs("Undefined charset, use -c option.\n", stderr);
380 else return ERROR_DATA;
382 fprintf(fs, "SIZE %d %d %d\n",
384 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
385 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
387 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
388 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
389 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
390 0, l_ascent - l_cellheight );
392 fprintf(fs, "STARTPROPERTIES 4\n");
394 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
395 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
396 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
397 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
398 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
400 fprintf(fs, "ENDPROPERTIES\n");
402 fprintf(fs, "CHARS %d\n", l_len);
408 void parse_options(int argc, char **argv)
415 g_lpstrInputFile = argv[1];
424 for( i = 1; i < argc - 1; i++ )
426 if( argv[i][0] != '-' ||
427 strlen(argv[i]) != 2 ) break;
429 if( argv[i][1] == 'c' )
430 g_lpstrCharSet = argv[i+1];
432 if( argv[i][1] == 'f' )
433 g_lpstrFileName = argv[i+1];
435 if( argv[i][1] == 't' )
447 g_lpstrInputFile = argv[i];
455 /* read file data and return file type */
457 int get_resource_table(int fd, unsigned char** lpdata, int fsize)
459 IMAGE_DOS_HEADER mz_header;
460 IMAGE_OS2_HEADER ne_header;
461 long s, offset, size;
464 lseek( fd, 0, SEEK_SET );
466 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
469 s = return_data_value(dfShort, &mz_header.e_magic);
471 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
473 s = return_data_value(dfShort, &mz_header.e_lfanew);
474 lseek( fd, s, SEEK_SET );
476 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
479 s = return_data_value(dfShort, &ne_header.ne_magic);
481 if( s == IMAGE_NT_SIGNATURE)
483 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
486 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
488 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
489 size = return_data_value(dfShort, &ne_header.ne_restab);
491 if( size > fsize ) return FILE_ERROR;
494 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
496 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
499 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
501 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
503 if( size != fsize ) return FILE_ERROR;
507 else return FILE_ERROR;
509 *lpdata = (unsigned char*)malloc(size);
513 lseek( fd, offset, SEEK_SET );
514 if( read(fd, *lpdata, size) != size )
515 { free( *lpdata ); *lpdata = NULL; }
523 int main(int argc, char **argv)
525 unsigned char* lpdata = NULL;
528 parse_options( argc, argv);
530 if( (fd = open( g_lpstrInputFile, O_RDONLY)) )
533 struct stat file_stat;
535 fstat( fd, &file_stat);
536 i = get_resource_table( fd, &lpdata, file_stat.st_size );
544 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
545 NE_NAMEINFO* pFontStorage = NULL;
547 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
549 j = return_data_value(dfShort, &pTInfo->count);
550 if( i == NE_RSCTYPE_FONT )
553 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
554 break; /* found one */
557 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
559 if( pFontStorage && count )
561 unsigned short size_shift = return_data_value(dfShort, lpdata);
562 unsigned char* lpfont = NULL;
566 for( j = 0; j < count; j++, pFontStorage++ )
568 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
569 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
571 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
573 fprintf(stderr, errorMemory );
577 lseek( fd, offset, SEEK_SET );
578 if( read(fd, lpfont, length) != length )
580 fprintf(stderr, errorDLLRead );
584 if( (i = parse_fnt_data( lpfont, length )) )
586 fprintf(stderr, "%s%d\n", errorFontData, i );
590 free(lpfont); free(lpdata);
595 fprintf(stderr, errorEmpty );
602 fprintf(stderr, errorDLLRead);
610 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
612 fprintf(stderr, "%s%d\n", errorFontData, i );
619 fprintf(stderr, errorFNTRead);
625 fprintf(stderr, errorFile );
633 fprintf(stderr, errorOpenFile );