summaryrefslogtreecommitdiff
path: root/fonts/utilities/macfont/macfont.c
blob: 260769fdf18ed6643a9522d453c2a78e286d17e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
/* This file is macfont.h
 *
 * Copyright (C) 1993 by Norman Walsh
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************/

#include <stdio.h>
#include "mactypes.h"
#include "macio.h"
#include "mac2bdf.h"
#include "applestd.h"

#define PTRSIZE 4
#define MIN(x,y) ((x) > (y) ? (y) : (x))
#define Vers "1.1"

extern char *optarg;
extern int optind, opterr;

int Mac_typeCount;
int POSTnum, FONDnum, NFNTnum;
RsrcHdrStruct Mac_header;
RsrcMapStruct Mac_map;
RsrcTypeStruct *Mac_types;
RsrcRefStruct  **Mac_refs;

FILE *input;
FILE *output;
int  rc;

char **style_names;
char **style_longnames;
char *familyname;

char *weight;
char *encoding_scheme;

int  debug = 0;

void dump_mac_header(RsrcHdrStruct h, RsrcMapStruct m)
{
  printf("Mac header:\t\tMac map:\n");
  printf("DataOffset (4): 0x%04x\tNextMap    (4) : 0x%04x\n", 
	 h.DataOffset, m.NextMap);
  printf("MapOffset  (4): 0x%04x\tFileRef    (2) :   %4d\n", 
	 h.MapOffset, m.FileRef);
  printf("DataLen    (4):   %4d\tFileAttr   (2) :   %4d\n", 
	 h.DataLen, m.FileAttr);
  printf("MapLen     (4):   %4d\tTypeOffset (2) :   %4d\n",
	 h.MapLen, m.TypeOffset);
  printf("\t\t\tNameOffset (2) :   %4d\n", 
	 m.NameOffset);
  printf("\n");
}

void dump_mac_info(RsrcTypeStruct t, RsrcRefStruct r)
{
  printf("Mac type:\t\tMac ref:\n");
  printf("Name      (4) :   %c%c%c%c\tIdent      (2) : %6d\n",
	 t.Name[0], t.Name[1], t.Name[2], t.Name[3], r.Ident);
  printf("Count     (2) :   %4d\tNameOffset (2) : 0x%04x\n",
	 t.Count, r.NameOffset);
  printf("RefOffset (2) : 0x%04x\tAttr       (1) : %d\n",
	 t.RefOffset, r.Attr);
  printf("\t\t\tDataOffset (2) : 0x%04x\n", 
	 r.DataOffset);
  printf("\n");
}

void dump_mac_info2(RsrcRefStruct r)
{
  printf("\t\t\tMac ref:\n");
  printf("\t\t\tIdent      (2) : %6d\n", r.Ident);
  printf("\t\t\tNameOffset (2) : 0x%04x\n", r.NameOffset);
  printf("\t\t\tAttr       (1) : %d\n", r.Attr);
  printf("\t\t\tDataOffset (2) : 0x%04x\n",  r.DataOffset);
  printf("\n");
}

void dump_mac_nfnt(RsrcNfntStruct n, int id)
{
  printf("Mac NFNT (%d)\n", id);
  printf("FontType    : %6d\n", n.FontType);
  printf("FirstChar   : %6d\n", n.FirstChar);
  printf("LastChar    : %6d\n", n.LastChar);
  printf("WidMax      : %6d\n", n.WidMax);
  printf("KernMax     : %6d\n", n.KernMax);
  printf("NDescent    : %6d\n", n.NDescent);
  printf("FRectWidth  : %6d\n", n.FRectWidth);
  printf("FRectHeight : %6d\n", n.FRectHeight);
  printf("OWTLoc      : 0x%04x\n", n.OWTLoc);
  printf("Ascent      : %6d\n", n.Ascent);
  printf("Descent     : %6d\n", n.Descent);
  printf("Leading     : %6d\n", n.Leading);
  printf("RowWords    : %6d\n\n", n.RowWords);
}

