fnt2bdf.c 20.7 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5 6 7
/************************************************
 *
 * Extract fonts from .fnt or Windows DLL files
 * and convert them to the .bdf format.
 *
 * Copyright 1994-1996 Kevin Carothers and Alex Korobka
 *
8 9 10 11 12 13 14 15 16 17 18 19
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
21 22
 */

23
#include "config.h"
24
#include "wine/port.h"
25 26 27 28

#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
29 30
#include <sys/types.h>
#include <sys/stat.h>
31
#include <stdio.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
32 33
#include <stdlib.h>
#include <string.h>
34 35 36
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
37
#include <fcntl.h>
38 39 40 41
#ifdef HAVE_IO_H
# include <io.h>
#endif

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
#include "windef.h"
#include "wingdi.h"

enum data_types {dfChar, dfShort, dfLong, dfString};

#define ERROR_DATA	1
#define ERROR_VERSION	2
#define ERROR_SIZE	3
#define ERROR_MEMORY	4
#define ERROR_FILE	5

#include "pshpack1.h"

typedef struct
{
    INT16 dfType;
    INT16 dfPoints;
    INT16 dfVertRes;
    INT16 dfHorizRes;
    INT16 dfAscent;
    INT16 dfInternalLeading;
    INT16 dfExternalLeading;
    BYTE  dfItalic;
    BYTE  dfUnderline;
    BYTE  dfStrikeOut;
    INT16 dfWeight;
    BYTE  dfCharSet;
    INT16 dfPixWidth;
    INT16 dfPixHeight;
    BYTE  dfPitchAndFamily;
    INT16 dfAvgWidth;
    INT16 dfMaxWidth;
    BYTE  dfFirstChar;
    BYTE  dfLastChar;
    BYTE  dfDefaultChar;
    BYTE  dfBreakChar;
    INT16 dfWidthBytes;
    LONG  dfDevice;
    LONG  dfFace;
    LONG  dfBitsPointer;
    LONG  dfBitsOffset;
    BYTE  dfReserved;
    LONG  dfFlags;
    INT16 dfAspace;
    INT16 dfBspace;
    INT16 dfCspace;
    LONG  dfColorPointer;
    LONG  dfReserved1[4];
} FONTINFO16;

typedef struct
{
    WORD  offset;
    WORD  length;
    WORD  flags;
    WORD  id;
    WORD  handle;
    WORD  usage;
} NE_NAMEINFO;

typedef struct
{
    WORD  type_id;
    WORD  count;
    DWORD resloader;
} NE_TYPEINFO;

#define NE_FFLAGS_SINGLEDATA    0x0001
#define NE_FFLAGS_MULTIPLEDATA  0x0002
#define NE_FFLAGS_WIN32         0x0010
#define NE_FFLAGS_FRAMEBUF      0x0100
#define NE_FFLAGS_CONSOLE       0x0200
#define NE_FFLAGS_GUI           0x0300
#define NE_FFLAGS_SELFLOAD      0x0800
#define NE_FFLAGS_LINKERROR     0x2000
#define NE_FFLAGS_CALLWEP       0x4000
#define NE_FFLAGS_LIBMODULE     0x8000

#define NE_OSFLAGS_WINDOWS      0x02

#define NE_RSCTYPE_FONTDIR            0x8007
#define NE_RSCTYPE_FONT               0x8008
#define NE_RSCTYPE_SCALABLE_FONTPATH  0x80cc

#define NE_SEGFLAGS_DATA        0x0001
#define NE_SEGFLAGS_ALLOCATED   0x0002
#define NE_SEGFLAGS_LOADED      0x0004
#define NE_SEGFLAGS_ITERATED    0x0008
#define NE_SEGFLAGS_MOVEABLE    0x0010
#define NE_SEGFLAGS_SHAREABLE   0x0020
#define NE_SEGFLAGS_PRELOAD     0x0040
#define NE_SEGFLAGS_EXECUTEONLY 0x0080
#define NE_SEGFLAGS_READONLY    0x0080
#define NE_SEGFLAGS_RELOC_DATA  0x0100
#define NE_SEGFLAGS_SELFLOAD    0x0800
#define NE_SEGFLAGS_DISCARDABLE 0x1000
#define NE_SEGFLAGS_32BIT       0x2000

