msxml3: Register classes.
[wine/multimedia.git] / tools / fnt2bdf.c
blob89e24c805e20cc7d6009071e83f8446cf5310ded
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
23 #include "config.h"
24 #include "wine/port.h"
26 #ifdef HAVE_SYS_PARAM_H
27 # include <sys/param.h>
28 #endif
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_IO_H
39 # include <io.h>
40 #endif
42 #include "windef.h"
43 #include "wingdi.h"
45 enum data_types {dfChar, dfShort, dfLong, dfString};
47 #define ERROR_DATA 1
48 #define ERROR_VERSION 2
49 #define ERROR_SIZE 3
50 #define ERROR_MEMORY 4
51 #define ERROR_FILE 5
53 #include "pshpack1.h"
55 typedef struct
57 INT16 dfType;
58 INT16 dfPoints;
59 INT16 dfVertRes;
60 INT16 dfHorizRes;
61 INT16 dfAscent;
62 INT16 dfInternalLeading;
63 INT16 dfExternalLeading;
64 BYTE dfItalic;
65 BYTE dfUnderline;
66 BYTE dfStrikeOut;
67 INT16 dfWeight;
68 BYTE dfCharSet;
69 INT16 dfPixWidth;
70 INT16 dfPixHeight;
71 BYTE dfPitchAndFamily;
72 INT16 dfAvgWidth;
73 INT16 dfMaxWidth;
74 BYTE dfFirstChar;
75 BYTE dfLastChar;
76 BYTE dfDefaultChar;
77 BYTE dfBreakChar;
78 INT16 dfWidthBytes;
79 LONG dfDevice;
80 LONG dfFace;
81 LONG dfBitsPointer;
82 LONG dfBitsOffset;
83 BYTE dfReserved;
84 LONG dfFlags;
85 INT16 dfAspace;
86 INT16 dfBspace;
87 INT16 dfCspace;
88 LONG dfColorPointer;
89 LONG dfReserved1[4];
90 } FONTINFO16;
92 typedef struct
94 WORD offset;
95 WORD length;
96 WORD flags;
97 WORD id;
98 WORD handle;
99 WORD usage;
100 } NE_NAMEINFO;
102 typedef struct
104 WORD type_id;
105 WORD count;
106 DWORD resloader;
107 } NE_TYPEINFO;
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 */
146 } fnt_hdrS;
148 typedef struct WinCharStruct
150 unsigned int charWidth;
151 long charOffset;
152 } WinCharS;
154 typedef struct fntFontStruct
156 fnt_hdrS hdr;
157 WinCharS *dfCharTable;
158 unsigned char *dfDeviceP;
159 unsigned char *dfFaceP;
160 unsigned char *dfBitsPointerP;
161 unsigned char *dfBitsOffsetP;
162 short *dfColorTableP;
163 } fnt_fontS;
165 #include "poppack.h"
167 #define FILE_ERROR 0
168 #define FILE_DLL 1
169 #define FILE_FNT 2
171 /* global options */
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;
181 /* info */
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");
191 exit(-1);
194 /* convert little-endian value to the local format */
196 static int return_data_value(enum data_types dtype, void * pChr)
198 int ret_val = 0;
200 switch(dtype) {
201 case (dfChar):
202 ret_val = (int) *(unsigned char *)pChr;
203 break;
205 case(dfShort):
206 ret_val = *(unsigned char *)pChr;
207 ret_val += (*((unsigned char *)pChr + 1) << 8);
208 break;
210 case(dfLong): {
211 int i;
213 for(i=3; i >= 0; i--) {
214 ret_val += *((unsigned char *)pChr + i) << (8*i);
216 break;
218 case(dfString):
219 break;
221 return ret_val;
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);
227 char* lpChar;
229 if( !g_lpstrFileName )
231 if( !l_nameoffset ||
232 l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
233 return ERROR_DATA;
234 lpChar = (char*)(file_buffer + l_nameoffset);
236 else lpChar = g_lpstrFileName;
238 strcpy( name, lpChar );
240 while( (lpChar = strchr( name, ' ')) )
241 *lpChar = '-';
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) );
252 return 0;
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;
260 int ic=0, t;
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);
270 if (t & 1)
272 fprintf(stderr, "Vector fonts not supported\n");
273 return ERROR_DATA;
276 t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
277 if( t > length ) return ERROR_SIZE;
278 else
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 )
289 l_ptr -= 30;
291 /* malloc size = (# chars) * sizeof(WinCharS) */
293 if((cpe_font_struct.dfCharTable = calloc(sizeof(WinCharS), l_len)) == NULL)
294 return ERROR_MEMORY;
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 );
317 return t;
320 static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
322 FILE* fp;
323 int ic;
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)) )
333 return ic;
335 if((fp = fopen(l_filename, "w")) == NULL)
337 fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
338 return ERROR_FILE;
341 ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
342 if (ic) {
343 fclose(fp);
344 return (ic);
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,
366 l_ascent - l_hgt);
368 fprintf(fp, "BITMAP\n");
369 for(rowidx=0; rowidx < l_hgt; rowidx++) {
370 switch(l_span) {
371 case(0): /* 1-7 pixels wide font */
373 fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
374 break;
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]);
381 fprintf(fp, "\n");
382 break;
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]);
391 fprintf(fp, "\n");
392 break;
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]);
402 fprintf(fp, "\n");
403 break;
405 case(4): /* 32-39 */
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]);
413 fprintf(fp, "\n");
414 break;
416 default:
417 fclose(fp);
418 unlink(l_filename);
419 return ERROR_DATA;
422 fprintf(fp, "ENDCHAR\n");
424 l_fchar++; /* Go to next one */
426 fprintf(fp, "ENDFONT\n");
427 fclose(fp);
428 return 0;
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 */
445 if( l_nameoffset &&
446 l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
448 int dpi, point_size;
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, '-')) )
453 *lpChar = ' ';
455 fprintf(fs, "FONT -windows-%s-", lpFace );
457 if( tmWeight == 0 ) /* weight */
458 fputs("medium-", fs);
459 else if( tmWeight <= FW_LIGHT )
460 fputs("light-", fs);
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 )
466 fputs("bold-", fs);
467 else fputs("black-", fs);
469 if( cpe_font_struct->hdr.fi.dfItalic ) /* slant */
470 fputs("i-", fs);
471 else fputs("r-", fs);
473 /* style */
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 */
479 /* y extents */
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));
488 /* spacing */
490 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
491 else fputs("p-", fs);
493 /* average width */
495 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
497 /* charset */
499 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
500 else
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;
517 default:
518 case OEM_CHARSET:
519 fputs("Undefined charset, use -c option.\n", stderr);
520 return ERROR_DATA;
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);
546 return 0;
551 static void parse_options(int argc, char **argv)
553 int i;
555 switch( argc )
557 case 2:
558 g_lpstrInputFile = argv[1];
559 break;
561 case 3:
562 case 4:
563 case 5:
564 case 6:
565 case 7:
566 case 8:
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];
574 else
575 if( argv[i][1] == 'f' )
576 g_lpstrFileName = argv[i+1];
577 else
578 if( argv[i][1] == 't' )
580 g_outputPoints = 1;
581 continue;
583 else
584 usage();
586 i++;
588 if( i == argc - 1 )
590 g_lpstrInputFile = argv[i];
591 break;
593 default: usage();
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;
605 int retval;
607 lseek( fd, 0, SEEK_SET );
609 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
610 return FILE_ERROR;
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) )
620 return FILE_ERROR;
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");
627 return FILE_ERROR;
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;
636 size -= s;
637 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
639 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
640 retval = FILE_DLL;
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;
647 offset = 0;
648 retval = FILE_FNT;
650 else return FILE_ERROR;
652 *lpdata = malloc(size);
654 if( *lpdata )
656 lseek( fd, offset, SEEK_SET );
657 if( read(fd, *lpdata, size) != size )
658 { free( *lpdata ); *lpdata = NULL; }
660 return retval;
664 /* entry point */
666 int main(int argc, char **argv)
668 unsigned char* lpdata = NULL;
669 int fd;
671 parse_options( argc, argv);
673 if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) != -1 )
675 int i;
676 struct stat file_stat;
678 fstat( fd, &file_stat);
679 i = get_resource_table( fd, &lpdata, file_stat.st_size );
681 switch(i)
683 case FILE_DLL:
684 if( lpdata )
686 int j, count = 0;
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 )
695 count = j;
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;
706 unsigned offset;
707 int length;
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 = realloc( lpfont, length )) )
716 fprintf(stderr, "Memory allocation error.\n" );
717 exit(1);
720 lseek( fd, offset, SEEK_SET );
721 if( read(fd, lpfont, length) != length )
723 fprintf(stderr, "Unable to read Windows DLL.\n" );
724 exit(1);
727 if( (i = parse_fnt_data( lpfont, length )) )
729 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
730 exit(1);
733 free(lpfont); free(lpdata);
734 exit(0);
736 else
738 fprintf(stderr, "No fonts found.\n" );
739 exit(1);
741 free( lpdata );
743 else
745 fprintf(stderr, "Unable to read Windows DLL.\n" );
746 exit(1);
748 break;
750 case FILE_FNT:
751 if( lpdata )
753 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
755 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
756 exit(1);
758 free( lpdata );
760 else
762 fprintf(stderr, "Unable to read .FNT file.\n" );
763 exit(1);
765 break;
767 case FILE_ERROR:
768 fprintf(stderr, "Corrupt or invalid file.\n" );
769 exit(1);
771 close(fd);
772 exit(0);
774 else
776 fprintf(stderr, "Unable to open '%s'.\n", g_lpstrInputFile );
777 exit(1);