Back to index

tetex-bin  3.0
pfb2pfa.c
Go to the documentation of this file.
00001 /*
00002  * NAME
00003  *     pfb2pfa - convert a type1 pfb file (binary) into a pfa (ASCII) 
00004  * SYNOPSIS
00005  *     pfb2pfa [-v] pfbfile [pfafile]
00006  * DESCRIPTION
00007  *     Program converts a binary MSDOS representation for a type1 
00008  *     PostScript font into a readable ASCII version. The MSDOS 
00009  *     newline (\r) is converted into the UNIX newline (\n).
00010  *     The output is written in a file whose name is the name that
00011  *     is provided on the command line or the basename of the input
00012  *     file plus extension ".pfa".
00013  *
00014  *     With the -v option you get some information about what the 
00015  *     program is doing.
00016  * AUTHOR
00017  *     Piet Tutelaers (rcpt@urc.tue.nl)
00018  */
00019 
00020 #include <stdarg.h>
00021 #include <stdio.h>
00022 #include "basics.h"  /* basic definitions and fatal() */
00023 #include "filenames.h"  /* newname() */
00024 
00025 #if defined(MSDOS) || defined(DOSISH)
00026 #define NEWLINE '\012'
00027 #else
00028 #define NEWLINE '\n'
00029 #endif
00030 
00031 #define HEX_PER_LINE 30
00032 
00033 int main (int argc, char *argv[])
00034 {  unsigned int t, l, i;
00035  unsigned int l1, l2, l3, l4; 
00036    short c, done, verbose = 0;
00037    FILE *pfb, *pfa;
00038    char *pfbname, *pfaname = NULL, *myname = "pfb2pfa";
00039 
00040    /* prototypes */
00041    void fatal(char *fmt, ...);
00042 
00043    while (--argc > 0 && (*++argv)[0] == '-') {
00044       done=0;
00045       while ((!done) && (c = *++argv[0]))  /* allow -bcK like options */
00046         switch (c) {
00047         case 'v':
00048            verbose = 1; break;
00049         default:
00050            fatal("%s: %c illegal option\n", myname, c);
00051         }
00052       }
00053 
00054    if (argc < 1) fatal("Usage: %s [-v] pfbfile [pfafile]\n", myname);
00055    
00056    pfbname = argv[0]; argc--; argv++;
00057    if (argc < 1) pfaname = newname(pfbname, ".pfa");
00058    else if (argc == 1) pfaname = argv[0];
00059    else fatal("Usage: %s [-v] pfbfile [pfafile]\n", myname);
00060 
00061    pfb = fopen(pfbname, RB);
00062    if (pfb == NULL) fatal("Can't open %s\n", pfbname);
00063    
00064    pfa = fopen(pfaname, WB);
00065    if (pfa == NULL) fatal("Can't open %s\n", pfaname);
00066    
00067    while(!feof(pfb)) {
00068       if (getc(pfb) != 128)
00069         fatal("%s: not a pfb file.\n", pfbname);
00070       t = getc(pfb);
00071       if (verbose) printf("Type: %d, ", t);
00072       switch (t) {
00073       case 1:
00074 #if 1
00075        l1 = getc(pfb); l2 = getc(pfb); l3 = getc(pfb); l4 = getc(pfb); 
00076        l = l1 | l2 << 8 | l3 << 16 | l4 << 24;
00077        /* printf("%2x %2x %2x %2x -> %x\n", l1, l2, l3, l4, l); */
00078 #else
00079        l = (getc(pfb)) | (getc(pfb)<<8) | (getc(pfb)<<16) | (getc(pfb)<<24);
00080 #endif
00081          if (verbose) printf(" plain text, length %d\n", l);
00082         for (i=0; i < l ; i++) {
00083             c = getc(pfb);
00084             if (c == '\r') putc(NEWLINE, pfa);
00085             else putc(c, pfa);
00086          }
00087          break;
00088       case 2:
00089 #if 1
00090        l1 = getc(pfb); l2 = getc(pfb); l3 = getc(pfb); l4 = getc(pfb); 
00091        l = l1 | l2 << 8 | l3 << 16 | l4 << 24;
00092        /* printf("%2x %2x %2x %2x -> %x\n", l1, l2, l3, l4, l); */
00093 #else
00094        l = (getc(pfb)) | (getc(pfb)<<8) | (getc(pfb)<<16) | (getc(pfb)<<24);
00095 #endif
00096          if (verbose) printf(" binary data, length %d\n", l);
00097          for(i = 0; i < l ;i++) {
00098            fprintf(pfa, "%02x", getc(pfb));
00099            if ((i+1) % HEX_PER_LINE == 0) putc(NEWLINE, pfa);
00100         }
00101         putc(NEWLINE, pfa);
00102          break;
00103       case 3:
00104          if (verbose) printf("End of file\n");
00105          exit(0);
00106          break;
00107       default:
00108         fatal("Unknown field type: %d\n", t);
00109       }
00110    }
00111    fclose(pfa); fclose(pfb);
00112    return 0;
00113 }