typedef struct tagFontHeader
{
    SHORT dfVersion;		/* Version */
    LONG dfSize;		/* Total File Size */
    char dfCopyright[60];	/* Copyright notice */
    FONTINFO16 fi;		/* FONTINFO structure */
} fnt_hdrS;

typedef struct WinCharStruct
{
    unsigned int charWidth;
    long charOffset;
} WinCharS;

typedef struct fntFontStruct
{
    fnt_hdrS 	 	hdr;
    WinCharS 		*dfCharTable;
    unsigned char	*dfDeviceP;
    unsigned char 	*dfFaceP;
    unsigned char 	*dfBitsPointerP;
    unsigned char 	*dfBitsOffsetP;
    short 		*dfColorTableP;
} fnt_fontS;

#include "poppack.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
166 167 168 169 170 171 172

#define FILE_ERROR	0
#define FILE_DLL	1
#define FILE_FNT	2

/* global options */

173 174 175 176 177 178 179
static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer);
static int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer);

static char* g_lpstrFileName = NULL;
static char* g_lpstrCharSet = NULL;
static char* g_lpstrInputFile = NULL;
static int   g_outputPoints = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
180 181 182

/* info */

183
static void usage(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
184
{
Alexandre Julliard's avatar
Alexandre Julliard committed
185 186
    printf("Usage: fnt2bdf [-t] [-c charset] [-o basename] [input file]\n");
    printf("  -c charset\tcharset name for OEM_CHARSET fonts\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
187
    printf("  -f basename\tbasic output filename\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
188
    printf("  -t \t\toutput files by point size instead of pixel height\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
189 190 191 192 193
    printf("  input file\tMSWindows .fon, .fnt, .dll, or .exe file.\n");
    printf("\nExample:\n  fnt2bdf -c winsys vgasys.fnt\n\n");
    exit(-1);
}

194
/* convert little-endian value to the local format */
Alexandre Julliard's avatar
Alexandre Julliard committed
195

196
static int return_data_value(enum data_types dtype, void * pChr)
Alexandre Julliard's avatar
Alexandre Julliard committed
197 198 199 200
{
int   ret_val = 0;

    switch(dtype) {
201
        case (dfChar):
Alexandre Julliard's avatar
Alexandre Julliard committed
202 203
            ret_val = (int) *(unsigned char *)pChr;
            break;
204 205

        case(dfShort):
Alexandre Julliard's avatar
Alexandre Julliard committed
206 207 208
            ret_val = *(unsigned char *)pChr;
            ret_val += (*((unsigned char *)pChr + 1) << 8);
            break;
209

Alexandre Julliard's avatar
Alexandre Julliard committed
210 211 212 213 214 215 216 217
        case(dfLong): {
            int  i;

            for(i=3; i >= 0; i--)  {
                ret_val += *((unsigned char *)pChr + i) << (8*i);
                }
            break;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
218 219
		case(dfString):
			break;
220
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
221 222 223
    return ret_val;
}

224
static int make_bdf_filename(char* name, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
Alexandre Julliard's avatar
Alexandre Julliard committed
225
{
226
long	l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
Alexandre Julliard's avatar
Alexandre Julliard committed
227 228 229 230
char*   lpChar;

  if( !g_lpstrFileName )
  {
231
    if( !l_nameoffset ||
232
         l_nameoffset > return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) + 1 )
Alexandre Julliard's avatar
Alexandre Julliard committed
233 234 235 236 237 238 239
         return ERROR_DATA;
    lpChar =  (char*)(file_buffer + l_nameoffset);
  }
    else lpChar = g_lpstrFileName;

  strcpy( name, lpChar );

240
  while( (lpChar = strchr( name, ' ')) )
Alexandre Julliard's avatar
Alexandre Julliard committed
241 242 243 244
         *lpChar = '-';

  /* construct a filename from the font typeface, slant, weight, and size */

245
  if( cpe_font_struct->hdr.fi.dfItalic ) strcat(name, "_i" );
Alexandre Julliard's avatar
Alexandre Julliard committed
246 247 248
  else strcat(name, "_r" );

  lpChar = name + strlen( name );
249 250 251
  sprintf(lpChar, "%d-%d.bdf", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight),
          (g_outputPoints) ? return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints)
			   : return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight) );
Alexandre Julliard's avatar
Alexandre Julliard committed
252 253 254 255 256
  return 0;
}

/* parse FONT resource and write .bdf file */

257
static int parse_fnt_data(unsigned char* file_buffer, int length)
Alexandre Julliard's avatar
Alexandre Julliard committed
258
{
259
  fnt_fontS	cpe_font_struct;
Alexandre Julliard's avatar
Alexandre Julliard committed
260 261
  int     	ic=0, t;

Alexandre Julliard's avatar
Alexandre Julliard committed
262
  memcpy((char *) &cpe_font_struct.hdr, file_buffer, sizeof(fnt_hdrS));
Alexandre Julliard's avatar
Alexandre Julliard committed
263 264 265

  /* check font header */

266
  t = return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion);
Alexandre Julliard's avatar
Alexandre Julliard committed
267 268
  if( t != 0x300 && t != 0x200) return ERROR_VERSION;

269 270 271 272 273 274 275
  t = return_data_value(dfShort, &cpe_font_struct.hdr.fi.dfType);
  if (t & 1)
  {
    fprintf(stderr, "Vector fonts not supported\n");
    return ERROR_DATA;
  }

276
  t = return_data_value(dfLong, &cpe_font_struct.hdr.dfSize);
Alexandre Julliard's avatar
Alexandre Julliard committed
277 278
  if( t > length ) return ERROR_SIZE;
  else
279
  {
Alexandre Julliard's avatar
Alexandre Julliard committed
280 281
    /* 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 285 286 287 288 289
    int l_len   = l_lchar - l_fchar + 1;
    int l_ptr = sizeof(fnt_hdrS);

    /* some fields were introduced for Windows 3.x fonts */
    if( return_data_value(dfShort, &cpe_font_struct.hdr.dfVersion) == 0x200 )
	l_ptr -= 30;
Alexandre Julliard's avatar
Alexandre Julliard committed
290 291 292

    /* malloc size = (# chars) * sizeof(WinCharS) */

293
    if((cpe_font_struct.dfCharTable = (WinCharS *) calloc(sizeof(WinCharS), l_len)) == NULL)
Alexandre Julliard's avatar
Alexandre Julliard committed
294 295 296 297 298 299 300 301 302
	return ERROR_MEMORY;

    /* NOW, convert them all to UNIX (lton) notation... */

    for(ic=0; ic < l_len; ic++) {
   	cpe_font_struct.dfCharTable[ic].charWidth = return_data_value(dfShort, &file_buffer[l_ptr]);
	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 =
Alexandre Julliard's avatar
Alexandre Julliard committed
305
			return_data_value(dfShort, &file_buffer[l_ptr]);
306
	    l_ptr += 2;	/* bump by sizeof(short) */
Alexandre Julliard's avatar
Alexandre Julliard committed
307 308
	    }
	else { 	/*  Windows Version 3.0 type font */
309
	    cpe_font_struct.dfCharTable[ic].charOffset =
Alexandre Julliard's avatar
Alexandre Julliard committed
310 311 312 313 314 315 316 317 318 319
			return_data_value(dfLong, &file_buffer[l_ptr]);
	    l_ptr += 4;	/* bump by sizeof(long) */
	    }
	}
    t = dump_bdf(&cpe_font_struct, file_buffer);
    free( cpe_font_struct.dfCharTable );
  }
  return t;
}

320
static int dump_bdf( fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
Alexandre Julliard's avatar
Alexandre Julliard committed
321 322 323
{
  FILE*   fp;
  int	  ic;
324 325
  int	  l_fchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfFirstChar),
	  l_lchar = return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfLastChar);
Alexandre Julliard's avatar
Alexandre Julliard committed
326

Alexandre Julliard's avatar
Alexandre Julliard committed
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);
Alexandre Julliard's avatar
Alexandre Julliard committed
330 331 332 333 334
  char    l_filename[256];

    if( (ic = make_bdf_filename(l_filename, cpe_font_struct, file_buffer)) )
	return ic;

