wined3d: Only unload the surface when we're using GL for rendering in updateSurfaceDe...
[wine/multimedia.git] / tools / fnt2bdf.c
blob4dc20e55844ce720ab4595253ef60bd1acdff01f
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 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 );
483 fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
484 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
485 return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
487 /* spacing */
489 if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
490 else fputs("p-", fs);
492 /* average width */
494 fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
496 /* charset */
498 if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
499 else
500 switch( cpe_font_struct->hdr.fi.dfCharSet )
502 /* Microsoft just had to invent its own charsets! */
504 case ANSI_CHARSET: fputs("microsoft-cp1252\n", fs); break;
505 case GREEK_CHARSET: fputs("microsoft-cp1253\n", fs); break;
506 case TURKISH_CHARSET: fputs("microsoft-cp1254\n", fs); break;
507 case HEBREW_CHARSET: fputs("microsoft-cp1255\n", fs); break;
508 case ARABIC_CHARSET: fputs("microsoft-cp1256\n", fs); break;
509 case BALTIC_CHARSET: fputs("microsoft-cp1257\n", fs); break;
510 case RUSSIAN_CHARSET: fputs("microsoft-cp1251\n", fs); break;
511 case EE_CHARSET: fputs("microsoft-cp1250\n", fs); break;
512 case SYMBOL_CHARSET: fputs("microsoft-symbol\n", fs); break;
513 case SHIFTJIS_CHARSET: fputs("jisx0208.1983-0\n", fs); break;
514 case DEFAULT_CHARSET: fputs("iso8859-1\n", fs); break;
516 default:
517 case OEM_CHARSET:
518 fputs("Undefined charset, use -c option.\n", stderr);
519 return ERROR_DATA;
522 else return ERROR_DATA;
524 fprintf(fs, "SIZE %d %d %d\n",
525 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
526 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
527 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes)); /* dfVertRes[2] */
529 fprintf(fs, "FONTBOUNDINGBOX %d %d %d %d\n",
530 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
531 return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
532 0, l_ascent - l_cellheight );
534 fprintf(fs, "STARTPROPERTIES 4\n");
536 fprintf(fs, "FONT_ASCENT %d\n", l_ascent ); /* dfAscent[2] */
537 fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
538 fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
539 return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
540 fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
542 fprintf(fs, "ENDPROPERTIES\n");
544 fprintf(fs, "CHARS %d\n", l_len);
545 return 0;
550 static void parse_options(int argc, char **argv)
552 int i;
554 switch( argc )
556 case 2:
557 g_lpstrInputFile = argv[1];
558 break;
560 case 3:
561 case 4:
562 case 5:
563 case 6:
564 case 7:
565 case 8:
566 for( i = 1; i < argc - 1; i++ )
568 if( argv[i][0] != '-' ||
569 strlen(argv[i]) != 2 ) break;
571 if( argv[i][1] == 'c' )
572 g_lpstrCharSet = argv[i+1];
573 else
574 if( argv[i][1] == 'f' )
575 g_lpstrFileName = argv[i+1];
576 else
577 if( argv[i][1] == 't' )
579 g_outputPoints = 1;
580 continue;
582 else
583 usage();
585 i++;
587 if( i == argc - 1 )
589 g_lpstrInputFile = argv[i];
590 break;
592 default: usage();
597 /* read file data and return file type */
599 static int get_resource_table(int fd, unsigned char** lpdata, int fsize)
601 IMAGE_DOS_HEADER mz_header;
602 IMAGE_OS2_HEADER ne_header;
603 long s, offset, size;
604 int retval;
606 lseek( fd, 0, SEEK_SET );
608 if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
609 return FILE_ERROR;
611 s = return_data_value(dfShort, &mz_header.e_magic);
613 if( s == IMAGE_DOS_SIGNATURE) /* looks like .dll file so far... */
615 s = return_data_value(dfShort, &mz_header.e_lfanew);
616 lseek( fd, s, SEEK_SET );
618 if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
619 return FILE_ERROR;
621 s = return_data_value(dfShort, &ne_header.ne_magic);
623 if( s == IMAGE_NT_SIGNATURE)
625 fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
626 return FILE_ERROR;
628 else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
630 s = return_data_value(dfShort, &ne_header.ne_rsrctab);
631 size = return_data_value(dfShort, &ne_header.ne_restab);
633 if( size > fsize ) return FILE_ERROR;
635 size -= s;
636 offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
638 if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
639 retval = FILE_DLL;
641 else if( s == 0x300 || s == 0x200 ) /* maybe .fnt ? */
643 size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
645 if( size != fsize ) return FILE_ERROR;
646 offset = 0;
647 retval = FILE_FNT;
649 else return FILE_ERROR;
651 *lpdata = malloc(size);
653 if( *lpdata )
655 lseek( fd, offset, SEEK_SET );
656 if( read(fd, *lpdata, size) != size )
657 { free( *lpdata ); *lpdata = NULL; }
659 return retval;
663 /* entry point */
665 int main(int argc, char **argv)
667 unsigned char* lpdata = NULL;
668 int fd;
670 parse_options( argc, argv);
672 if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) != -1 )
674 int i;
675 struct stat file_stat;
677 fstat( fd, &file_stat);
678 i = get_resource_table( fd, &lpdata, file_stat.st_size );
680 switch(i)
682 case FILE_DLL:
683 if( lpdata )
685 int j, count = 0;
686 NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
687 NE_NAMEINFO* pFontStorage = NULL;
689 while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
691 j = return_data_value(dfShort, &pTInfo->count);
692 if( i == NE_RSCTYPE_FONT )
694 count = j;
695 pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
696 break; /* found one */
699 pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
701 if( pFontStorage && count )
703 unsigned short size_shift = return_data_value(dfShort, lpdata);
704 unsigned char* lpfont = NULL;
705 unsigned offset;
706 int length;
708 for( j = 0; j < count; j++, pFontStorage++ )
710 length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
711 offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
713 if( !(lpfont = realloc( lpfont, length )) )
715 fprintf(stderr, "Memory allocation error.\n" );
716 exit(1);
719 lseek( fd, offset, SEEK_SET );
720 if( read(fd, lpfont, length) != length )
722 fprintf(stderr, "Unable to read Windows DLL.\n" );
723 exit(1);
726 if( (i = parse_fnt_data( lpfont, length )) )
728 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
729 exit(1);
732 free(lpfont); free(lpdata);
733 exit(0);
735 else
737 fprintf(stderr, "No fonts found.\n" );
738 exit(1);
740 free( lpdata );
742 else
744 fprintf(stderr, "Unable to read Windows DLL.\n" );
745 exit(1);
747 break;
749 case FILE_FNT:
750 if( lpdata )
752 if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
754 fprintf(stderr, "Unable to parse font data: Error %d\n", i );
755 exit(1);
757 free( lpdata );
759 else
761 fprintf(stderr, "Unable to read .FNT file.\n" );
762 exit(1);
764 break;
766 case FILE_ERROR:
767 fprintf(stderr, "Corrupt or invalid file.\n" );
768 exit(1);
770 close(fd);
771 exit(0);
773 else
775 fprintf(stderr, "Unable to open '%s'.\n", g_lpstrInputFile );
776 exit(1);