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>
29 extern char* g_lpstrFileName
;
30 extern char* g_lpstrCharSet
;
38 char* g_lpstrFileName
= NULL
;
39 char* g_lpstrCharSet
= NULL
;
40 char* g_lpstrInputFile
= NULL
;
41 int g_outputPoints
= 0;
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";
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");
64 /* convert big-endian value to the local format */
66 int return_data_value(enum data_types dtype
, void * pChr
)
72 ret_val
= (int) *(unsigned char *)pChr
;
76 ret_val
= *(unsigned char *)pChr
;
77 ret_val
+= (*((unsigned char *)pChr
+ 1) << 8);
83 for(i
=3; i
>= 0; i
--) {
84 ret_val
+= *((unsigned char *)pChr
+ i
) << (8*i
);
94 int make_bdf_filename(char* name
, fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
)
96 int l_nameoffset
= return_data_value(dfLong
, cpe_font_struct
->hdr
.dfFace
);
99 if( !g_lpstrFileName
)
102 l_nameoffset
> return_data_value(dfLong
, cpe_font_struct
->hdr
.dfSize
) + 1 )
104 lpChar
= (char*)(file_buffer
+ l_nameoffset
);
106 else lpChar
= g_lpstrFileName
;
108 strcpy( name
, lpChar
);
110 while( (lpChar
= strchr( name
, ' ')) )
113 /* construct a filename from the font typeface, slant, weight, and size */
115 if( cpe_font_struct
->hdr
.dfItalic
[0] ) strcat(name
, "_i" );
116 else strcat(name
, "_r" );
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
) );
125 /* parse FONT resource and write .bdf file */
127 int parse_fnt_data(unsigned char* file_buffer
, int length
)
129 fnt_fontS cpe_font_struct
;
132 memcpy((char *) &cpe_font_struct
.hdr
, file_buffer
, sizeof(fnt_hdrS
));
134 /* check font header */
136 t
= return_data_value(dfShort
, cpe_font_struct
.hdr
.dfVersion
);
137 if( t
!= 0x300 && t
!= 0x200) return ERROR_VERSION
;
139 t
= return_data_value(dfLong
, cpe_font_struct
.hdr
.dfSize
);
140 if( t
> length
) return ERROR_SIZE
;
143 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
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
;
149 /* malloc size = (# chars) * sizeof(WinCharS) */
151 if((cpe_font_struct
.dfCharTable
= (WinCharS
*) calloc(sizeof(WinCharS
), l_len
)) == NULL
)
154 /* NOW, convert them all to UNIX (lton) notation... */
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) */
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) */
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) */
172 t
= dump_bdf(&cpe_font_struct
, file_buffer
);
173 free( cpe_font_struct
.dfCharTable
);
178 int dump_bdf( fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
)
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
);
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];
190 if( (ic
= make_bdf_filename(l_filename
, cpe_font_struct
, file_buffer
)) )
193 if((fp
= fopen(l_filename
, "w")) == (FILE *) 0)
195 fprintf(stderr
, "Couldn't open \"%s\" for output.\n", l_filename
);
199 ic
= dump_bdf_hdr(fp
, cpe_font_struct
, file_buffer
);
202 /* NOW, convert all chars to UNIX (lton) notation... */
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
;
208 l_span
= (int) (cpe_font_struct
->dfCharTable
[ic
].charWidth
-1)/8;
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,
216 fprintf(fp
, "DWIDTH %d %d \n",
217 cpe_font_struct
->dfCharTable
[ic
].charWidth
, 0);
219 fprintf(fp
, "BBX %d %d %d %d\n",
220 cpe_font_struct
->dfCharTable
[ic
].charWidth
, l_hgt
, 0,
223 fprintf(fp
, "BITMAP\n");
224 for(rowidx
=0; rowidx
< l_hgt
; rowidx
++) {
226 case(0): /* 1-7 pixels wide font */
228 fprintf(fp
, "%02X\n", (int) file_buffer
[l_idx
+rowidx
]);
232 case(1): /* 8-15 pixels wide font */
234 fprintf(fp
, "%02X%02X",
235 (int) file_buffer
[l_idx
+rowidx
], file_buffer
[l_idx
+l_hgt
+rowidx
]);
240 case(2): /* 16-23 pixels wide font */
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
]);
250 case(3): /* 24-31 pixels wide font */
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
]);
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
]);
277 fprintf(fp
, "ENDCHAR\n");
279 l_fchar
++; /* Go to next one */
281 fprintf(fp
, "ENDFONT\n");
287 int dump_bdf_hdr(FILE* fs
, fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
)
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
);
296 fprintf(fs
, "STARTFONT 2.1\n");
298 /* Compose font name */
301 l_nameoffset
< return_data_value(dfLong
, cpe_font_struct
->hdr
.dfSize
) )
304 char* lpFace
= (char*)(file_buffer
+ l_nameoffset
), *lpChar
;
305 short tmWeight
= return_data_value(dfShort
, cpe_font_struct
->hdr
.dfWeight
);
307 while((lpChar
= strchr(lpFace
, '-')) )
310 fprintf(fs
, "FONT -windows-%s-", lpFace
);
312 if( tmWeight
== 0 ) /* weight */
313 fputs("medium-", fs
);
314 else if( tmWeight
<= FW_LIGHT
)
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
)
322 else fputs("black-", fs
);
324 if( cpe_font_struct
->hdr
.dfItalic
[0] ) /* slant */
326 else fputs("r-", fs
);
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 */
336 point_size
= 10 * return_data_value(dfShort
, cpe_font_struct
->hdr
.dfPoints
);
337 dpi
= (l_cellheight
* 720) / point_size
;
339 fprintf(fs
, "%d-%d-%d-%d-", l_cellheight
, 10*l_cellheight
, 72, 72);
340 /* point_size, dpi, dpi); */
344 if( return_data_value(dfShort
, cpe_font_struct
->hdr
.dfPixWidth
) ) fputs("c-", fs
);
345 else fputs("p-", fs
);
349 fprintf( fs
, "%d-", 10 * return_data_value(dfShort
, cpe_font_struct
->hdr
.dfAvgWidth
) );
353 switch( cpe_font_struct
->hdr
.dfCharSet
[0] )
355 /* Microsoft just had to invent its own charsets! */
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;
371 if( !g_lpstrCharSet
)
373 fputs("Undefined charset, use -c option.\n", stderr
);
376 fprintf(fs
, "%s\n", g_lpstrCharSet
);
379 else return ERROR_DATA
;
381 fprintf(fs
, "SIZE %d %d %d\n",
383 return_data_value(dfShort
, cpe_font_struct
->hdr
.dfHorizRes
),
384 return_data_value(dfShort
, cpe_font_struct
->hdr
.dfVertRes
)); /* dfVertRes[2] */
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
);
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
.dfInternalLeading
));
397 fprintf(fs
, "DEFAULT_CHAR %d\n", return_data_value(dfShort
, cpe_font_struct
->hdr
.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 short s
, offset
, size
, retval
;
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
.resource_tab_offset
);
488 size
= return_data_value(dfShort
, &ne_header
.rname_tab_offset
);
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
, (char*)&mz_header
+2);
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);
555 pTInfo
= (NE_TYPEINFO
*)((char*)(pTInfo
+1) + j
*sizeof(NE_NAMEINFO
));
557 if( pFontStorage
&& count
)
559 unsigned short size_shift
= return_data_value(dfShort
, lpdata
);
560 unsigned char* lpfont
= NULL
;
564 for( j
= 0; j
< count
; j
++, pFontStorage
++ )
566 length
= return_data_value(dfShort
, &pFontStorage
->length
) << size_shift
;
567 offset
= return_data_value(dfShort
, &pFontStorage
->offset
) << size_shift
;
569 if( !(lpfont
= (unsigned char*) realloc( lpfont
, length
)) )
571 fprintf(stderr
, errorMemory
);
575 lseek( fd
, offset
, SEEK_SET
);
576 if( read(fd
, lpfont
, length
) != length
)
578 fprintf(stderr
, errorDLLRead
);
582 if( (i
= parse_fnt_data( lpfont
, length
)) )
584 fprintf(stderr
, "%s%d\n", errorFontData
, i
);
588 free(lpfont
); free(lpdata
);
593 fprintf(stderr
, errorEmpty
);
600 fprintf(stderr
, errorDLLRead
);
608 if( (i
= parse_fnt_data( lpdata
, file_stat
.st_size
)) )
610 fprintf(stderr
, "%s%d\n", errorFontData
, i
);
617 fprintf(stderr
, errorFNTRead
);
623 fprintf(stderr
, errorFile
);
631 fprintf(stderr
, errorOpenFile
);