void dump_mac_fond(RsrcFondStruct f, int id)
{
  int count;

  printf("Mac FOND (%d)\n", id);
  printf("Flag          : %6d\n", f.Flag);
  printf("FamilyId      : %6d\n", f.FamilyId);
  printf("FirstChar     : %6d\n", f.FirstChar);
  printf("LastChar      : %6d\n", f.LastChar);
  printf("Ascent        : %6d\n", f.Ascent);
  printf("Descent       : %6d\n", f.Descent);
  printf("Leading       : %6d\n", f.Leading);
  printf("WidMax        : %6d\n", f.WidMax);
  printf("WidTblOfs (4) : 0x%04x\n", f.WidTblOfs);
  printf("KrnTblOfs (4) : 0x%04x\n", f.KrnTblOfs);
  printf("StyTblOfs (4) : 0x%04x\n", f.StyTblOfs);

  for (count = 0; count < 9; count++)
      printf("  FamStyProp %d: %6d\n", count, f.FamStyProp[count]);

  printf("Intrnatnl (4) : %6d\n", f.Intrnatnl);
  printf("FontVers      : %6d\n", f.FontVers); 

  for (count = 0; count < f.NumFonts; count++)
    {
       printf("  ResourceId  : %6d\n", f.AssocTable[count].ResourceNumber);
       printf("  StyleCode   : %6d\n", f.AssocTable[count].StyleCode);
       printf("  PointSize   : %6d\n", f.AssocTable[count].PointSize);
     }

  printf("\n");
}

void write_post(int count, int offset)
{
  int datalen,post_type,filler;
  int place,hexplace,rowlen,newplace;
  unsigned char *buffer, *newbuf;
  char hexdigits[16] = "0123456789ABCDEF";

  fseek (input, offset, 0);
  datalen = read_macint() - 2;
  post_type = read_macchar();
  filler = read_macchar();
  while (count > 0 && post_type != 5) 
    {
      buffer = (char *) malloc(datalen);
      fread (buffer, 1, datalen, input);

      switch (post_type)
	{
	case 0: /* comment */
	  break;
	case 1: /* ASCII text */
	  newbuf = (char *) malloc(2*datalen); /* 2 is a guesstimate */
	  for (place = 0, newplace=0; place < datalen; place++)
	    {
	      switch (buffer[place])
		{
		case 13:
		  newbuf[newplace++] = 10;
		  break;
		case 0251:
		  newbuf[newplace++] = '(';
		  newbuf[newplace++] = 'C';
		  newbuf[newplace++] = ')';
		  break;
		default:
		  newbuf[newplace++] = buffer[place];
		  break;
		}
	    }
	  fwrite (newbuf, 1, newplace, output);
	  free(newbuf);
	  break;
	case 2: /* hexidecimal data */
	  newbuf = (char *) malloc(3*datalen); /* 3 is overkill */
	  for (place = 0, hexplace = 0, rowlen = 0;
	       place < datalen;
	       place++)
	    {
	      newbuf[hexplace++] = hexdigits[(buffer[place] >> 4) & 0x0F];
	      newbuf[hexplace++] = hexdigits[buffer[place] & 0x0F];
	      rowlen += 2;
	      if (rowlen > 63)
		{
		  newbuf[hexplace++] = 10;
		  rowlen = 0;
		}
	    }
	  if (rowlen != 0)
	    newbuf[hexplace++] = 10;
	  fwrite(newbuf, 1, hexplace, output);
	  free(newbuf);
	  break;
	case 3: /* end-of-file marker */
	  break;
	case 4: /* font program is in data fork */
	  printf("Can't handle POST resources in the data fork.\n");
	  exit(1);
	  break;
	case 5: /* end of font program */
	  break;
	}
	  
      free (buffer);
      datalen = read_macint() - 2;
      post_type = read_macchar();
      filler = read_macchar();
      count--;
    }
}

float frac2real(int W)
{
  float R;
  int   I;

  R = (W & 0x0FFF);
  R = R / 4096.0;

  W = (W >> 12) & 0x0F;
  if (W >= 0x08)
    {
      W = W ^ 0xFFFF;
      W = (W + 1) & 0x0F;
      I = -W;
    }
  else 
    I = W;

  R = R + I;
  
  return R;
}

