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
10 #include <sys/param.h>
11 #include <sys/types.h>
25 extern char* g_lpstrFileName
;
26 extern char* g_lpstrCharSet
;
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";
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");
60 /* convert big-endian value to the local format */
62 int return_data_value(enum data_types dtype
, void * pChr
)
68 ret_val
= (int) *(unsigned char *)pChr
;
72 ret_val
= *(unsigned char *)pChr
;
73 ret_val
+= (*((unsigned char *)pChr
+ 1) << 8);
79 for(i
=3; i
>= 0; i
--) {
80 ret_val
+= *((unsigned char *)pChr
+ i
) << (8*i
);
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
);
95 if( !g_lpstrFileName
)
98 l_nameoffset
> return_data_value(dfLong
, cpe_font_struct
->hdr
.dfSize
) + 1 )
100 lpChar
= (char*)(file_buffer
+ l_nameoffset
);
102 else lpChar
= g_lpstrFileName
;
104 strcpy( name
, lpChar
);
106 while( (lpChar
= strchr( name
, ' ')) )
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
) );
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
;
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
;
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
)
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
);
174 int dump_bdf( fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
)
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
)) )
189 if((fp
= fopen(l_filename
, "w")) == (FILE *) 0)
191 fprintf(stderr
, "Couldn't open \"%s\" for output.\n", l_filename
);
195 ic
= dump_bdf_hdr(fp
, cpe_font_struct
, file_buffer
);
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,
219 fprintf(fp
, "BITMAP\n");
220 for(rowidx
=0; rowidx
< l_hgt
; rowidx
++) {
222 case(0): /* 1-7 pixels wide font */
224 fprintf(fp
, "%02X\n", (int) file_buffer
[l_idx
+rowidx
]);
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
]);
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
]);
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
]);
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
]);
273 fprintf(fp
, "ENDCHAR\n");
275 l_fchar
++; /* Go to next one */
277 fprintf(fp
, "ENDFONT\n");
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 */
297 l_nameoffset
< return_data_value(dfLong
, cpe_font_struct
->hdr
.dfSize
) )
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
, '-')) )
306 fprintf(fs
, "FONT -windows-%s-", lpFace
);
308 if( tmWeight
== 0 ) /* weight */
309 fputs("medium-", fs
);
310 else if( tmWeight
<= FW_LIGHT
)
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
)
318 else fputs("black-", fs
);
320 if( cpe_font_struct
->hdr
.dfItalic
[0] ) /* slant */
322 else fputs("r-", fs
);
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 */
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); */
340 if( return_data_value(dfShort
, cpe_font_struct
->hdr
.dfPixWidth
) ) fputs("c-", fs
);
341 else fputs("p-", fs
);
345 fprintf( fs
, "%d-", 10 * return_data_value(dfShort
, cpe_font_struct
->hdr
.dfAvgWidth
) );
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;
367 if( !g_lpstrCharSet
)
369 fputs("Undefined charset, use -c option.\n", stderr
);
372 fprintf(fs
, "%s\n", g_lpstrCharSet
);
375 else return ERROR_DATA
;
377 fprintf(fs
, "SIZE %d %d %d\n",
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
);
403 void parse_options(int argc
, char **argv
)
410 g_lpstrInputFile
= argv
[1];
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];
427 if( argv
[i
][1] == 'f' )
428 g_lpstrFileName
= argv
[i
+1];
430 if( argv
[i
][1] == 't' )
442 g_lpstrInputFile
= argv
[i
];
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
) )
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
) )
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");
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
;
489 offset
= s
+ return_data_value(dfShort
, &mz_header
.e_lfanew
);
491 if( size
<= sizeof(NE_TYPEINFO
) ) return FILE_ERROR
;
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
;
502 else return FILE_ERROR
;
504 *lpdata
= (unsigned char*)malloc(size
);
508 lseek( fd
, offset
, SEEK_SET
);
509 if( read(fd
, *lpdata
, size
) != size
)
510 { free( *lpdata
); *lpdata
= NULL
; }
518 int main(int argc
, char **argv
)
520 unsigned char* lpdata
= NULL
;
523 parse_options( argc
, argv
);
525 if( (fd
= open( g_lpstrInputFile
, O_RDONLY
)) )
528 struct stat file_stat
;
530 fstat( fd
, &file_stat
);
531 i
= get_resource_table( fd
, &lpdata
, file_stat
.st_size
);
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
)
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
;
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
);
571 lseek( fd
, offset
, SEEK_SET
);
572 if( read(fd
, lpfont
, length
) != length
)
574 fprintf(stderr
, errorDLLRead
);
578 if( (i
= parse_fnt_data( lpfont
, length
)) )
580 fprintf(stderr
, "%s%d\n", errorFontData
, i
);
584 free(lpfont
); free(lpdata
);
589 fprintf(stderr
, errorEmpty
);
596 fprintf(stderr
, errorDLLRead
);
604 if( (i
= parse_fnt_data( lpdata
, file_stat
.st_size
)) )
606 fprintf(stderr
, "%s%d\n", errorFontData
, i
);
613 fprintf(stderr
, errorFNTRead
);
619 fprintf(stderr
, errorFile
);
627 fprintf(stderr
, errorOpenFile
);