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>
45 enum data_types {dfChar, dfShort, dfLong, dfString};
48 #define ERROR_VERSION 2
50 #define ERROR_MEMORY 4
62 INT16 dfInternalLeading;
63 INT16 dfExternalLeading;
71 BYTE dfPitchAndFamily;
109 #define NE_FFLAGS_SINGLEDATA 0x0001
110 #define NE_FFLAGS_MULTIPLEDATA 0x0002
111 #define NE_FFLAGS_WIN32 0x0010
112 #define NE_FFLAGS_FRAMEBUF 0x0100
113 #define NE_FFLAGS_CONSOLE 0x0200
114 #define NE_FFLAGS_GUI 0x0300
115 #define NE_FFLAGS_SELFLOAD 0x0800
116 #define NE_FFLAGS_LINKERROR 0x2000
117 #define NE_FFLAGS_CALLWEP 0x4000
118 #define NE_FFLAGS_LIBMODULE 0x8000
120 #define NE_OSFLAGS_WINDOWS 0x02
122 #define NE_RSCTYPE_FONTDIR 0x8007
123 #define NE_RSCTYPE_FONT 0x8008
124 #define NE_RSCTYPE_SCALABLE_FONTPATH 0x80cc
126 #define NE_SEGFLAGS_DATA 0x0001
127 #define NE_SEGFLAGS_ALLOCATED 0x0002
128 #define NE_SEGFLAGS_LOADED 0x0004
129 #define NE_SEGFLAGS_ITERATED 0x0008
130 #define NE_SEGFLAGS_MOVEABLE 0x0010
131 #define NE_SEGFLAGS_SHAREABLE 0x0020
132 #define NE_SEGFLAGS_PRELOAD 0x0040
133 #define NE_SEGFLAGS_EXECUTEONLY 0x0080
134 #define NE_SEGFLAGS_READONLY 0x0080
135 #define NE_SEGFLAGS_RELOC_DATA 0x0100
136 #define NE_SEGFLAGS_SELFLOAD 0x0800
137 #define NE_SEGFLAGS_DISCARDABLE 0x1000
138 #define NE_SEGFLAGS_32BIT 0x2000
140 typedef struct tagFontHeader
142 SHORT dfVersion; /* Version */
143 LONG dfSize; /* Total File Size */
144 char dfCopyright[60]; /* Copyright notice */
145 FONTINFO16 fi; /* FONTINFO structure */
148 typedef struct WinCharStruct
150 unsigned int charWidth;
154 typedef struct fntFontStruct
157 WinCharS *dfCharTable;
158 unsigned char *dfDeviceP;
159 unsigned char *dfFaceP;
160 unsigned char *dfBitsPointerP;
161 unsigned char *dfBitsOffsetP;
162 short *dfColorTableP;
173 static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
174 static int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
176 static char* g_lpstrFileName = NULL;
177 static char* g_lpstrCharSet = NULL;
178 static char* g_lpstrInputFile = NULL;
179 static int g_outputPoints = 0;
183 static void usage(void)
185 printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
186 printf(" -c charset\tcharset name for OEM_CHARSET fonts\n");
187 printf(" -f basename\tbasic output filename\n");
188 printf(" -t \t\toutput files by point size instead of pixel height\n");
189 printf(" input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
190 printf("\nExample:\n fnt2bdf -c winsys vgasys.fnt\n\n");
194 /* convert little-endian value to the local format */
196 static int return_data_value(enum data_types dtype, void * pChr)
202 ret_val = (int) *(unsigned char *)pChr;
206 ret_val = *(unsigned char *)pChr;
207 ret_val += (*((unsigned char *)pChr + 1) << 8);
213 for(i=3; i >= 0; i--) {
214 ret_val += *((unsigned char *)pChr + i) << (8*i);
224 static int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
226 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
229 if( !g_lpstrFileName )
232 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
234 lpChar = (char*)(file_buffer + l_nameoffset);
236 else lpChar = g_lpstrFileName;
238 strcpy( name, lpChar );
240 while( (lpChar = strchr( name, ' ')) )
243 /* construct a filename from the font typeface, slant, weight, and size */
245 if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
246 else strcat(name, "_r" );
248 lpChar = name + strlen( name );
249 sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight),
250 (g_outputPoints) ? return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints)
251 : return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight) );
255 /* parse FONT resource and write .bdf file */
257 static int parse_fnt_data(unsigned char* file_buffer, int length)
259 fnt_fontS cpe_font_struct;
262 memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
264 /* check font header */
266 t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
267 if( t != 0x300 && t != 0x200) return ERROR_VERSION;
269 t = return_data_value(dfShort, &cpe_font_struct.hdr.fi.dfType);
272 fprintf(stderr, "Vector fonts not supported\n");
276 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
277 if( t > length ) return ERROR_SIZE;
280 /* set up the charWidth/charOffset structure pairs (dfCharTable)... */
282 int l_fchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfFirstChar),
283 l_lchar = return_data_value(dfChar, &cpe_font_struct.hdr.fi.dfLastChar);
284 int l_len = l_lchar - l_fchar + 1;
285 int l_ptr = sizeof(fnt_hdrS);
287 /* some fields were introduced for Windows 3.x fonts */
288 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200 )
291 /* malloc size = (# chars) * sizeof(WinCharS) */
293 if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
296 /* NOW, convert them all to UNIX (lton) notation... */
298 for(ic=0; ic < l_len; ic++) {
299 cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
300 l_ptr += 2; /* bump by sizeof(short) */
303 if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200) {
304 cpe_font_struct.dfCharTable[ic].charOffset =
305 return_data_value(dfShort, &file_buffer[l_ptr]);
306 l_ptr += 2; /* bump by sizeof(short) */
308 else { /* Windows Version 3.0 type font */
309 cpe_font_struct.dfCharTable[ic].charOffset =
310 return_data_value(dfLong, &file_buffer[l_ptr]);
311 l_ptr += 4; /* bump by sizeof(long) */
314 t = dump_bdf(&cpe_font_struct, file_buffer);
315 free( cpe_font_struct.dfCharTable );
320 static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
324 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
325 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
327 int l_len = l_lchar-l_fchar + 1,
328 l_hgt = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight);
329 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
330 char l_filename[256];
332 if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
335 if((fp = fopen(l_filename, "w")) == NULL)
337 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
341 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
347 /* NOW, convert all chars to UNIX (lton) notation... */
349 for(ic=0; ic < l_len; ic++) {
350 int rowidx, l_span, /* how many char-cols wide is char? */
351 l_idx = cpe_font_struct->dfCharTable[ic].charOffset;
353 l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
355 fprintf(fp, "STARTCHAR %d\n", ic);
356 fprintf(fp, "ENCODING %d\n", l_fchar);
357 fprintf(fp, "SWIDTH %d %d\n",
358 cpe_font_struct->dfCharTable[ic].charWidth*1000,
361 fprintf(fp, "DWIDTH %d %d\n",
362 cpe_font_struct->dfCharTable[ic].charWidth, 0);
364 fprintf(fp, "BBX %d %d %d %d\n",
365 cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
368 fprintf(fp, "BITMAP\n");
369 for(rowidx=0; rowidx < l_hgt; rowidx++) {
371 case(0): /* 1-7 pixels wide font */
373 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
377 case(1): /* 8-15 pixels wide font */
379 fprintf(fp, "%02X%02X",
380 (int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
385 case(2): /* 16-23 pixels wide font */
387 fprintf(fp, "%02X%02X%02X",
388 file_buffer[l_idx+rowidx],
389 file_buffer[l_idx+l_hgt+rowidx],
390 file_buffer[l_idx+(2*l_hgt)+rowidx]);
395 case(3): /* 24-31 pixels wide font */
397 fprintf(fp, "%02X%02X%02X%02X",
398 file_buffer[l_idx+rowidx],
399 file_buffer[l_idx+l_hgt+rowidx],
400 file_buffer[l_idx+(2*l_hgt)+rowidx],
401 file_buffer[l_idx+(3*l_hgt)+rowidx]);
407 fprintf(fp, "%02X%02X%02X%02X%02X",
408 file_buffer[l_idx+rowidx],
409 file_buffer[l_idx+l_hgt+rowidx],
410 file_buffer[l_idx+(2*l_hgt)+rowidx],
411 file_buffer[l_idx+(3*l_hgt)+rowidx],
412 file_buffer[l_idx+(4*l_hgt)+rowidx]);
422 fprintf(fp, "ENDCHAR\n");
424 l_fchar++; /* Go to next one */
426 fprintf(fp, "ENDFONT\n");
432 static int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
434 int l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
435 l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
436 int l_len = l_lchar - l_fchar + 1;
437 long l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
438 int l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
439 int l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
441 fprintf(fs, "STARTFONT 2.1\n");
443 /* Compose font name */
446 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
449 char* lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
450 short tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight);
452 while((lpChar = strchr(lpFace, '-')) )
455 fprintf(fs, "FONT -windows-%s-", lpFace );
457 if( tmWeight == 0 ) /* weight */
458 fputs("medium-", fs);
459 else if( tmWeight <= FW_LIGHT )
461 else if( tmWeight <= FW_MEDIUM )
462 fputs("medium-", fs);
463 else if( tmWeight <= FW_DEMIBOLD )
464 fputs("demibold-", fs);
465 else if( tmWeight <= FW_BOLD )
467 else fputs("black-", fs);
469 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
471 else fputs("r-", fs);
475 if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
476 fputs("normal-sans-", fs);
477 else fputs("normal--", fs); /* still can be -sans */
481 point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
482 dpi = (l_cellheight * 720) / point_size;
484 fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
485 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
486 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
490 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
491 else fputs("p-", fs);
495 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
499 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
501 switch( cpe_font_struct->hdr.fi.dfCharSet )
503 /* Microsoft just had to invent its own charsets! */
505 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
506 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
507 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
508 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
509 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
510 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
511 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
512 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
513 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
514 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
515 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
519 fputs("Undefined charset, use -c option.\n", stderr);
523 else return ERROR_DATA;
525 fprintf(fs, "SIZE %d %d %d\n",
526 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
527 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
528 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
530 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
531 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
532 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
533 0, l_ascent - l_cellheight );
535 fprintf(fs, "STARTPROPERTIES 4\n");
537 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
538 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
539 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
540 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
541 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
543 fprintf(fs, "ENDPROPERTIES\n");
545 fprintf(fs, "CHARS %d\n", l_len);
551 static void parse_options(int argc, char **argv)
558 g_lpstrInputFile = argv[1];
567 for( i = 1; i < argc - 1; i++ )
569 if( argv[i][0] != '-' ||
570 strlen(argv[i]) != 2 ) break;
572 if( argv[i][1] == 'c' )
573 g_lpstrCharSet = argv[i+1];
575 if( argv[i][1] == 'f' )
576 g_lpstrFileName = argv[i+1];
578 if( argv[i][1] == 't' )
590 g_lpstrInputFile = argv[i];
598 /* read file data and return file type */
600 static int get_resource_table(int fd, unsigned char** lpdata, int fsize)
602 IMAGE_DOS_HEADER mz_header;
603 IMAGE_OS2_HEADER ne_header;
604 long s, offset, size;
607 lseek( fd, 0, SEEK_SET );
609 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
612 s = return_data_value(dfShort, &mz_header.e_magic);
614 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
616 s = return_data_value(dfShort, &mz_header.e_lfanew);
617 lseek( fd, s, SEEK_SET );
619 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
622 s = return_data_value(dfShort, &ne_header.ne_magic);
624 if( s == IMAGE_NT_SIGNATURE)
626 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
629 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
631 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
632 size = return_data_value(dfShort, &ne_header.ne_restab);
634 if( size > fsize ) return FILE_ERROR;
637 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
639 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
642 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
644 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
646 if( size != fsize ) return FILE_ERROR;
650 else return FILE_ERROR;
652 *lpdata = (unsigned char*)malloc(size);
656 lseek( fd, offset, SEEK_SET );
657 if( read(fd, *lpdata, size) != size )
658 { free( *lpdata ); *lpdata = NULL; }
666 int main(int argc, char **argv)
668 unsigned char* lpdata = NULL;
671 parse_options( argc, argv);
673 if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) != -1 )
676 struct stat file_stat;
678 fstat( fd, &file_stat);
679 i = get_resource_table( fd, &lpdata, file_stat.st_size );
687 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
688 NE_NAMEINFO* pFontStorage = NULL;
690 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
692 j = return_data_value(dfShort, &pTInfo->count);
693 if( i == NE_RSCTYPE_FONT )
696 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
697 break; /* found one */
700 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
702 if( pFontStorage && count )
704 unsigned short size_shift = return_data_value(dfShort, lpdata);
705 unsigned char* lpfont = NULL;
709 for( j = 0; j < count; j++, pFontStorage++ )
711 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
712 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
714 if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
716 fprintf(stderr, "Memory allocation error.\n" );
720 lseek( fd, offset, SEEK_SET );
721 if( read(fd, lpfont, length) != length )
723 fprintf(stderr, "Unable to read Windows DLL.\n" );
727 if( (i = parse_fnt_data( lpfont, length )) )
729 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
733 free(lpfont); free(lpdata);
738 fprintf(stderr, "No fonts found.\n" );
745 fprintf(stderr, "Unable to read Windows DLL.\n" );
753 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
755 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
762 fprintf(stderr, "Unable to read .FNT file.\n" );
768 fprintf(stderr, "Corrupt or invalid file.\n" );
776 fprintf(stderr, "Unable to open '%s'.\n", g_lpstrInputFile );