335
    if((fp = fopen(l_filename, "w")) == NULL)
Alexandre Julliard's avatar
Alexandre Julliard committed
336 337 338 339 340
    {
      fprintf(stderr, "Couldn't open \"%s\" for output.\n", l_filename);
      return ERROR_FILE;
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
341
    ic = dump_bdf_hdr(fp, cpe_font_struct, file_buffer);
342 343 344 345
    if (ic) {
      fclose(fp);
      return (ic);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
346 347 348 349 350 351 352

    /* NOW, convert all chars to UNIX (lton) notation... */

    for(ic=0; ic < l_len; ic++) {
	int rowidx, l_span,		/* how many char-cols wide is char? */
	    l_idx = cpe_font_struct->dfCharTable[ic].charOffset;

353
	l_span = (int) (cpe_font_struct->dfCharTable[ic].charWidth-1)/8;
Alexandre Julliard's avatar
Alexandre Julliard committed
354

355
	fprintf(fp, "STARTCHAR %d\n", ic);
Alexandre Julliard's avatar
Alexandre Julliard committed
356
	fprintf(fp, "ENCODING %d\n",   l_fchar);
357
	fprintf(fp, "SWIDTH    %d    %d\n",
358
		cpe_font_struct->dfCharTable[ic].charWidth*1000,
Alexandre Julliard's avatar
Alexandre Julliard committed
359 360
		0);

361
	fprintf(fp, "DWIDTH    %d    %d\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
362 363 364
		cpe_font_struct->dfCharTable[ic].charWidth, 0);

	fprintf(fp, "BBX  %d  %d  %d   %d\n",
365
		cpe_font_struct->dfCharTable[ic].charWidth, l_hgt, 0,
Alexandre Julliard's avatar
Alexandre Julliard committed
366 367 368 369 370 371 372 373 374 375
		l_ascent - l_hgt);

	fprintf(fp, "BITMAP\n");
	for(rowidx=0; rowidx < l_hgt; rowidx++) {
	    switch(l_span) {
		case(0):	/* 1-7 pixels wide font */
		    {
		    fprintf(fp, "%02X\n", (int) file_buffer[l_idx+rowidx]);
		    break;
		    }
376

Alexandre Julliard's avatar
Alexandre Julliard committed
377 378
		case(1):	/* 8-15 pixels wide font */
		    {
379
		    fprintf(fp, "%02X%02X",
Alexandre Julliard's avatar
Alexandre Julliard committed
380 381 382 383 384 385 386
			(int) file_buffer[l_idx+rowidx], file_buffer[l_idx+l_hgt+rowidx]);
		    fprintf(fp, "\n");
		    break;
		    }

		case(2):	/* 16-23 pixels wide font */
		    {
387
		    fprintf(fp, "%02X%02X%02X",
Alexandre Julliard's avatar
Alexandre Julliard committed
388 389 390 391 392 393 394 395 396
			file_buffer[l_idx+rowidx],
		        file_buffer[l_idx+l_hgt+rowidx],
		        file_buffer[l_idx+(2*l_hgt)+rowidx]);
		    fprintf(fp, "\n");
		    break;
		    }

		case(3):	/* 24-31 pixels wide font */
		    {
397
		    fprintf(fp, "%02X%02X%02X%02X",
Alexandre Julliard's avatar
Alexandre Julliard committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
			file_buffer[l_idx+rowidx],
			file_buffer[l_idx+l_hgt+rowidx],
			file_buffer[l_idx+(2*l_hgt)+rowidx],
			file_buffer[l_idx+(3*l_hgt)+rowidx]);
		    fprintf(fp, "\n");
		    break;
		    }
		case(4):	/* 32-39 */
		    {
		    fprintf(fp, "%02X%02X%02X%02X%02X",
			file_buffer[l_idx+rowidx],
			file_buffer[l_idx+l_hgt+rowidx],
                        file_buffer[l_idx+(2*l_hgt)+rowidx],
                        file_buffer[l_idx+(3*l_hgt)+rowidx],
			file_buffer[l_idx+(4*l_hgt)+rowidx]);
		    fprintf(fp, "\n");
                    break;
                    }
		default:
		    fclose(fp);
		    unlink(l_filename);
		    return ERROR_DATA;
		}
	    }
	fprintf(fp, "ENDCHAR\n");

	l_fchar++;	/* Go to next one */
	}
fprintf(fp, "ENDFONT\n");
fclose(fp);
return 0;
}


432
static int dump_bdf_hdr(FILE* fs, fnt_fontS* cpe_font_struct, unsigned char* file_buffer)
Alexandre Julliard's avatar
Alexandre Julliard committed
433
{
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);
Alexandre Julliard's avatar
Alexandre Julliard committed
436
int     l_len = l_lchar - l_fchar + 1;
437 438 439
long	l_nameoffset = return_data_value(dfLong, &cpe_font_struct->hdr.fi.dfFace);
int	l_cellheight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixHeight);
int	l_ascent = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAscent);
Alexandre Julliard's avatar
Alexandre Julliard committed
440 441 442 443 444

    fprintf(fs, "STARTFONT   2.1\n");

    /* Compose font name */

