Debian Codegraph @

texk/ps2pk/pfb2pfa.c

raw
1 /*
2 * NAME
3 * pfb2pfa - convert a type1 pfb file (binary) into a pfa (ASCII)
4 * SYNOPSIS
5 * pfb2pfa [-v] pfbfile [pfafile]
6 * DESCRIPTION
7 * Program converts a binary MSDOS representation for a type1
8 * PostScript font into a readable ASCII version. The MSDOS
9 * newline (\r) is converted into the UNIX newline (\n).
10 * The output is written in a file whose name is the name that
11 * is provided on the command line or the basename of the input
12 * file plus extension ".pfa".
13 *
14 * With the -v option you get some information about what the
15 * program is doing.
16 * AUTHOR
17 * Piet Tutelaers
18 */
19
20 #include "basics.h" /* basic definitions and fatal() */
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include "filenames.h" /* newname() */
24
25 #if defined(MSDOS) || defined(DOSISH)
26 #define NEWLINE '\012'
27 #else
28 #define NEWLINE '\n'
29 #endif
30
31 #define HEX_PER_LINE 30
32
33 int main (int argc, char *argv[])
34 { unsigned int t, l, i;
35 unsigned int l1, l2, l3, l4;
36 short c, done, verbose = 0;
37 FILE *pfb, *pfa;
38 char *pfbname, *pfaname = NULL;
39 const char *myname = "pfb2pfa";
40
41 while (--argc > 0 && (*++argv)[0] == '-') {
42 done=0;
43 while ((!done) && (c = *++argv[0])) /* allow -bcK like options */
44 switch (c) {
45 case 'v':
46 verbose = 1; break;
47 default:
48 fatal("%s: %c invalid option\n", myname, c);
49 }
50 }
51
52 if (argc < 1) {
53 msg ("pfb2pfa (ps2pk) version " PACKAGE_VERSION " (" TL_VERSION ")\n");
54 msg ("Usage: %s [-v] pfbfile [pfafile]\n", myname);
55 fatal("\nEmail bug reports to %s.\n", PACKAGE_BUGREPORT);
56 }
57
58 pfbname = argv[0]; argc--; argv++;
59 if (argc < 1) pfaname = newname(pfbname, ".pfa");
60 else if (argc == 1) pfaname = argv[0];
61 else fatal("Usage: %s [-v] pfbfile [pfafile]\n", myname);
62
63 pfb = fopen(pfbname, RB);
64 if (pfb == NULL) fatal("Can't open %s\n", pfbname);
65
66 pfa = fopen(pfaname, WB);
67 if (pfa == NULL) fatal("Can't open %s\n", pfaname);
68
69 while(!feof(pfb)) {
70 if (getc(pfb) != 128)
71 fatal("%s: not a pfb file.\n", pfbname);
72 t = getc(pfb);
73 if (verbose) printf("Type: %d, ", t);
74 switch (t) {
75 case 1:
76 l1 = getc(pfb); l2 = getc(pfb); l3 = getc(pfb); l4 = getc(pfb);
77 l = l1 | l2 << 8 | l3 << 16 | l4 << 24;
78 /* printf("%2x %2x %2x %2x -> %x\n", l1, l2, l3, l4, l); */
79 if (verbose) printf(" plain text, length %d\n", l);
80 for (i=0; i < l ; i++) {
81 c = getc(pfb);
82 if (c == '\r') putc(NEWLINE, pfa);
83 else putc(c, pfa);
84 }
85 break;
86 case 2:
87 l1 = getc(pfb); l2 = getc(pfb); l3 = getc(pfb); l4 = getc(pfb);
88 l = l1 | l2 << 8 | l3 << 16 | l4 << 24;
89 /* printf("%2x %2x %2x %2x -> %x\n", l1, l2, l3, l4, l); */
90 if (verbose) printf(" binary data, length %d\n", l);
91 for(i = 0; i < l ;i++) {
92 fprintf(pfa, "%02x", getc(pfb));
93 if ((i+1) % HEX_PER_LINE == 0) putc(NEWLINE, pfa);
94 }
95 putc(NEWLINE, pfa);
96 break;
97 case 3:
98 if (verbose) printf("End of file\n");
99 exit(0);
100 break;
101 default:
102 fatal("Unknown field type: %d\n", t);
103 }
104 }
105 fclose(pfa); fclose(pfb);
106 return 0;
107 }