[alt.sources] A *.PFB to *.PF converter

jtc@motcad.portal.com (J.T. Conklin) (05/13/91)

Two weeks ago I was attempting to use the type 1 fonts distributed
with Adobe Type Manager with Ghostscript.  I ended up writing this
filter which strips out the extra binary stuff and converts the 
binary eexec section to hex representation (Ghostscript doesn't 
understand the binary stuff yet).

This program is so trivial, there must be hundreds of them out there
that do the same thing.  But I don't know of any, so here is my
contribution.

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  pfb2pf.c
# Wrapped by jtc@sparc1 on Mon May 13 09:04:55 1991
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'pfb2pf.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'pfb2pf.c'\"
else
echo shar: Extracting \"'pfb2pf.c'\" \(2438 characters\)
sed "s/^X//" >'pfb2pf.c' <<'END_OF_FILE'
X/*
X * Copyright (c) 1991 J.T. Conklin (jtc@motcad.PORTAL.COM)  
X * All Rights Reserved.
X *
X * You may copy, distribute, and use this software as long as this
X * copyright statement is not removed.
X * 
X * $Id: pfb2pf.c,v 1.1 1991/05/10 22:58:10 jtc Exp $
X */
X
X#include <stdio.h>
X#include <stdlib.h>
X
Xenum
X{
X  FIND_KEY,
X  FIND_TYPE,
X  PROC_ASCII,
X  PROC_BINARY
X} state;
X
Xchar hex[] =
X{
X  '0', '1', '2', '3', '4', '5', '6', '7',
X  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
X};
X
Xmain()
X{
X  int c;
X  unsigned int i;
X  unsigned long length;
X  state = FIND_KEY;
X  
X  for (;;)
X    {
X      switch (state)
X	{
X	case FIND_KEY:
X	  if ((c = getchar ()) == EOF)
X	    {
X	      fprintf (stderr, "Unexpected end of file\n");
X	      exit (1);
X	    }
X	  else if (c != 0x80)
X	    {
X	      fprintf (stderr, "Expecting key, found 0x%.2x\n", c);
X	      exit (1);
X	    }
X	  else
X	    {
X	      state = FIND_TYPE;
X	    }
X	  break;
X	  
X	case FIND_TYPE:
X	  if ((c = getchar ()) == EOF)
X	    {
X	      fprintf (stderr, "Unexpected end of file\n");
X	      exit (1);
X	    }
X	  else if (c == '\001')
X	    {
X	      state = PROC_ASCII;
X	    }
X	  else if (c == '\002')
X	    {
X	      state = PROC_BINARY;
X	    }
X	  else if (c == '\003')
X	    {
X	      exit (0);
X	    }
X	  else
X	    {
X	      fprintf (stderr, "Unknown section type\n");
X	      exit (1);
X	    }
X	  break;
X
X	case PROC_ASCII:
X	  length = 0;
X	  for (i = 0; i < 4; i++)
X	    {
X	      if ((c = getchar ()) == EOF)
X		{
X		  fprintf (stderr, "Unexpected end of file\n");
X		  exit (1);
X		}
X	      length |= (c << (8*i));
X	    }
X
X	  for (i = 0; i < length; i++)
X	    {
X	      if ((c = getchar ()) == EOF)
X		{
X		  fprintf (stderr, "Unexpected end of file in TEXT section\n");
X		  exit (1);
X		}
X	      else if (c == '\r')
X		{
X		  putchar ('\n');
X		}
X	      else
X		{
X		  putchar (c);
X		}
X	    }
X	  state = FIND_KEY;
X	  break;
X	  
X	case PROC_BINARY:
X	  length = 0;
X	  for (i = 0; i < 4; i++)
X	    {
X	      if ((c = getchar ()) == EOF)
X		{
X		  fprintf (stderr, "Unexpected end of file\n");
X		  exit (1);
X		}
X	      length |= (c << (8*i));
X	    }
X
X	  for (i = 0; i < length; i++)
X	    {
X	      if ((c = getchar ()) == EOF)
X		{
X		  fprintf (stderr, "Unexpected end of file in BINARY section\n");
X		  exit (1);
X		}
X	      else
X		{
X		  putchar (hex[(c & 0xf0) >> 4]);
X		  putchar (hex[(c & 0x0f)]);
X
X		  if (i % 32 == 31)
X		    {
X		      putchar ('\n');
X		    }
X		}
X	    }
X
X	  putchar ('\n');
X	  state = FIND_KEY;
X	  break;
X	}
X    }
X}
END_OF_FILE
if test 2438 -ne `wc -c <'pfb2pf.c'`; then
    echo shar: \"'pfb2pf.c'\" unpacked with wrong size!
fi
# end of 'pfb2pf.c'
fi
echo shar: End of shell archive.
exit 0
-- 
J.T. Conklin    jtc@motcad.portal.com, ...!portal!motcad!jtc