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>
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";
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");
61 /* convert little-endian value to the local format */
63 int return_data_value(enum data_types dtype
, void * pChr
)
69 ret_val
= (int) *(unsigned char *)pChr
;
73 ret_val
= *(unsigned char *)pChr
;
74 ret_val
+= (*((unsigned char *)pChr
+ 1) << 8);
80 for(i
=3; i
>= 0; i
--) {
81 ret_val
+= *((unsigned char *)pChr
+ i
) << (8*i
);
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
);
96 if( !g_lpstrFileName
)
99 l_nameoffset
> return_data_value(dfLong
, &cpe_font_struct
->hdr
.dfSize
) + 1 )
101 lpChar
= (char*)(file_buffer
+ l_nameoffset
);
103 else lpChar
= g_lpstrFileName
;
105 strcpy( name
, lpChar
);
107 while( (lpChar
= strchr( name
, ' ')) )
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
) );
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
;
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
;
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 )
151 /* malloc size = (# chars) * sizeof(WinCharS) */
153 if((cpe_font_struct
.dfCharTable
= (WinCharS
*) calloc(sizeof(WinCharS
), l_len
)) == NULL
)
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
);
180 int dump_bdf( fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
)
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
)) )
195 if((fp
= fopen(l_filename
, "w")) == (FILE *) 0)
197 fprintf(stderr
, "Couldn't open \"%s\" for output.\n", l_filename
);
201 ic
= dump_bdf_hdr(fp
, cpe_font_struct
, file_buffer
);
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,
225 fprintf(fp
, "BITMAP\n");
226 for(rowidx
=0; rowidx
< l_hgt
; rowidx
++) {
228 case(0): /* 1-7 pixels wide font */
230 fprintf(fp
, "%02X\n", (int) file_buffer
[l_idx
+rowidx
]);
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
]);
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
]);
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
]);
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
]);
279 fprintf(fp
, "ENDCHAR\n");
281 l_fchar
++; /* Go to next one */
283 fprintf(fp
, "ENDFONT\n");
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 */
303 l_nameoffset
< return_data_value(dfLong
, &cpe_font_struct
->hdr
.dfSize
) )
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
, '-')) )
312 fprintf(fs
, "FONT -windows-%s-", lpFace
);
314 if( tmWeight
== 0 ) /* weight */
315 fputs("medium-", fs
);
316 else if( tmWeight
<= FW_LIGHT
)
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
)
324 else fputs("black-", fs
);
326 if( cpe_font_struct
->hdr
.fi
.dfItalic
) /* slant */
328 else fputs("r-", fs
);
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 */
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); */
346 if( return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfPixWidth
) ) fputs("c-", fs
);
347 else fputs("p-", fs
);
351 fprintf( fs
, "%d-", 10 * return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfAvgWidth
) );
355 if( g_lpstrCharSet
) fprintf(fs
, "%s\n", g_lpstrCharSet
);
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;
375 fputs("Undefined charset, use -c option.\n", stderr
);
379 else return ERROR_DATA
;
381 fprintf(fs
, "SIZE %d %d %d\n",
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
);
407 void parse_options(int argc
, char **argv
)
414 g_lpstrInputFile
= argv
[1];
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];
431 if( argv
[i
][1] == 'f' )
432 g_lpstrFileName
= argv
[i
+1];
434 if( argv
[i
][1] == 't' )
446 g_lpstrInputFile
= argv
[i
];
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
;
463 lseek( fd
, 0, SEEK_SET
);
465 if( read(fd
, &mz_header
, sizeof(mz_header
)) != sizeof(mz_header
) )
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
) )
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");
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
;
493 offset
= s
+ return_data_value(dfShort
, &mz_header
.e_lfanew
);
495 if( size
<= sizeof(NE_TYPEINFO
) ) return FILE_ERROR
;
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
;
506 else return FILE_ERROR
;
508 *lpdata
= (unsigned char*)malloc(size
);
512 lseek( fd
, offset
, SEEK_SET
);
513 if( read(fd
, *lpdata
, size
) != size
)
514 { free( *lpdata
); *lpdata
= NULL
; }
522 int main(int argc
, char **argv
)
524 unsigned char* lpdata
= NULL
;
527 parse_options( argc
, argv
);
529 if( (fd
= open( g_lpstrInputFile
, O_RDONLY
)) )
532 struct stat file_stat
;
534 fstat( fd
, &file_stat
);
535 i
= get_resource_table( fd
, &lpdata
, file_stat
.st_size
);
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
)
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
;
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
);
576 lseek( fd
, offset
, SEEK_SET
);
577 if( read(fd
, lpfont
, length
) != length
)
579 fprintf(stderr
, errorDLLRead
);
583 if( (i
= parse_fnt_data( lpfont
, length
)) )
585 fprintf(stderr
, "%s%d\n", errorFontData
, i
);
589 free(lpfont
); free(lpdata
);
594 fprintf(stderr
, errorEmpty
);
601 fprintf(stderr
, errorDLLRead
);
609 if( (i
= parse_fnt_data( lpdata
, file_stat
.st_size
)) )
611 fprintf(stderr
, "%s%d\n", errorFontData
, i
);
618 fprintf(stderr
, errorFNTRead
);
624 fprintf(stderr
, errorFile
);
632 fprintf(stderr
, errorOpenFile
);