summaryrefslogtreecommitdiff
path: root/Build/source/texk/ps2pk/pfb2pfa.c
blob: 62d58fb0200bb104a9bbfa414252b2b02bab51c3 (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
/*
 * NAME
 *	pfb2pfa - convert a type1 pfb file (binary) into a pfa (ASCII) 
 * SYNOPSIS
 *	pfb2pfa [-v] pfbfile [pfafile]
 * DESCRIPTION
 *	Program converts a binary MSDOS representation for a type1 
 *	PostScript font into a readable ASCII version. The MSDOS 
 *	newline (\r) is converted into the UNIX newline (\n).
 *	The output is written in a file whose name is the name that
 *	is provided on the command line or the basename of the input
 *	file plus extension ".pfa".
 *
 *	With the -v option you get some information about what the 
 *	program is doing.
 * AUTHOR
 *	Piet Tutelaers
 */

#include "basics.h"	/* basic definitions and fatal() */
#include <stdarg.h>
#include <stdio.h>
#include "filenames.h"  /* newname() */

#if defined(MSDOS) || defined(DOSISH)
#define NEWLINE '\012'
#else
#define NEWLINE '\n'
#endif

#define HEX_PER_LINE 30

int main (int argc, char *argv[])
{  unsigned int t, l, i;
   unsigned int l1, l2, l3, l4; 
   short c, done, verbose = 0;
   FILE *pfb, *pfa;
   char *pfbname, *pfaname = NULL;
   const char *myname = "pfb2pfa";

   while (--argc > 0 && (*++argv)[0] == '-') {
      done=0;
      while ((!done) && (c = *++argv[0]))  /* allow -bcK like options */
      	 switch (c) {
      	 case 'v':
      	    verbose = 1; break;
      	 default:
      	    fatal("%s: %c invalid option\n", myname, c);
      	 }
      }

   if (argc < 1) {
     msg  ("pfb2pfa (ps2pk) version " PACKAGE_VERSION " (" TL_VERSION ")\n");
     msg  ("Usage: %s [-v] pfbfile [pfafile]\n", myname);
     fatal("\nEmail bug reports to %s.\n", PACKAGE_BUGREPORT);
   }
   
   pfbname = argv[0]; argc--; argv++;
   if (argc < 1) pfaname = newname(pfbname, ".pfa");
   else if (argc == 1) pfaname = argv[0];
   else fatal("Usage: %s [-v] pfbfile [pfafile]\n", myname);

   pfb = fopen(pfbname, RB);
   if (pfb == NULL) fatal("Can't open %s\n", pfbname);
   
   pfa = fopen(pfaname, WB);
   if (pfa == NULL) fatal("Can't open %s\n", pfaname);
   
   while(!feof(pfb)) {
      if (getc(pfb) != 128)
	 fatal("%s: not a pfb file.\n", pfbname);
      t = getc(pfb);
      if (verbose) printf("Type: %d, ", t);
      switch (t) {
      case 1:
         l1 = getc(pfb); l2 = getc(pfb); l3 = getc(pfb); l4 = getc(pfb); 
         l = l1 | l2 << 8 | l3 << 16 | l4 << 24;
         /* printf("%2x %2x %2x %2x -> %x\n", l1, l2, l3, l4, l); */
         if (verbose) printf(" plain text, length %d\n", l);
	 for (i=0; i < l ; i++) {
            c = getc(pfb);
            if (c == '\r') putc(NEWLINE, pfa);
            else putc(c, pfa);
	  }
         break;
      case 2:
         l1 = getc(pfb); l2 = getc(pfb); l3 = getc(pfb); l4 = getc(pfb); 
         l = l1 | l2 << 8 | l3 << 16 | l4 << 24;
         /* printf("%2x %2x %2x %2x -> %x\n", l1, l2, l3, l4, l); */
         if (verbose) printf(" binary data, length %d\n", l);
         for(i = 0; i < l ;i++) {
	    fprintf(pfa, "%02x", getc(pfb));
	    if ((i+1) % HEX_PER_LINE == 0) putc(NEWLINE, pfa);
	 }
	 putc(NEWLINE, pfa);
         break;
      case 3:
         if (verbose) printf("End of file\n");
         exit(0);
         break;
      default:
	 fatal("Unknown field type: %d\n", t);
      }
   }
   fclose(pfa); fclose(pfb);
   return 0;
}