home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / fonts / convmacf / convmacf.c < prev    next >
C/C++ Source or Header  |  1991-02-12  |  5KB  |  214 lines

  1. /*
  2.  
  3.    This program reads a MAC format Adobe Font file and unpacks it so
  4.    that it can be loaded as a program into a PostScript printer.  Note
  5.    that it does not decrypt the file.
  6.  
  7.    I don't beleive that unpacking (as opposed to decrypting) should be a
  8.    violation of the license agreement, but then again, I am not a
  9.    lawyer.  In short, as far as the legalities go, you are on your own.
  10.  
  11.    This program was compiled and tested on an IBM PC/AT with Turbo C
  12.    Version 2.01.  It should build with little trouble using any ANSI C
  13.    compiler.
  14.  
  15.    Modifications for the Amiga: Removed the inclusion of "io.h" and added
  16.    a MOTOROLA define to select the type of byte ordering. The program
  17.    requires a MacBinary file of the font to do its magic.
  18.  
  19. */
  20.  
  21.  
  22. #include <ctype.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #define MOTOROLA
  27.  
  28. #define COMMENT_BLOCK       0
  29. #define ASCII_BLOCK         1
  30. #define HEX_BLOCK           2
  31. #define EOF_BLOCK           3
  32. #define PREAMBLE_LENGTH     0x180L
  33. #define MAX_LINELEN         78
  34. #define MAX_EVEN_LINELEN    MAX_LINELEN & (~1)
  35. /*
  36.    Define error codes
  37. */
  38. #define E_IPO 1
  39. #define E_COI 2                       /* Can't open input */
  40. #define E_COO 3                       /* Can't open output */
  41. #define E_BIF 4                       /* Bad input file format */
  42.  
  43.  
  44. const char *errmsg[] = {
  45.    "\nSuccess %s\n",
  46.    "\nCan't Open Input file %s\n",
  47.    "\nCan't open Output file %s\n",
  48.    "\nBad Input Format %s\n",
  49.     ""
  50. };
  51.  
  52.  
  53. void EXIT(int code,char *text)
  54. {
  55.    printf(errmsg[code], text ? text : "");
  56.    exit(code);
  57. }
  58.  
  59. char tohex[] = {"0123456789ABCDEF"};       /* Array to convert to hex */
  60.  
  61.  
  62.  
  63. /*
  64.     read_block_header()
  65.  
  66.     The font block header consists of a Motorola-format long integer
  67.     consisting of the block length (not including the length of the
  68.     length field), and a byte (padded to an int) consisting of the block
  69.     type.
  70. */
  71.  
  72. int read_block_header(FILE *infile, unsigned long *blksiz, int *blktyp)
  73. {
  74.   int i;
  75.  
  76.     /*
  77.      read and convert the Motorola long
  78.      */
  79. #if MOTOROLA
  80.     for (i=0; i<4; i++)
  81.         *((unsigned char *)blksiz+i) = (unsigned char) fgetc(infile);
  82. #else
  83.     for (i=3; i>=0; i--)
  84.         *((unsigned char *)blksiz+i) = (unsigned char) fgetc(infile);
  85. #endif
  86.     /*
  87.      get the block type
  88.      */
  89.     *blktyp =  fgetc(infile);
  90.     /*
  91.      discard the padding
  92.      */
  93.     fgetc(infile);
  94.     /*
  95.      adjust the block size to reflect the bytes remaining to read
  96.      */
  97.     *blksiz -=2L;
  98.     /*
  99.    flag any problem with the block type
  100.     */
  101.   return (*blktyp < EOF_BLOCK);
  102. }
  103.  
  104.  
  105. void main (int argc, char *argv[])
  106. {
  107.  
  108. int           linlen;              /* length of current line */
  109. FILE          *infile;             /* Input file */
  110. FILE          *outfile;            /* Output file */
  111. unsigned char ch;                  /* a character */
  112. unsigned long blksiz;              /* size of data block */
  113. int           blktyp;              /* block type */
  114.  
  115.  
  116. /* Open the input file and output file */
  117.  
  118.    /*
  119.     check for proper number of command-line parameters
  120.     */
  121.    if (argc != 3)
  122.      EXIT(E_IPO, NULL);
  123.    /*
  124.     identify and open input file
  125.     */
  126.      if ((infile = fopen(argv[1],"rb")) == NULL)
  127.          EXIT(E_COI,argv[1]);
  128.    /*
  129.     identify and open output file
  130.     */
  131.      if ((outfile = fopen(argv[2],"wb")) == NULL)
  132.          EXIT(E_COO,argv[2]);
  133.    /*
  134.     Macintosh font files have a preamble to discard, so seek past the
  135.     preamble.
  136.     */
  137.     fseek(infile, PREAMBLE_LENGTH, SEEK_SET);
  138.     /*
  139.      Do an initial check on the file to see if it looks like a
  140.      PostScript font file.  Here we check to see if the first block type
  141.      in ASCII.
  142.     */
  143.     read_block_header(infile, &blksiz, &blktyp);
  144.     if (blktyp != 1)
  145.     {
  146.       printf("PostScript font program not in correct format\n");
  147.       EXIT(E_BIF, argv[1]);
  148.     }
  149.  
  150.    /*
  151.       output the PostScript "magic number" and the text to invoke the
  152.       server dictionary password
  153.    */
  154.    fputs("%!\n",outfile);
  155.    fputs("serverdict begin 0 exitserver\n",outfile);
  156.  
  157.    /*
  158.     reseek the start source file
  159.     */
  160.      fseek(infile, PREAMBLE_LENGTH, SEEK_SET);
  161.  
  162.    /*
  163.     while there are blocks in the source file, process them
  164.     */
  165.    while (read_block_header(infile, &blksiz, &blktyp))
  166.      {
  167.          switch (blktyp)
  168.          {
  169.        case ASCII_BLOCK:
  170.          /*
  171.           read and output the ASCII characters in the ASCII block
  172.           */
  173.                  for ( ; blksiz > 0; blksiz--)
  174.                  {
  175.            ch = fgetc(infile);
  176.            if (ch != '\015')
  177.              fputc(ch,outfile);    /* if not a carriage return character */
  178.            else
  179.              fputs("\n",outfile);  /* otherwise end the string */
  180.                  }
  181.                  break;
  182.  
  183.        case HEX_BLOCK:
  184.          for (linlen=MAX_EVEN_LINELEN;
  185.               blksiz > 0;
  186.               blksiz--, linlen-=2 )
  187.                  {
  188.                      if (linlen <= 0)
  189.                      {
  190.              /* force a new line */
  191.              fputs("\n",outfile);
  192.              linlen=MAX_EVEN_LINELEN;
  193.                      }
  194.            ch = fgetc(infile);
  195.            fputc(tohex[ch/16], outfile);
  196.            fputc(tohex[ch%16], outfile);
  197.                  }
  198.          fputs("\n",outfile);
  199.                  break;
  200.  
  201.              default:
  202.                  /* skip comments */
  203.          fseek(infile, (long)blksiz, SEEK_CUR);
  204.                  break;
  205.          }
  206.      }
  207.    /*
  208.      Close the files and return success
  209.     */
  210.    fclose(infile);
  211.    fclose(outfile);
  212.    EXIT(0,NULL);
  213. }
  214.