void get_style_names (int offset, int num_styles)
{
  int count, place, actual, strcnt, namecnt;
  unsigned char SNT[4096];
  unsigned short int SNPos[64];
  char *SPtr;
  char *EncStr;
  unsigned short int Class;
  int EncTblOfs;
  unsigned char StyIdxTbl[48];
  char **strings;
  
  fseek (input, offset, 0);
  count = read_macint();
  Class = read_macshortint();
  EncTblOfs = read_macint();
  read_macint(); /* skip the reserved word */
  for (count = 0; count < 48; count++)
    StyIdxTbl[count] = read_macchar() - 1;

  if (EncTblOfs != 0)
    {
      printf("Info: This file contains an encoding table...\n");
    }

  strcnt = read_macshortint();
  fread (SNT+2, 1, 126, input);
  place = 2;
  SNPos[0] = place;
  for (count = 1; count < 64; count++)
    {
      place = place + SNT[SNPos[count-1]] + 1;
      SNPos[count] = place;
    }

  strings = (char **) malloc (strcnt * PTRSIZE);

  for (count = 0; count < strcnt; count++)
    {
      int slen;
      char *sptr;

      slen = SNT[SNPos[count]];
      sptr = (char *) malloc(slen+1);
      strncpy(sptr,&SNT[SNPos[count]+1],slen);
      *(sptr+slen) = 0;

      strings[count] = sptr;
    }

  familyname = strings[0];
  style_names = (char **) malloc((num_styles+1) * PTRSIZE);
  style_longnames = (char **) malloc((num_styles+1) * PTRSIZE);

  for (count = 0; count < num_styles; count++)
    {
      int slen;
      char *sptr,*strnum;
      char *nptr,*lnptr;

      sptr = strings[0];
      slen = strlen(sptr);

      if (strcnt > 1) 
	{
	  strnum = strings[StyIdxTbl[count]];
	  while (*strnum) 
	    {
	      sptr = strings[*strnum-1];
	      slen += strlen(sptr);
	      strnum++;
	    }
	}

      style_names[count] = (char *) malloc(slen+1);
      style_longnames[count] = (char *) malloc(slen+1);

      nptr = style_names[count];
      lnptr = style_longnames[count];

      sptr = strings[0];
      slen = strlen(sptr);

      strncpy (nptr, sptr, MIN(slen,5));
      nptr += MIN(slen,5);
      *nptr = 0;

      strcpy (lnptr, sptr);
      lnptr += slen;
      *lnptr = 0;

      if (strcnt > 1) 
	{
	  strnum = strings[StyIdxTbl[count]];
	  while (*strnum) 
	    {
	      sptr = strings[*strnum-1];
	      slen = strlen(sptr);

	      strncpy (nptr, sptr, MIN(slen,3));
	      nptr += MIN(slen,3);
	      *nptr = 0;

	      strcpy (lnptr, sptr);
	      lnptr += slen;
	      *lnptr = 0;

	      strnum++;
	    }
	}
    }

  style_names[num_styles] = 0;
  style_longnames[num_styles] = 0;
}

void write_kerns(int offset, FILE *afm)
{
  int NumKernTbls, StyleCode, NumKernPairs;
  int count;
  unsigned char L1, L2;
  int Frac;

  fseek(input, offset, 0);

  read_macint(); /* skip length, or something? */

  NumKernTbls  = read_macshortint();
  StyleCode    = read_macshortint();
  NumKernPairs = read_macshortint();

  fprintf(afm, "StartKernData\n");
  fprintf(afm, "StartKernPairs %d\n", NumKernPairs);

  for (count = 0; count < NumKernPairs; count++)
    {
      L1 = read_macchar();
      L2 = read_macchar();
      Frac = read_macshortint();

      if (encoding[L1] != 0 && encoding[L2] != 0)
	fprintf(afm, "KPX %s %s %d\n",
		encoding[L1],
		encoding[L2],
		(int) (frac2real(Frac)*1000));
    }

  fprintf(afm, "EndKernPairs\n");
  fprintf(afm, "EndKernData\n");
}

