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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifdef HAVE_SYS_PARAM_H
26 # include <sys/param.h>
28 #include <sys/types.h>
45 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
46 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
48 char* g_lpstrFileName = NULL;
49 char* g_lpstrCharSet = NULL;
50 char* g_lpstrInputFile = NULL;
51 int g_outputPoints = 0;
53 static char* errorDLLRead = "Unable to read Windows DLL.\n";
54 static char* errorFNTRead = "Unable to read .FNT file.\n";
55 static char* errorOpenFile = "Unable to open file.\n";
56 static char* errorMemory = "Memory allocation error.\n";
57 static char* errorFile = "Corrupt or invalid file.\n";
58 static char* errorFontData = "Unable to parse font data: Error ";
59 static char* errorEmpty = "No fonts found.\n";
65 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
66 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
67 printf(" -f basename\tbasic output filename\n");
68 printf(" -t \t\toutput files by point size instead of pixel height\n");
69 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
70 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
74 /* convert little-endian value to the local format */
76 int return_data_value(enum data_types dtype, void * pChr)
82 ret_val = (int) *(unsigned char *)pChr;
86 ret_val = *(unsigned char *)pChr;
87 ret_val += (*((unsigned char *)pChr + 1) << 8);
93 for(i=3; i >= 0; i--) {
94 ret_val += *((unsigned char *)pChr + i) << (8*i);
104 int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
106 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
109 if( !g_lpstrFileName )
112 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
114 lpChar = (char*)(file_buffer + l_nameoffset);
116 else lpChar = g_lpstrFileName;
118 strcpy( name, lpChar );
120 while( (lpChar = strchr( name, ' ')) )
123 /* construct a filename from the font typeface, slant, weight, and size */
125 if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
126 else strcat(name, "_r" );
128 lpChar = name + strlen( name );
129 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight),
130 (g_outputPoints) ? return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints)
131 : return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight) );
135 /* parse FONT resource and write .bdf file */
137 int parse_fnt_data(unsigned char* file_buffer, int length)
139 fnt_fontS cpe_font_struct;
142 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
144 /* check font header */
146 t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
147 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
149 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
150 if( t > length ) return ERROR_SIZE;
153 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
155 int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar),
156 l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar);
157 int l_len = l_lchar - l_fchar + 1;
158 int l_ptr = sizeof(fnt_hdrS);
160 /* some fields were introduced for Windows 3.x fonts */
161 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200 )
164 /* malloc size = (# chars) * sizeof(WinCharS) */
166 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
169 /* NOW, convert them all to UNIX (lton) notation... */
171 for(ic=0; ic < l_len; ic++) {
172 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
173 l_ptr += 2; /* bump by sizeof(short) */
176 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200) {
177 cpe_font_struct.dfCharTable[ic].charOffset =
178 return_data_value(dfShort, &file_buffer[l_ptr]);
179 l_ptr += 2; /* bump by sizeof(long) */
181 else { /* Windows Version 3.0 type font */
182 cpe_font_struct.dfCharTable[ic].charOffset =
183 return_data_value(dfLong, &file_buffer[l_ptr]);
184 l_ptr += 4; /* bump by sizeof(long) */
187 t = dump_bdf(&cpe_font_struct, file_buffer);
188 free( cpe_font_struct.dfCharTable );
193 int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
197 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
198 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
200 int l_len = l_lchar-l_fchar + 1,
201 l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight);
202 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
203 char l_filename[256];
205 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
208 if((fp = fopen(l_filename, "w")) == (FILE *) 0)
210 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
214 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
217 /* NOW, convert all chars to UNIX (lton) notation... */
219 for(ic=0; ic < l_len; ic++) {
220 int rowidx, l_span, /* how many char-cols wide is char? */
221 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
223 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
225 fprintf(fp, "STARTCHAR %d \n", ic);
226 fprintf(fp, "ENCODING %d\n", l_fchar);
227 fprintf(fp, "SWIDTH %d %d \n",
228 cpe_font_struct->dfCharTable[ic].charWidth*1000,
231 fprintf(fp, "DWIDTH %d %d \n",
232 cpe_font_struct->dfCharTable[ic].charWidth, 0);
234 fprintf(fp, "BBX %d %d %d %d\n",
235 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
238 fprintf(fp, "BITMAP\n");
239 for(rowidx=0; rowidx < l_hgt; rowidx++) {
241 case(0): /* 1-7 pixels wide font */
243 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
247 case(1): /* 8-15 pixels wide font */
249 fprintf(fp, "%02X%02X",
250 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
255 case(2): /* 16-23 pixels wide font */
257 fprintf(fp, "%02X%02X%02X",
258 file_buffer[l_idx+rowidx],
259 file_buffer[l_idx+l_hgt+rowidx],
260 file_buffer[l_idx+(2*l_hgt)+rowidx]);
265 case(3): /* 24-31 pixels wide font */
267 fprintf(fp, "%02X%02X%02X%02X",
268 file_buffer[l_idx+rowidx],
269 file_buffer[l_idx+l_hgt+rowidx],
270 file_buffer[l_idx+(2*l_hgt)+rowidx],
271 file_buffer[l_idx+(3*l_hgt)+rowidx]);
277 fprintf(fp, "%02X%02X%02X%02X%02X",
278 file_buffer[l_idx+rowidx],
279 file_buffer[l_idx+l_hgt+rowidx],
280 file_buffer[l_idx+(2*l_hgt)+rowidx],
281 file_buffer[l_idx+(3*l_hgt)+rowidx],
282 file_buffer[l_idx+(4*l_hgt)+rowidx]);
292 fprintf(fp, "ENDCHAR\n");
294 l_fchar++; /* Go to next one */
296 fprintf(fp, "ENDFONT\n");
302 int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
304 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
305 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
306 int l_len = l_lchar - l_fchar + 1;
307 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
308 int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
309 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
311 fprintf(fs, "STARTFONT 2.1\n");
313 /* Compose font name */
316 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
319 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
320 short tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight);
322 while((lpChar = strchr(lpFace, '-')) )
325 fprintf(fs, "FONT -windows-%s-", lpFace );
327 if( tmWeight == 0 ) /* weight */
328 fputs("medium-", fs);
329 else if( tmWeight <= FW_LIGHT )
331 else if( tmWeight <= FW_MEDIUM )
332 fputs("medium-", fs);
333 else if( tmWeight <= FW_DEMIBOLD )
334 fputs("demibold-", fs);
335 else if( tmWeight <= FW_BOLD )
337 else fputs("black-", fs);
339 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
341 else fputs("r-", fs);
345 if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
346 fputs("normal-sans-", fs);
347 else fputs("normal--", fs); /* still can be -sans */
351 point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
352 dpi = (l_cellheight * 720) / point_size;
354 fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
355 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
356 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
360 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
361 else fputs("p-", fs);
365 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
369 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
371 switch( cpe_font_struct->hdr.fi.dfCharSet )
373 /* Microsoft just had to invent its own charsets! */
375 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
376 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
377 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
378 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
379 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
380 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
381 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
382 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
383 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
384 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
385 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
389 fputs("Undefined charset, use -c option.\n", stderr);
393 else return ERROR_DATA;
395 fprintf(fs, "SIZE %d %d %d\n",
396 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
397 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
398 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
400 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
401 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
402 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
403 0, l_ascent - l_cellheight );
405 fprintf(fs, "STARTPROPERTIES 4\n");
407 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
408 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
409 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
410 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
411 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
413 fprintf(fs, "ENDPROPERTIES\n");
415 fprintf(fs, "CHARS %d\n", l_len);
421 void parse_options(int argc, char **argv)
428 g_lpstrInputFile = argv[1];
437 for( i = 1; i < argc - 1; i++ )
439 if( argv[i][0] != '-' ||
440 strlen(argv[i]) != 2 ) break;
442 if( argv[i][1] == 'c' )
443 g_lpstrCharSet = argv[i+1];
445 if( argv[i][1] == 'f' )
446 g_lpstrFileName = argv[i+1];
448 if( argv[i][1] == 't' )
460 g_lpstrInputFile = argv[i];
468 /* read file data and return file type */
470 int get_resource_table(int fd, unsigned char** lpdata, int fsize)
472 IMAGE_DOS_HEADER mz_header;
473 IMAGE_OS2_HEADER ne_header;
474 long s, offset, size;
477 lseek( fd, 0, SEEK_SET );
479 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
482 s = return_data_value(dfShort, &mz_header.e_magic);
484 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
486 s = return_data_value(dfShort, &mz_header.e_lfanew);
487 lseek( fd, s, SEEK_SET );
489 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
492 s = return_data_value(dfShort, &ne_header.ne_magic);
494 if( s == IMAGE_NT_SIGNATURE)
496 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
499 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
501 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
502 size = return_data_value(dfShort, &ne_header.ne_restab);
504 if( size > fsize ) return FILE_ERROR;
507 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
509 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
512 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
514 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
516 if( size != fsize ) return FILE_ERROR;
520 else return FILE_ERROR;
522 *lpdata = (unsigned char*)malloc(size);
526 lseek( fd, offset, SEEK_SET );
527 if( read(fd, *lpdata, size) != size )
528 { free( *lpdata ); *lpdata = NULL; }
536 int main(int argc, char **argv)
538 unsigned char* lpdata = NULL;
541 parse_options( argc, argv);
543 if( (fd = open( g_lpstrInputFile, O_RDONLY)) )
546 struct stat file_stat;
548 fstat( fd, &file_stat);
549 i = get_resource_table( fd, &lpdata, file_stat.st_size );
557 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
558 NE_NAMEINFO* pFontStorage = NULL;
560 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
562 j = return_data_value(dfShort, &pTInfo->count);
563 if( i == NE_RSCTYPE_FONT )
566 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
567 break; /* found one */
570 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
572 if( pFontStorage && count )
574 unsigned short size_shift = return_data_value(dfShort, lpdata);
575 unsigned char* lpfont = NULL;
579 for( j = 0; j < count; j++, pFontStorage++ )
581 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
582 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
584 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
586 fprintf(stderr, errorMemory );
590 lseek( fd, offset, SEEK_SET );
591 if( read(fd, lpfont, length) != length )
593 fprintf(stderr, errorDLLRead );
597 if( (i = parse_fnt_data( lpfont, length )) )
599 fprintf(stderr, "%s%d\n", errorFontData, i );
603 free(lpfont); free(lpdata);
608 fprintf(stderr, errorEmpty );
615 fprintf(stderr, errorDLLRead);
623 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
625 fprintf(stderr, "%s%d\n", errorFontData, i );
632 fprintf(stderr, errorFNTRead);
638 fprintf(stderr, errorFile );
646 fprintf(stderr, errorOpenFile );