445
    if( l_nameoffset &&
446
	l_nameoffset < return_data_value(dfLong, &cpe_font_struct->hdr.dfSize) )
Alexandre Julliard's avatar
Alexandre Julliard committed
447 448 449
    {
      int     dpi, point_size;
      char*   lpFace = (char*)(file_buffer + l_nameoffset), *lpChar;
450
      short   tmWeight = return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfWeight);
Alexandre Julliard's avatar
Alexandre Julliard committed
451

452
      while((lpChar = strchr(lpFace, '-')) )
Alexandre Julliard's avatar
Alexandre Julliard committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
	    *lpChar = ' ';

      fprintf(fs, "FONT -windows-%s-", lpFace );

      if( tmWeight == 0 )			/* weight */
	  fputs("medium-", fs);
      else if( tmWeight <= FW_LIGHT )
	  fputs("light-", fs);
      else if( tmWeight <= FW_MEDIUM )
	  fputs("medium-", fs);
      else if( tmWeight <= FW_DEMIBOLD )
	  fputs("demibold-", fs);
      else if( tmWeight <= FW_BOLD )
	  fputs("bold-", fs);
      else fputs("black-", fs);

469
      if( cpe_font_struct->hdr.fi.dfItalic )	/* slant */
Alexandre Julliard's avatar
Alexandre Julliard committed
470 471 472 473 474
	  fputs("i-", fs);
      else fputs("r-", fs);

      /* style */