void write_nfnt(int offset, 
		FontEntryFormat font_entry, 
		char *name,int id,
		int sequential)
{
  RsrcNfntStruct Nfnt;
  int datalen;
  unsigned char *buf;

  fseek (input, offset, 0);
  datalen = read_macint();

  Nfnt = read_mac_nfnt();

  if (debug)
    dump_mac_nfnt(Nfnt, id);

  fseek (input, offset, 0);
  datalen = read_macint();

  buf = (char *) malloc (datalen);
  fread (buf, 1, datalen, input);

  if (sequential) 
    name = "mf";

  FontDump(buf, name, font_entry.StyleCode, font_entry.PointSize);

  free(buf);

  return;
}

void write_fond(int offset, int id, int afmopt, int bdfopt, int sequential)
{
  int fonddatalen,datalen,count,tblcount;
  unsigned char *buffer;
  RsrcFondStruct Fond;
  unsigned short int TableCount, Style, Width;
  unsigned short int **Widths;
  unsigned short int *curWidths;
  FILE *afm;

  fseek (input, offset, 0);
  fonddatalen = read_macint();
  
  Fond = read_mac_fond();

  if (debug)
    dump_mac_fond(Fond, id);

  fseek (input, offset+Fond.WidTblOfs, 0);
  
  datalen = read_macshortint();
  datalen = read_macshortint();
  
  TableCount = read_macshortint()+1;
  
  Widths = (unsigned short int **) malloc (TableCount * PTRSIZE);
  
  for (tblcount = 0; tblcount < TableCount; tblcount++)
    {
      Style = read_macshortint();
      
      Widths[tblcount] = (unsigned short int *) malloc(256 * 2);
      curWidths = Widths[tblcount];
      
      for (count = 0; count < 256; count++)
	{
	  Width = read_macshortint();
	  curWidths[count] = (frac2real(Width) * 1000);
	}
      
      read_macshortint();	/* discard unused word */
      count = read_macshortint();
      
      if (count != 0)
	printf("Warning: table doesn't conform to spec.\n");
    }
  
  get_style_names (offset + Fond.StyTblOfs, TableCount);
  
  for (tblcount = 0; afmopt && tblcount < TableCount; tblcount++)
    {
      char *filename;
      char *ch;
      
      if (sequential)
	{
	  filename = (char *) malloc(12);
	  sprintf(filename, "macfont.%d", sequential++);
	}
      else
	{
	  filename = (char *) malloc(strlen(style_names[tblcount])+5);
	  strcpy(filename, style_names[tblcount]);
	  strcpy(filename+strlen(style_names[tblcount]),".afm");
	  
	  for (ch = filename;
	       *ch;
	       ch++)
	    if (*ch < 32 || *ch > 126)
	      *ch = '-';
	}
      
      fprintf (stderr, "Writing AFM file to %s.\n", filename);
      afm = fopen(filename, "w");

      fprintf (afm, "StartFontMetrics 3.0\n");
      fprintf (afm, "Comment Created by MacFont vers %s.\n", Vers);
      fprintf (afm, "Comment MacFont Copyright (C) 1993 by Norman Walsh\n");
      fprintf (afm, "Comment Free software.  No warranty.\n");
      fprintf (afm, "FontName %s\n", style_names[tblcount]);
      fprintf (afm, "FullName %s\n", style_longnames[tblcount]);
      fprintf (afm, "FamilyName %s\n", familyname);
      fprintf (afm, "Weight %s\n", weight);
      fprintf (afm, "FontBBox 0 %d %d %d\n",
	       (int) (frac2real(Fond.Descent)*1000),
	       (int) (frac2real(Fond.WidMax)*1000),
	       (int) (frac2real(Fond.Ascent)*1000));
      fprintf (afm, "EncodingScheme %s\n", encoding_scheme);

      fprintf (afm, "StartCharMetrics %d\n",
	       Fond.LastChar-Fond.FirstChar+1);
      
      curWidths = Widths[tblcount];
      for (count = Fond.FirstChar; count <= Fond.LastChar; count++)
	{
	  fprintf(afm,"C %3d ; WX %d ; N %s ; B 0 %d %d %d ;\n", 
		  count,
		  curWidths[count],
		  encoding[count],
		  (int) (frac2real(Fond.Descent) * 1000),
		  curWidths[count],
		  (int) (frac2real(Fond.Ascent) * 1000));
	}

      fprintf (afm, "EndCharMetrics\n");

      if (Fond.KrnTblOfs == 0)
	{
	  fprintf(afm, "StartKernData\n");
	  fprintf(afm, "StartKernPairs 0\n");
	  fprintf(afm, "EndKernPairs\n");
	  fprintf(afm, "EndKernData\n");
	}
      else
	write_kerns(offset+Fond.KrnTblOfs,afm);

      fprintf(afm, "EndFontMetrics\n");
      fclose(afm);
    }

  if (bdfopt)
    {
      RsrcRefStruct *curRef;
      int refcount;

      for (count = 0; count < Fond.NumFonts; count++)
	{
	  curRef = Mac_refs[NFNTnum];
	  for (refcount = 0; refcount < Mac_types[NFNTnum].Count; refcount++)
	    {
	      if (curRef->Ident == Fond.AssocTable[count].ResourceNumber)
		write_nfnt(Mac_header.DataOffset + curRef->DataOffset,
			   Fond.AssocTable[count], 
			   style_names[Fond.AssocTable[count].StyleCode], 
			   curRef->Ident,
			   sequential);
	      curRef++;
	    }
	}
    }
}

