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>
31 extern char* g_lpstrFileName
;
32 extern char* g_lpstrCharSet
;
40 char* g_lpstrFileName
= NULL
;
41 char* g_lpstrCharSet
= NULL
;
42 char* g_lpstrInputFile
= NULL
;
43 int g_outputPoints
= 0;
45 static char* errorDLLRead
= "Unable to read Windows DLL.\n";
46 static char* errorFNTRead
= "Unable to read .FNT file.\n";
47 static char* errorOpenFile
= "Unable to open file.\n";
48 static char* errorMemory
= "Memory allocation error.\n";
49 static char* errorFile
= "Corrupt or invalid file.\n";
50 static char* errorFontData
= "Unable to parse font data: Error ";
51 static char* errorEmpty
= "No fonts found.\n";
57 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
58 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
59 printf(" -f basename\tbasic output filename\n");
60 printf(" -t \t\toutput files by point size instead of pixel height\n");
61 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
62 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
66 /* convert big-endian value to the local format */
68 int return_data_value(enum data_types dtype
, void * pChr
)
74 ret_val
= (int) *(unsigned char *)pChr
;
78 ret_val
= *(unsigned char *)pChr
;
79 ret_val
+= (*((unsigned char *)pChr
+ 1) << 8);
85 for(i
=3; i
>= 0; i
--) {
86 ret_val
+= *((unsigned char *)pChr
+ i
) << (8*i
);
96 int make_bdf_filename(char* name
, fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
)
98 int l_nameoffset
= return_data_value(dfLong
, cpe_font_struct
->hdr
.dfFace
);
101 if( !g_lpstrFileName
)
104 l_nameoffset
> return_data_value(dfLong
, cpe_font_struct
->hdr
.dfSize
) + 1 )
106 lpChar
= (char*)(file_buffer
+ l_nameoffset
);
108 else lpChar
= g_lpstrFileName
;
110 strcpy( name
, lpChar
);
112 while( (lpChar
= strchr( name
, ' ')) )
115 /* construct a filename from the font typeface, slant, weight, and size */
117 if( cpe_font_struct
->hdr
.dfItalic
[0] ) strcat(name
, "_i" );
118 else strcat(name
, "_r" );
120 lpChar
= name
+ strlen( name
);
121 sprintf(lpChar
, "%d-%d.bdf", return_data_value(dfShort
, cpe_font_struct
->hdr
.dfWeight
),
122 (g_outputPoints
) ? return_data_value(dfShort
, cpe_font_struct
->hdr
.dfPoints
)
123 : return_data_value(dfShort
, cpe_font_struct
->hdr
.dfPixHeight
) );
127 /* parse FONT resource and write .bdf file */
129 int parse_fnt_data(unsigned char* file_buffer
, int length
)
131 fnt_fontS cpe_font_struct
;
134 memcpy((char *) &cpe_font_struct
.hdr
, file_buffer
, sizeof(fnt_hdrS
));
136 /* check font header */
138 t
= return_data_value(dfShort
, cpe_font_struct
.hdr
.dfVersion
);
139 if( t
!= 0x300 && t
!= 0x200) return ERROR_VERSION
;
141 t
= return_data_value(dfLong
, cpe_font_struct
.hdr
.dfSize
);
142 if( t
> length
) return ERROR_SIZE
;
145 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
147 int l_fchar
= return_data_value(dfChar
, cpe_font_struct
.hdr
.dfFirstChar
),
148 l_lchar
= return_data_value(dfChar
, cpe_font_struct
.hdr
.dfLastChar
);
149 int l_len
= l_lchar
- l_fchar
+ 1, l_ptr
= MAP_BEG
;
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
.dfFirstChar
),
185 l_lchar
= return_data_value(dfChar
, cpe_font_struct
->hdr
.dfLastChar
);
187 int l_len
= l_lchar
-l_fchar
+ 1,
188 l_hgt
= return_data_value(dfChar
, cpe_font_struct
->hdr
.dfPixHeight
);
189 int l_ascent
= return_data_value(dfShort
, cpe_font_struct
->hdr
.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
.dfFirstChar
),
292 l_lchar
= return_data_value(dfChar
, cpe_font_struct
->hdr
.dfLastChar
);
293 int l_len
= l_lchar
- l_fchar
+ 1;
294 int l_nameoffset
= return_data_value(dfLong
, cpe_font_struct
->hdr
.dfFace
);
295 int l_cellheight
= return_data_value(dfShort
, cpe_font_struct
->hdr
.dfPixHeight
);
296 int l_ascent
= return_data_value(dfShort
, cpe_font_struct
->hdr
.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
.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
.dfItalic
[0] ) /* slant */
328 else fputs("r-", fs
);
332 if( (cpe_font_struct
->hdr
.dfPitchAndFamily
[0] & 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
.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
.dfPixWidth
) ) fputs("c-", fs
);
347 else fputs("p-", fs
);
351 fprintf( fs
, "%d-", 10 * return_data_value(dfShort
, cpe_font_struct
->hdr
.dfAvgWidth
) );
355 switch( cpe_font_struct
->hdr
.dfCharSet
[0] )
357 /* Microsoft just had to invent its own charsets! */
359 case ANSI_CHARSET
: fputs("microsoft-cp1252\n", fs
); break;
360 case GREEK_CHARSET
: fputs("microsoft-cp1253\n", fs
); break;
361 case TURKISH_CHARSET
: fputs("microsoft-cp1254\n", fs
); break;
362 case HEBREW_CHARSET
: fputs("microsoft-cp1255\n", fs
); break;
363 case ARABIC_CHARSET
: fputs("microsoft-cp1256\n", fs
); break;
364 case BALTIC_CHARSET
: fputs("microsoft-cp1257\n", fs
); break;
365 case RUSSIAN_CHARSET
: fputs("microsoft-cp1251\n", fs
); break;
366 case EE_CHARSET
: fputs("microsoft-cp1250\n", fs
); break;
367 case SYMBOL_CHARSET
: fputs("microsoft-symbol\n", fs
); break;
368 case SHIFTJIS_CHARSET
: fputs("jisx0208.1983-0\n", fs
); break;
369 case DEFAULT_CHARSET
: fputs("iso8859-1\n", fs
); break;
373 if( !g_lpstrCharSet
)
375 fputs("Undefined charset, use -c option.\n", stderr
);
378 fprintf(fs
, "%s\n", g_lpstrCharSet
);
381 else return ERROR_DATA
;
383 fprintf(fs
, "SIZE %d %d %d\n",
385 return_data_value(dfShort
, cpe_font_struct
->hdr
.dfHorizRes
),
386 return_data_value(dfShort
, cpe_font_struct
->hdr
.dfVertRes
)); /* dfVertRes[2] */
388 fprintf(fs
, "FONTBOUNDINGBOX %d %d %d %d\n",
389 return_data_value(dfShort
, cpe_font_struct
->hdr
.dfMaxWidth
),
390 return_data_value(dfChar
, cpe_font_struct
->hdr
.dfPixHeight
),
391 0, l_ascent
- l_cellheight
);
393 fprintf(fs
, "STARTPROPERTIES 4\n");
395 fprintf(fs
, "FONT_ASCENT %d\n", l_ascent
); /* dfAscent[2] */
396 fprintf(fs
, "FONT_DESCENT %d\n", l_cellheight
- l_ascent
);
397 fprintf(fs
, "CAP_HEIGHT %d\n", l_ascent
-
398 return_data_value(dfShort
, cpe_font_struct
->hdr
.dfInternalLeading
));
399 fprintf(fs
, "DEFAULT_CHAR %d\n", return_data_value(dfShort
, cpe_font_struct
->hdr
.dfDefaultChar
));
401 fprintf(fs
, "ENDPROPERTIES\n");
403 fprintf(fs
, "CHARS %d\n", l_len
);
409 void parse_options(int argc
, char **argv
)
416 g_lpstrInputFile
= argv
[1];
425 for( i
= 1; i
< argc
- 1; i
++ )
427 if( argv
[i
][0] != '-' ||
428 strlen(argv
[i
]) != 2 ) break;
430 if( argv
[i
][1] == 'c' )
431 g_lpstrCharSet
= argv
[i
+1];
433 if( argv
[i
][1] == 'f' )
434 g_lpstrFileName
= argv
[i
+1];
436 if( argv
[i
][1] == 't' )
448 g_lpstrInputFile
= argv
[i
];
456 /* read file data and return file type */
458 int get_resource_table(int fd
, unsigned char** lpdata
, int fsize
)
460 IMAGE_DOS_HEADER mz_header
;
461 IMAGE_OS2_HEADER ne_header
;
462 short s
, offset
, size
, retval
;
465 lseek( fd
, 0, SEEK_SET
);
467 if( read(fd
, &mz_header
, sizeof(mz_header
)) != sizeof(mz_header
) )
470 s
= return_data_value(dfShort
, &mz_header
.e_magic
);
472 if( s
== IMAGE_DOS_SIGNATURE
) /* looks like .dll file so far... */
474 s
= return_data_value(dfShort
, &mz_header
.e_lfanew
);
475 lseek( fd
, s
, SEEK_SET
);
477 if( read(fd
, &ne_header
, sizeof(ne_header
)) != sizeof(ne_header
) )
480 s
= return_data_value(dfShort
, &ne_header
.ne_magic
);
482 if( s
== IMAGE_NT_SIGNATURE
)
484 fprintf( stderr
, "Do not know how to handle 32-bit Windows DLLs.\n");
487 else if ( s
!= IMAGE_OS2_SIGNATURE
) return FILE_ERROR
;
489 s
= return_data_value(dfShort
, &ne_header
.resource_tab_offset
);
490 size
= return_data_value(dfShort
, &ne_header
.rname_tab_offset
);
492 if( size
> fsize
) return FILE_ERROR
;
495 offset
= s
+ return_data_value(dfShort
, &mz_header
.e_lfanew
);
497 if( size
<= sizeof(NE_TYPEINFO
) ) return FILE_ERROR
;
500 else if( s
== 0x300 || s
== 0x200 ) /* maybe .fnt ? */
502 size
= return_data_value(dfLong
, (char*)&mz_header
+2);
504 if( size
!= fsize
) return FILE_ERROR
;
508 else return FILE_ERROR
;
510 *lpdata
= (unsigned char*)malloc(size
);
514 lseek( fd
, offset
, SEEK_SET
);
515 if( read(fd
, *lpdata
, size
) != size
)
516 { free( *lpdata
); *lpdata
= NULL
; }
524 int main(int argc
, char **argv
)
526 unsigned char* lpdata
= NULL
;
529 parse_options( argc
, argv
);
531 if( (fd
= open( g_lpstrInputFile
, O_RDONLY
)) )
534 struct stat file_stat
;
536 fstat( fd
, &file_stat
);
537 i
= get_resource_table( fd
, &lpdata
, file_stat
.st_size
);
545 NE_TYPEINFO
* pTInfo
= (NE_TYPEINFO
*)(lpdata
+ 2);
546 NE_NAMEINFO
* pFontStorage
= NULL
;
548 while( (i
= return_data_value(dfShort
, &pTInfo
->type_id
)) )
550 j
= return_data_value(dfShort
, &pTInfo
->count
);
551 if( i
== NE_RSCTYPE_FONT
)
554 pFontStorage
= (NE_NAMEINFO
*)(pTInfo
+ 1);
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
);