475
      if( (cpe_font_struct->hdr.fi.dfPitchAndFamily & 0xF0) == FF_SWISS )
Alexandre Julliard's avatar
Alexandre Julliard committed
476 477 478 479 480
	  fputs("normal-sans-", fs);
      else fputs("normal--", fs);	/* still can be -sans */

      /* y extents */

481
      point_size = 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints );
Alexandre Julliard's avatar
Alexandre Julliard committed
482 483
      dpi = (l_cellheight * 720) / point_size;

484
      fprintf(fs, "%d-%d-%d-%d-",l_cellheight, point_size,
485 486
            return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
            return_data_value (dfShort, &cpe_font_struct->hdr.fi.dfVertRes));
Alexandre Julliard's avatar
Alexandre Julliard committed
487 488 489

      /* spacing */

490
      if( return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPixWidth) ) fputs("c-", fs);
Alexandre Julliard's avatar
Alexandre Julliard committed
491
      else fputs("p-", fs);
492

Alexandre Julliard's avatar
Alexandre Julliard committed
493 494
      /* average width */

495
      fprintf( fs, "%d-", 10 * return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfAvgWidth) );
Alexandre Julliard's avatar
Alexandre Julliard committed
496 497 498

      /* charset */

499 500
     if( g_lpstrCharSet ) fprintf(fs, "%s\n", g_lpstrCharSet);
     else
501
      switch( cpe_font_struct->hdr.fi.dfCharSet )
Alexandre Julliard's avatar
Alexandre Julliard committed
502
      {
Alexandre Julliard's avatar
Alexandre Julliard committed
503 504
	/* Microsoft just had to invent its own charsets! */

505
	case ANSI_CHARSET: 	fputs("microsoft-cp1252\n", fs); break;
Alexandre Julliard's avatar
Alexandre Julliard committed
506 507 508 509 510 511
	case GREEK_CHARSET: 	fputs("microsoft-cp1253\n", fs); break;
	case TURKISH_CHARSET: 	fputs("microsoft-cp1254\n", fs); break;
	case HEBREW_CHARSET: 	fputs("microsoft-cp1255\n", fs); break;
	case ARABIC_CHARSET: 	fputs("microsoft-cp1256\n", fs); break;
	case BALTIC_CHARSET: 	fputs("microsoft-cp1257\n", fs); break;
	case RUSSIAN_CHARSET: 	fputs("microsoft-cp1251\n", fs); break;
512
	case EE_CHARSET: 	fputs("microsoft-cp1250\n", fs); break;
Alexandre Julliard's avatar
Alexandre Julliard committed
513
	case SYMBOL_CHARSET: 	fputs("microsoft-symbol\n", fs); break;
Alexandre Julliard's avatar
Alexandre Julliard committed
514 515 516
	case SHIFTJIS_CHARSET: 	fputs("jisx0208.1983-0\n", fs); break;
	case DEFAULT_CHARSET:	fputs("iso8859-1\n", fs); break;

Alexandre Julliard's avatar
Alexandre Julliard committed
517
	default:
518 519 520
	case OEM_CHARSET:
		    fputs("Undefined charset, use -c option.\n", stderr);
		    return ERROR_DATA;
Alexandre Julliard's avatar
Alexandre Julliard committed
521 522 523 524
      }
    }
    else return ERROR_DATA;