int main (int argc, char **argv)
{
  int dump_input = 1;
  int Mac_typeCount;
  int count;
  char outputname[80];
  int outputcount = 1;
  FILE *encinput;

  char opt;
  char *optstring = "ap:e:E:dsw:b";
  char *pfafile   = 0;
  char *encfile   = 0;
  char *rsrcfile  = 0;
  int  pfaopt     = 0;
  int  afmopt     = 0;
  int  bdfopt     = 0;
  int  encopt     = 0;
  int  sequential = 0;

  fprintf (stderr,"MacFont vers %s.  Copyright (C) 1993 by Norman Walsh.\n",
	   Vers);
  fprintf (stderr,"Portions Copyright (C) 1992, 1993, Metis Technology, Inc.\n");

  /* ********************************************************************** */
  /* Read the command line arguments.  This is really crudely done!         */

  weight = 0;

  opterr = 0;
  while ((opt = getopt (argc, argv, optstring)) != EOF) 
    {
      switch (opt)
	{
	case '?':
	  fprintf(stderr,"Invalid option.\n");
	  exit(1);
	  break;

	case 'a':
	  afmopt = 1;
	  break;

	case 'b':
	  bdfopt = 1;
	  break;

	case 's':
	  sequential = 1;
	  break;

	case 'p':
	  pfaopt = 1;
	  pfafile = optarg;
	  break;

	case 'e':
	  encopt = 1;
	  encfile = optarg;
	  break;

	case 'E':
	  encoding_scheme = optarg;
	  break;

	case 'w':
	  weight = optarg;
	  break;

	case 'd':
	  debug = 1;
	  break;
	}
    }

  rsrcfile = argv[optind];

  if (!rsrcfile) 
    {
      fprintf(stderr, "You must supply a resource file.\n");
      fprintf(stderr, "\nUsage  : %s <opts> macfile\n", argv[0]);
      fprintf(stderr, "Where  : macfile is a Macintosh resource fork.\n");
      fprintf(stderr, "Options: -a      = extract AFM file from FOND.\n");
      fprintf(stderr, "         -b      = extract BDF bitmap fonts from NFNT.\n");
      fprintf(stderr, "         -p file = extract PFA file from POST into `file'.\n");
      fprintf(stderr, "         -d      = dump resource data (debugging).\n");
      fprintf(stderr, "         -e file = load encoding vector from `file'.\n");
      fprintf(stderr, "         -E name = change EncodingScheme name in AFM.\n");
      fprintf(stderr, "         -s      = extract AFM files with sequential names.\n");
      fprintf(stderr, "         -w wght = change Weight to `wght' in AFM.\n");
      exit(1);
    }

  if (!weight)
    weight = "Normal";

  input = fopen(rsrcfile,"r");
  if (!input)
    {
      fprintf (stderr, "Can't open resource file: %s\n", rsrcfile);
      exit(1);
    }

  if (encopt && encfile)
    {
      char line[80];

      if (!encoding_scheme)
	encoding_scheme = "FontSpecific";

      encinput = fopen(encfile,"r");
      if (!encinput)
	{
	  fprintf (stderr, "Can't open encoding file: %s\n", encfile);
	  exit(1);
	}

      for (count = 0; count < 256; count++)
	{
	  fgets(line, 80, encinput);
	  line[strlen(line)-1] = 0;

	  if (strncmp(line, ".notdef", 7) == 0)
	    encoding[count] = NULL;
	  else
	    {
	      encoding[count] = (char *) malloc(strlen(line)+1);
	      strcpy(encoding[count], line);
	    }
	}

      if (debug)
	{
	  printf("Encoding:\n");
	  for (count = 0; count < 256; count++)
	    if (encoding[count])
	      printf("\t%3d `%s'\n", count, encoding[count]);
	    else
	      printf("\t%3d `.notdef'\n", count);
	}
    }

  if (!encoding_scheme)
    encoding_scheme = "AppleStandard";

  /* ********************************************************************** */
  /* Read the Mac resource file ...                                         */

  Mac_header = read_mac_header();

  fseek(input, Mac_header.MapOffset, 0);

  Mac_map = read_mac_map();

  if (debug)
    dump_mac_header(Mac_header, Mac_map);

  fseek(input, Mac_header.MapOffset + Mac_map.TypeOffset, 0);

  Mac_typeCount = read_macshortint()+1;
  Mac_types = (RsrcTypeStruct *) malloc (Mac_typeCount*sizeof(RsrcTypeStruct));
  Mac_refs = (RsrcRefStruct **) malloc (Mac_typeCount*sizeof(RsrcRefStruct));

  POSTnum = -1;
  FONDnum = -1;
  NFNTnum = -1;
  for (count = 0; count < Mac_typeCount; count++)
    {
      Mac_types[count] = read_mac_type();
      if (strncmp (Mac_types[count].Name, "POST", 4) == 0)
	POSTnum = count;
      if (strncmp (Mac_types[count].Name, "FOND", 4) == 0)
	FONDnum = count;
      if (strncmp (Mac_types[count].Name, "NFNT", 4) == 0)
	NFNTnum = count;
    }

  for (count = 0; count < Mac_typeCount; count++)
    {
      RsrcRefStruct *curRef;
      int ref_count;

      curRef = (RsrcRefStruct *) malloc(Mac_types[count].Count
					* sizeof(RsrcRefStruct));

      Mac_refs[count] = curRef;

      fseek(input,
            Mac_header.MapOffset
            + Mac_map.TypeOffset
            + Mac_types[count].RefOffset,
            0);

      for (ref_count = 0; ref_count < Mac_types[count].Count; ref_count++)
	{
	  *curRef = read_mac_ref();
	  curRef++;
	}
    }

  if (debug)
    for (count = 0; count < Mac_typeCount; count++)
      {
	RsrcRefStruct *curRef;
	int ref_count;
	
	curRef = Mac_refs[count];
	dump_mac_info(Mac_types[count], *curRef++);
	for (ref_count = 1; ref_count < Mac_types[count].Count; ref_count++)
	  dump_mac_info2(*curRef++);
      }

  /* ********************************************************************** */
  /* Dump out the POST and FOND resources, as requested                     */

  if (pfaopt)
    {
      if (POSTnum >= 0) 
	{
	  RsrcRefStruct *curRef = Mac_refs[POSTnum];

	  fprintf (stderr, "Writing PFA file to %s.\n", pfafile);
	  sprintf(outputname, pfafile);
	  output = fopen(outputname, "w");
	  write_post(Mac_types[POSTnum].Count,
		     Mac_header.DataOffset + curRef->DataOffset);
	  fclose(output);
	}
      else
	fprintf (stderr, "No POST found in %s.\n", rsrcfile);
    }

  if (afmopt || bdfopt)
    {
      if (FONDnum >= 0) 
	{
	  RsrcRefStruct *curRef = Mac_refs[FONDnum];
	  int refcount;

	  for (refcount = 0; refcount < Mac_types[FONDnum].Count; refcount++)
	    {
	      write_fond(Mac_header.DataOffset + curRef->DataOffset,
			 curRef->Ident,
			 afmopt, bdfopt,
			 sequential);
	      curRef++;
	    }
	}
      else
	fprintf (stderr, "No metrics or bitmaps found in %s.\n", rsrcfile);
    }

  exit (0);
}