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
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #ifdef HAVE_SYS_PARAM_H
27 # include <sys/param.h>
29 #include <sys/types.h>
50 int dump_bdf( fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
);
51 int dump_bdf_hdr(FILE* fs
, fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
);
53 char* g_lpstrFileName
= NULL
;
54 char* g_lpstrCharSet
= NULL
;
55 char* g_lpstrInputFile
= NULL
;
56 int g_outputPoints
= 0;
58 static const char* errorDLLRead
= "Unable to read Windows DLL.\n";
59 static const char* errorFNTRead
= "Unable to read .FNT file.\n";
60 static const char* errorOpenFile
= "Unable to open file.\n";
61 static const char* errorMemory
= "Memory allocation error.\n";
62 static const char* errorFile
= "Corrupt or invalid file.\n";
63 static const char* errorFontData
= "Unable to parse font data: Error ";
64 static const char* errorEmpty
= "No fonts found.\n";
68 static void usage(void)
70 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
71 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
72 printf(" -f basename\tbasic output filename\n");
73 printf(" -t \t\toutput files by point size instead of pixel height\n");
74 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
75 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
79 /* convert little-endian value to the local format */
81 static int return_data_value(enum data_types dtype
, void * pChr
)
87 ret_val
= (int) *(unsigned char *)pChr
;
91 ret_val
= *(unsigned char *)pChr
;
92 ret_val
+= (*((unsigned char *)pChr
+ 1) << 8);
98 for(i
=3; i
>= 0; i
--) {
99 ret_val
+= *((unsigned char *)pChr
+ i
) << (8*i
);
109 static int make_bdf_filename(char* name
, fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
)
111 long l_nameoffset
= return_data_value(dfLong
, &cpe_font_struct
->hdr
.fi
.dfFace
);
114 if( !g_lpstrFileName
)
117 l_nameoffset
> return_data_value(dfLong
, &cpe_font_struct
->hdr
.dfSize
) + 1 )
119 lpChar
= (char*)(file_buffer
+ l_nameoffset
);
121 else lpChar
= g_lpstrFileName
;
123 strcpy( name
, lpChar
);
125 while( (lpChar
= strchr( name
, ' ')) )
128 /* construct a filename from the font typeface, slant, weight, and size */
130 if( cpe_font_struct
->hdr
.fi
.dfItalic
) strcat(name
, "_i" );
131 else strcat(name
, "_r" );
133 lpChar
= name
+ strlen( name
);
134 sprintf(lpChar
, "%d-%d.bdf", return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfWeight
),
135 (g_outputPoints
) ? return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfPoints
)
136 : return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfPixHeight
) );
140 /* parse FONT resource and write .bdf file */
142 static int parse_fnt_data(unsigned char* file_buffer
, int length
)
144 fnt_fontS cpe_font_struct
;
147 memcpy((char *) &cpe_font_struct
.hdr
, file_buffer
, sizeof(fnt_hdrS
));
149 /* check font header */
151 t
= return_data_value(dfShort
, &cpe_font_struct
.hdr
.dfVersion
);
152 if( t
!= 0x300 && t
!= 0x200) return ERROR_VERSION
;
154 t
= return_data_value(dfShort
, &cpe_font_struct
.hdr
.fi
.dfType
);
157 fprintf(stderr
, "Vector fonts not supported\n");
161 t
= return_data_value(dfLong
, &cpe_font_struct
.hdr
.dfSize
);
162 if( t
> length
) return ERROR_SIZE
;
165 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
167 int l_fchar
= return_data_value(dfChar
, &cpe_font_struct
.hdr
.fi
.dfFirstChar
),
168 l_lchar
= return_data_value(dfChar
, &cpe_font_struct
.hdr
.fi
.dfLastChar
);
169 int l_len
= l_lchar
- l_fchar
+ 1;
170 int l_ptr
= sizeof(fnt_hdrS
);
172 /* some fields were introduced for Windows 3.x fonts */
173 if( return_data_value(dfShort
, &cpe_font_struct
.hdr
.dfVersion
) == 0x200 )
176 /* malloc size = (# chars) * sizeof(WinCharS) */
178 if((cpe_font_struct
.dfCharTable
= (WinCharS
*) calloc(sizeof(WinCharS
), l_len
)) == NULL
)
181 /* NOW, convert them all to UNIX (lton) notation... */
183 for(ic
=0; ic
< l_len
; ic
++) {
184 cpe_font_struct
.dfCharTable
[ic
].charWidth
= return_data_value(dfShort
, &file_buffer
[l_ptr
]);
185 l_ptr
+= 2; /* bump by sizeof(short) */
188 if( return_data_value(dfShort
, &cpe_font_struct
.hdr
.dfVersion
) == 0x200) {
189 cpe_font_struct
.dfCharTable
[ic
].charOffset
=
190 return_data_value(dfShort
, &file_buffer
[l_ptr
]);
191 l_ptr
+= 2; /* bump by sizeof(short) */
193 else { /* Windows Version 3.0 type font */
194 cpe_font_struct
.dfCharTable
[ic
].charOffset
=
195 return_data_value(dfLong
, &file_buffer
[l_ptr
]);
196 l_ptr
+= 4; /* bump by sizeof(long) */
199 t
= dump_bdf(&cpe_font_struct
, file_buffer
);
200 free( cpe_font_struct
.dfCharTable
);
205 int dump_bdf( fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
)
209 int l_fchar
= return_data_value(dfChar
, &cpe_font_struct
->hdr
.fi
.dfFirstChar
),
210 l_lchar
= return_data_value(dfChar
, &cpe_font_struct
->hdr
.fi
.dfLastChar
);
212 int l_len
= l_lchar
-l_fchar
+ 1,
213 l_hgt
= return_data_value(dfChar
, &cpe_font_struct
->hdr
.fi
.dfPixHeight
);
214 int l_ascent
= return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfAscent
);
215 char l_filename
[256];
217 if( (ic
= make_bdf_filename(l_filename
, cpe_font_struct
, file_buffer
)) )
220 if((fp
= fopen(l_filename
, "w")) == (FILE *) 0)
222 fprintf(stderr
, "Couldn't open \"%s\" for output.\n", l_filename
);
226 ic
= dump_bdf_hdr(fp
, cpe_font_struct
, file_buffer
);
229 /* NOW, convert all chars to UNIX (lton) notation... */
231 for(ic
=0; ic
< l_len
; ic
++) {
232 int rowidx
, l_span
, /* how many char-cols wide is char? */
233 l_idx
= cpe_font_struct
->dfCharTable
[ic
].charOffset
;
235 l_span
= (int) (cpe_font_struct
->dfCharTable
[ic
].charWidth
-1)/8;
237 fprintf(fp
, "STARTCHAR %d \n", ic
);
238 fprintf(fp
, "ENCODING %d\n", l_fchar
);
239 fprintf(fp
, "SWIDTH %d %d \n",
240 cpe_font_struct
->dfCharTable
[ic
].charWidth
*1000,
243 fprintf(fp
, "DWIDTH %d %d \n",
244 cpe_font_struct
->dfCharTable
[ic
].charWidth
, 0);
246 fprintf(fp
, "BBX %d %d %d %d\n",
247 cpe_font_struct
->dfCharTable
[ic
].charWidth
, l_hgt
, 0,
250 fprintf(fp
, "BITMAP\n");
251 for(rowidx
=0; rowidx
< l_hgt
; rowidx
++) {
253 case(0): /* 1-7 pixels wide font */
255 fprintf(fp
, "%02X\n", (int) file_buffer
[l_idx
+rowidx
]);
259 case(1): /* 8-15 pixels wide font */
261 fprintf(fp
, "%02X%02X",
262 (int) file_buffer
[l_idx
+rowidx
], file_buffer
[l_idx
+l_hgt
+rowidx
]);
267 case(2): /* 16-23 pixels wide font */
269 fprintf(fp
, "%02X%02X%02X",
270 file_buffer
[l_idx
+rowidx
],
271 file_buffer
[l_idx
+l_hgt
+rowidx
],
272 file_buffer
[l_idx
+(2*l_hgt
)+rowidx
]);
277 case(3): /* 24-31 pixels wide font */
279 fprintf(fp
, "%02X%02X%02X%02X",
280 file_buffer
[l_idx
+rowidx
],
281 file_buffer
[l_idx
+l_hgt
+rowidx
],
282 file_buffer
[l_idx
+(2*l_hgt
)+rowidx
],
283 file_buffer
[l_idx
+(3*l_hgt
)+rowidx
]);
289 fprintf(fp
, "%02X%02X%02X%02X%02X",
290 file_buffer
[l_idx
+rowidx
],
291 file_buffer
[l_idx
+l_hgt
+rowidx
],
292 file_buffer
[l_idx
+(2*l_hgt
)+rowidx
],
293 file_buffer
[l_idx
+(3*l_hgt
)+rowidx
],
294 file_buffer
[l_idx
+(4*l_hgt
)+rowidx
]);
304 fprintf(fp
, "ENDCHAR\n");
306 l_fchar
++; /* Go to next one */
308 fprintf(fp
, "ENDFONT\n");
314 int dump_bdf_hdr(FILE* fs
, fnt_fontS
* cpe_font_struct
, unsigned char* file_buffer
)
316 int l_fchar
= return_data_value(dfChar
, &cpe_font_struct
->hdr
.fi
.dfFirstChar
),
317 l_lchar
= return_data_value(dfChar
, &cpe_font_struct
->hdr
.fi
.dfLastChar
);
318 int l_len
= l_lchar
- l_fchar
+ 1;
319 long l_nameoffset
= return_data_value(dfLong
, &cpe_font_struct
->hdr
.fi
.dfFace
);
320 int l_cellheight
= return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfPixHeight
);
321 int l_ascent
= return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfAscent
);
323 fprintf(fs
, "STARTFONT 2.1\n");
325 /* Compose font name */
328 l_nameoffset
< return_data_value(dfLong
, &cpe_font_struct
->hdr
.dfSize
) )
331 char* lpFace
= (char*)(file_buffer
+ l_nameoffset
), *lpChar
;
332 short tmWeight
= return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfWeight
);
334 while((lpChar
= strchr(lpFace
, '-')) )
337 fprintf(fs
, "FONT -windows-%s-", lpFace
);
339 if( tmWeight
== 0 ) /* weight */
340 fputs("medium-", fs
);
341 else if( tmWeight
<= FW_LIGHT
)
343 else if( tmWeight
<= FW_MEDIUM
)
344 fputs("medium-", fs
);
345 else if( tmWeight
<= FW_DEMIBOLD
)
346 fputs("demibold-", fs
);
347 else if( tmWeight
<= FW_BOLD
)
349 else fputs("black-", fs
);
351 if( cpe_font_struct
->hdr
.fi
.dfItalic
) /* slant */
353 else fputs("r-", fs
);
357 if( (cpe_font_struct
->hdr
.fi
.dfPitchAndFamily
& 0xF0) == FF_SWISS
)
358 fputs("normal-sans-", fs
);
359 else fputs("normal--", fs
); /* still can be -sans */
363 point_size
= 10 * return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfPoints
);
364 dpi
= (l_cellheight
* 720) / point_size
;
366 fprintf(fs
, "%d-%d-%d-%d-",l_cellheight
, point_size
,
367 return_data_value (dfShort
, &cpe_font_struct
->hdr
.fi
.dfHorizRes
),
368 return_data_value (dfShort
, &cpe_font_struct
->hdr
.fi
.dfVertRes
));
372 if( return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfPixWidth
) ) fputs("c-", fs
);
373 else fputs("p-", fs
);
377 fprintf( fs
, "%d-", 10 * return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfAvgWidth
) );
381 if( g_lpstrCharSet
) fprintf(fs
, "%s\n", g_lpstrCharSet
);
383 switch( cpe_font_struct
->hdr
.fi
.dfCharSet
)
385 /* Microsoft just had to invent its own charsets! */
387 case ANSI_CHARSET
: fputs("microsoft-cp1252\n", fs
); break;
388 case GREEK_CHARSET
: fputs("microsoft-cp1253\n", fs
); break;
389 case TURKISH_CHARSET
: fputs("microsoft-cp1254\n", fs
); break;
390 case HEBREW_CHARSET
: fputs("microsoft-cp1255\n", fs
); break;
391 case ARABIC_CHARSET
: fputs("microsoft-cp1256\n", fs
); break;
392 case BALTIC_CHARSET
: fputs("microsoft-cp1257\n", fs
); break;
393 case RUSSIAN_CHARSET
: fputs("microsoft-cp1251\n", fs
); break;
394 case EE_CHARSET
: fputs("microsoft-cp1250\n", fs
); break;
395 case SYMBOL_CHARSET
: fputs("microsoft-symbol\n", fs
); break;
396 case SHIFTJIS_CHARSET
: fputs("jisx0208.1983-0\n", fs
); break;
397 case DEFAULT_CHARSET
: fputs("iso8859-1\n", fs
); break;
401 fputs("Undefined charset, use -c option.\n", stderr
);
405 else return ERROR_DATA
;
407 fprintf(fs
, "SIZE %d %d %d\n",
408 return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfPoints
),
409 return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfHorizRes
),
410 return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfVertRes
)); /* dfVertRes[2] */
412 fprintf(fs
, "FONTBOUNDINGBOX %d %d %d %d\n",
413 return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfMaxWidth
),
414 return_data_value(dfChar
, &cpe_font_struct
->hdr
.fi
.dfPixHeight
),
415 0, l_ascent
- l_cellheight
);
417 fprintf(fs
, "STARTPROPERTIES 4\n");
419 fprintf(fs
, "FONT_ASCENT %d\n", l_ascent
); /* dfAscent[2] */
420 fprintf(fs
, "FONT_DESCENT %d\n", l_cellheight
- l_ascent
);
421 fprintf(fs
, "CAP_HEIGHT %d\n", l_ascent
-
422 return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfInternalLeading
));
423 fprintf(fs
, "DEFAULT_CHAR %d\n", return_data_value(dfShort
, &cpe_font_struct
->hdr
.fi
.dfDefaultChar
));
425 fprintf(fs
, "ENDPROPERTIES\n");
427 fprintf(fs
, "CHARS %d\n", l_len
);
433 static void parse_options(int argc
, char **argv
)
440 g_lpstrInputFile
= argv
[1];
449 for( i
= 1; i
< argc
- 1; i
++ )
451 if( argv
[i
][0] != '-' ||
452 strlen(argv
[i
]) != 2 ) break;
454 if( argv
[i
][1] == 'c' )
455 g_lpstrCharSet
= argv
[i
+1];
457 if( argv
[i
][1] == 'f' )
458 g_lpstrFileName
= argv
[i
+1];
460 if( argv
[i
][1] == 't' )
472 g_lpstrInputFile
= argv
[i
];
480 /* read file data and return file type */
482 static int get_resource_table(int fd
, unsigned char** lpdata
, int fsize
)
484 IMAGE_DOS_HEADER mz_header
;
485 IMAGE_OS2_HEADER ne_header
;
486 long s
, offset
, size
;
489 lseek( fd
, 0, SEEK_SET
);
491 if( read(fd
, &mz_header
, sizeof(mz_header
)) != sizeof(mz_header
) )
494 s
= return_data_value(dfShort
, &mz_header
.e_magic
);
496 if( s
== IMAGE_DOS_SIGNATURE
) /* looks like .dll file so far... */
498 s
= return_data_value(dfShort
, &mz_header
.e_lfanew
);
499 lseek( fd
, s
, SEEK_SET
);
501 if( read(fd
, &ne_header
, sizeof(ne_header
)) != sizeof(ne_header
) )
504 s
= return_data_value(dfShort
, &ne_header
.ne_magic
);
506 if( s
== IMAGE_NT_SIGNATURE
)
508 fprintf( stderr
, "Do not know how to handle 32-bit Windows DLLs.\n");
511 else if ( s
!= IMAGE_OS2_SIGNATURE
) return FILE_ERROR
;
513 s
= return_data_value(dfShort
, &ne_header
.ne_rsrctab
);
514 size
= return_data_value(dfShort
, &ne_header
.ne_restab
);
516 if( size
> fsize
) return FILE_ERROR
;
519 offset
= s
+ return_data_value(dfShort
, &mz_header
.e_lfanew
);
521 if( size
<= sizeof(NE_TYPEINFO
) ) return FILE_ERROR
;
524 else if( s
== 0x300 || s
== 0x200 ) /* maybe .fnt ? */
526 size
= return_data_value(dfLong
, &((fnt_hdrS
*)&mz_header
)->dfSize
);
528 if( size
!= fsize
) return FILE_ERROR
;
532 else return FILE_ERROR
;
534 *lpdata
= (unsigned char*)malloc(size
);
538 lseek( fd
, offset
, SEEK_SET
);
539 if( read(fd
, *lpdata
, size
) != size
)
540 { free( *lpdata
); *lpdata
= NULL
; }
548 int main(int argc
, char **argv
)
550 unsigned char* lpdata
= NULL
;
553 parse_options( argc
, argv
);
555 if( (fd
= open( g_lpstrInputFile
, O_RDONLY
| O_BINARY
)) )
558 struct stat file_stat
;
560 fstat( fd
, &file_stat
);
561 i
= get_resource_table( fd
, &lpdata
, file_stat
.st_size
);
569 NE_TYPEINFO
* pTInfo
= (NE_TYPEINFO
*)(lpdata
+ 2);
570 NE_NAMEINFO
* pFontStorage
= NULL
;
572 while( (i
= return_data_value(dfShort
, &pTInfo
->type_id
)) )
574 j
= return_data_value(dfShort
, &pTInfo
->count
);
575 if( i
== NE_RSCTYPE_FONT
)
578 pFontStorage
= (NE_NAMEINFO
*)(pTInfo
+ 1);
579 break; /* found one */
582 pTInfo
= (NE_TYPEINFO
*)((char*)(pTInfo
+1) + j
*sizeof(NE_NAMEINFO
));
584 if( pFontStorage
&& count
)
586 unsigned short size_shift
= return_data_value(dfShort
, lpdata
);
587 unsigned char* lpfont
= NULL
;
591 for( j
= 0; j
< count
; j
++, pFontStorage
++ )
593 length
= return_data_value(dfShort
, &pFontStorage
->length
) << size_shift
;
594 offset
= return_data_value(dfShort
, &pFontStorage
->offset
) << size_shift
;
596 if( !(lpfont
= (unsigned char*) realloc( lpfont
, length
)) )
598 fprintf(stderr
, errorMemory
);
602 lseek( fd
, offset
, SEEK_SET
);
603 if( read(fd
, lpfont
, length
) != length
)
605 fprintf(stderr
, errorDLLRead
);
609 if( (i
= parse_fnt_data( lpfont
, length
)) )
611 fprintf(stderr
, "%s%d\n", errorFontData
, i
);
615 free(lpfont
); free(lpdata
);
620 fprintf(stderr
, errorEmpty
);
627 fprintf(stderr
, errorDLLRead
);
635 if( (i
= parse_fnt_data( lpdata
, file_stat
.st_size
)) )
637 fprintf(stderr
, "%s%d\n", errorFontData
, i
);
644 fprintf(stderr
, errorFNTRead
);
650 fprintf(stderr
, errorFile
);
658 fprintf(stderr
, errorOpenFile
);