525
    fprintf(fs, "SIZE  %d  %d   %d\n",
526
        return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfPoints ),
527 528
	return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfHorizRes),
	return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfVertRes));   /* dfVertRes[2] */
Alexandre Julliard's avatar
Alexandre Julliard committed
529 530

    fprintf(fs, "FONTBOUNDINGBOX %d  %d  %d  %d\n",
531 532
	return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfMaxWidth),
	return_data_value(dfChar, &cpe_font_struct->hdr.fi.dfPixHeight),
Alexandre Julliard's avatar
Alexandre Julliard committed
533 534 535 536 537 538 539
   	0, l_ascent - l_cellheight );

    fprintf(fs, "STARTPROPERTIES  4\n");

    fprintf(fs, "FONT_ASCENT %d\n", l_ascent );                       /*  dfAscent[2] */
    fprintf(fs, "FONT_DESCENT %d\n", l_cellheight - l_ascent );
    fprintf(fs, "CAP_HEIGHT %d\n", l_ascent -
540 541
                                   return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfInternalLeading));
    fprintf(fs, "DEFAULT_CHAR %d\n", return_data_value(dfShort, &cpe_font_struct->hdr.fi.dfDefaultChar));
Alexandre Julliard's avatar
Alexandre Julliard committed
542 543 544 545 546 547 548 549 550

    fprintf(fs, "ENDPROPERTIES\n");

    fprintf(fs, "CHARS  %d\n",  l_len);
    return 0;
}



551
static void parse_options(int argc, char **argv)
Alexandre Julliard's avatar
Alexandre Julliard committed
552 553 554 555 556 557 558 559 560 561 562 563 564
{
  int i;

  switch( argc )
  {
    case 2:
	 g_lpstrInputFile = argv[1];
	 break;

    case 3:
    case 4:
    case 5:
    case 6:
Alexandre Julliard's avatar
Alexandre Julliard committed
565 566
    case 7:
    case 8:
Alexandre Julliard's avatar
Alexandre Julliard committed
567 568 569 570 571
	 for( i = 1; i < argc - 1; i++ )
	 {
	   if( argv[i][0] != '-' ||
	       strlen(argv[i]) != 2 ) break;

572
	   if( argv[i][1] == 'c' )
Alexandre Julliard's avatar
Alexandre Julliard committed
573
	       g_lpstrCharSet = argv[i+1];
574 575
	   else
	   if( argv[i][1] == 'f' )
Alexandre Julliard's avatar
Alexandre Julliard committed
576 577 578 579 580 581 582
	       g_lpstrFileName = argv[i+1];
	   else
	   if( argv[i][1] == 't' )
	   {
	      g_outputPoints = 1;
	      continue;
	   }
Alexandre Julliard's avatar
Alexandre Julliard committed
583 584 585 586
	   else
	   usage();

	   i++;
587
	 }
Alexandre Julliard's avatar
Alexandre Julliard committed
588 589 590 591 592 593 594
	 if( i == argc - 1 )
	 {
	   g_lpstrInputFile = argv[i];
	   break;
	 }
    default: usage();
  }
595

Alexandre Julliard's avatar
Alexandre Julliard committed
596 597 598 599
}

/* read file data and return file type */

600
static int get_resource_table(int fd, unsigned char** lpdata, int fsize)
Alexandre Julliard's avatar
Alexandre Julliard committed
601
{
Alexandre Julliard's avatar
Alexandre Julliard committed
602 603
  IMAGE_DOS_HEADER mz_header;
  IMAGE_OS2_HEADER ne_header;
604 605
  long s, offset, size;
  int retval;
Alexandre Julliard's avatar
Alexandre Julliard committed
606 607 608

  lseek( fd, 0, SEEK_SET );

609
  if( read(fd, &mz_header, sizeof(mz_header)) != sizeof(mz_header) )
Alexandre Julliard's avatar
Alexandre Julliard committed
610 611
      return FILE_ERROR;

Alexandre Julliard's avatar
Alexandre Julliard committed
612
  s = return_data_value(dfShort, &mz_header.e_magic);
Alexandre Julliard's avatar
Alexandre Julliard committed
613

Alexandre Julliard's avatar
Alexandre Julliard committed
614
  if( s == IMAGE_DOS_SIGNATURE)		/* looks like .dll file so far... */
Alexandre Julliard's avatar
Alexandre Julliard committed
615
  {
Alexandre Julliard's avatar
Alexandre Julliard committed
616
    s = return_data_value(dfShort, &mz_header.e_lfanew);
Alexandre Julliard's avatar
Alexandre Julliard committed
617 618 619 620 621 622 623
    lseek( fd, s, SEEK_SET );

    if( read(fd, &ne_header, sizeof(ne_header)) != sizeof(ne_header) )
	return FILE_ERROR;

    s = return_data_value(dfShort, &ne_header.ne_magic);

Alexandre Julliard's avatar
Alexandre Julliard committed
624
    if( s == IMAGE_NT_SIGNATURE)
Alexandre Julliard's avatar
Alexandre Julliard committed
625 626
    {
       fprintf( stderr, "Do not know how to handle 32-bit Windows DLLs.\n");
627
       return FILE_ERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
628
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
629
    else if ( s != IMAGE_OS2_SIGNATURE) return FILE_ERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
630

631 632
    s = return_data_value(dfShort, &ne_header.ne_rsrctab);
    size = return_data_value(dfShort, &ne_header.ne_restab);
Alexandre Julliard's avatar
Alexandre Julliard committed
633 634 635 636

    if( size > fsize ) return FILE_ERROR;

    size -= s;
Alexandre Julliard's avatar
Alexandre Julliard committed
637
    offset = s + return_data_value(dfShort, &mz_header.e_lfanew);
Alexandre Julliard's avatar
Alexandre Julliard committed
638 639 640 641 642 643

    if( size <= sizeof(NE_TYPEINFO) ) return FILE_ERROR;
    retval = FILE_DLL;
  }
  else if( s == 0x300 || s == 0x200 )		/* maybe .fnt ? */
  {
644
    size = return_data_value(dfLong, &((fnt_hdrS *)&mz_header)->dfSize);
Alexandre Julliard's avatar
Alexandre Julliard committed
645 646 647

    if( size != fsize ) return FILE_ERROR;
    offset  = 0;
648
    retval = FILE_FNT;
Alexandre Julliard's avatar
Alexandre Julliard committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
  }
  else return FILE_ERROR;

  *lpdata = (unsigned char*)malloc(size);

  if( *lpdata )
  {
    lseek( fd, offset, SEEK_SET );
    if( read(fd, *lpdata, size) != size )
           { free( *lpdata ); *lpdata = NULL; }
  }
  return retval;
}


/* entry point */

int main(int argc, char **argv)
{
  unsigned char* lpdata = NULL;
  int 		 fd;

  parse_options( argc, argv);

673
  if( (fd = open( g_lpstrInputFile, O_RDONLY | O_BINARY)) != -1 )
Alexandre Julliard's avatar
Alexandre Julliard committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
  {
    int    i;
    struct stat file_stat;

    fstat( fd, &file_stat);
    i = get_resource_table( fd, &lpdata, file_stat.st_size );

    switch(i)
    {
	case FILE_DLL:
	     if( lpdata )
	     {
	       int	    j, count = 0;
	       NE_TYPEINFO* pTInfo = (NE_TYPEINFO*)(lpdata + 2);
	       NE_NAMEINFO* pFontStorage = NULL;

690
	       while( (i = return_data_value(dfShort, &pTInfo->type_id)) )
Alexandre Julliard's avatar
Alexandre Julliard committed
691 692 693 694 695 696
	       {
		  j = return_data_value(dfShort, &pTInfo->count);
		  if( i == NE_RSCTYPE_FONT )
		  {
		    count = j;
		    pFontStorage = (NE_NAMEINFO*)(pTInfo + 1);
697
		    break; /* found one */
Alexandre Julliard's avatar
Alexandre Julliard committed
698 699 700 701 702 703 704 705 706
		  }

		  pTInfo = (NE_TYPEINFO *)((char*)(pTInfo+1) + j*sizeof(NE_NAMEINFO));
	       }
	       if( pFontStorage && count )
	       {
		 unsigned short		size_shift = return_data_value(dfShort, lpdata);
		 unsigned char*		lpfont = NULL;
		 unsigned		offset;
707
		 int			length;
Alexandre Julliard's avatar
Alexandre Julliard committed
708 709 710 711 712

		 for( j = 0; j < count; j++, pFontStorage++ )
		 {
		    length = return_data_value(dfShort, &pFontStorage->length) << size_shift;
		    offset = return_data_value(dfShort, &pFontStorage->offset) << size_shift;
713

Alexandre Julliard's avatar
Alexandre Julliard committed
714 715
		    if( !(lpfont = (unsigned char*) realloc( lpfont, length )) )
		    {
716
			fprintf(stderr, "Memory allocation error.\n" );
Alexandre Julliard's avatar
Alexandre Julliard committed
717
			exit(1);
Alexandre Julliard's avatar
Alexandre Julliard committed
718 719 720 721 722
		    }

		    lseek( fd, offset, SEEK_SET );
		    if( read(fd, lpfont, length) != length )
		    {
723
			fprintf(stderr, "Unable to read Windows DLL.\n" );
Alexandre Julliard's avatar
Alexandre Julliard committed
724
			exit(1);
Alexandre Julliard's avatar
Alexandre Julliard committed
725 726 727
		    }

		    if( (i = parse_fnt_data( lpfont, length )) )
Alexandre Julliard's avatar
Alexandre Julliard committed
728
		    {
729
			fprintf(stderr, "Unable to parse font data: Error %d\n", i );
Alexandre Julliard's avatar
Alexandre Julliard committed
730 731
			exit(1);
		    }
Alexandre Julliard's avatar
Alexandre Julliard committed
732 733
		 }
		 free(lpfont); free(lpdata);
Alexandre Julliard's avatar
Alexandre Julliard committed
734 735 736 737
		 exit(0);
	       }
	       else
	       {
738
                 fprintf(stderr, "No fonts found.\n" );
Alexandre Julliard's avatar
Alexandre Julliard committed
739
		 exit(1);
Alexandre Julliard's avatar
Alexandre Julliard committed
740 741 742
	       }
	       free( lpdata );
	     }
Alexandre Julliard's avatar
Alexandre Julliard committed
743 744
	     else
	     {
745
               fprintf(stderr, "Unable to read Windows DLL.\n" );
Alexandre Julliard's avatar
Alexandre Julliard committed
746 747
	       exit(1);
	     }
Alexandre Julliard's avatar
Alexandre Julliard committed
748 749 750 751 752 753
	     break;

	case FILE_FNT:
	     if( lpdata )
	     {
	       if( (i = parse_fnt_data( lpdata, file_stat.st_size )) )
Alexandre Julliard's avatar
Alexandre Julliard committed
754
	       {
755
                   fprintf(stderr, "Unable to parse font data: Error %d\n", i );
Alexandre Julliard's avatar
Alexandre Julliard committed
756 757
		   exit(1);
	       }
Alexandre Julliard's avatar
Alexandre Julliard committed
758 759
	       free( lpdata );
	     }
Alexandre Julliard's avatar
Alexandre Julliard committed
760 761
	     else
	     {
762
               fprintf(stderr, "Unable to read .FNT file.\n" );
Alexandre Julliard's avatar
Alexandre Julliard committed
763 764
	       exit(1);
	     }
Alexandre Julliard's avatar
Alexandre Julliard committed
765 766 767
	     break;

	case FILE_ERROR:
768
             fprintf(stderr, "Corrupt or invalid file.\n" );
Alexandre Julliard's avatar
Alexandre Julliard committed
769
	     exit(1);
Alexandre Julliard's avatar
Alexandre Julliard committed
770 771
    }
    close(fd);
Alexandre Julliard's avatar
Alexandre Julliard committed
772 773 774 775
    exit(0);
  }
  else
  {
776
    fprintf(stderr, "Unable to open '%s'.\n", g_lpstrInputFile );
Alexandre Julliard's avatar
Alexandre Julliard committed
777
    exit(1);
Alexandre Julliard's avatar
Alexandre Julliard committed
778 779
  }
}