[comp.lang.postscript] Was: Binary printer fonts, now pfbtopfa.c

pgd@bbt.se (P.Garbha) (07/25/90)

In article <929@ssc.UUCP> fyl@ssc.UUCP (Phil Hughes) writes:
>                            The printer font is in a binary format (PFB
>extension).  if I download it as is to the printer (along with a program
>to print out the bounding box info, etc) it pukes.  My guess is that I
>need to translate it to a "PFA" format.
>

Here follows a small program to convert from PFB to PFA format. Note that 
it is a fast hack, so it carries no guarantees. 

#include <stdio.h>
#include <strings.h>

/*
 * Program pfbtopfa.c
 *
 * Convert an adobe .PFB file to a .PFA file
 */

main(argc, argv)
	int argc;
	char **argv;
{
	int	c;
	extern int optind;
	extern char	*optarg;
	int errflg = 0;
	
	while ((c = getopt (argc, argv, "")) != EOF)
		switch	(c) {
		case '?':
			errflg++;
			break;
		}
	if (errflg) {
		fprintf (stderr, "usage: . . .	");
		exit (1);
	}
	for( ; optind < argc; optind++) {
		convert(argv[optind]);
	}
}


convert(font)
	char *font;
{
	char pfaname[20];
	char *cp;
	FILE *ifp, *ofp;
	int len, ft;
	register c, i;
	int j, k;

	strcpy(pfaname, font);
	cp = strrchr(pfaname, '.');
	if (cp == NULL)
		cp = pfaname + strlen(pfaname);
	if (strcmp(cp, ".PFB") != 0 && strcmp(cp, ".pfb") != 0) {
		*cp = '\0';
		fprintf(stderr, "Font '%s' has to have name '%s.PFB'\n",
			font, pfaname);
		return;
	}
	strcpy(cp, strcmp(cp, ".PFB") == 0 ? ".PFA" : ".pfa");
	if ((ifp = fopen(font, "r")) == NULL) {
		perror(font);
		return;
	}
	if ((ofp = fopen(pfaname, "w")) == NULL) {
		perror(pfaname);
		return;
	}
	ft = 0;
	while (ft != 3) {
		c = getc(ifp);
		if (c == -1) {
			fprintf(stderr, "Premature eof on file %s\n", font);
			break;
		}
		if (c != 128) {
			fprintf(stderr, "Illegal format on font %s\n", font);
			break;
		}
		c = getc(ifp);
		if (c == -1) {
			fprintf(stderr, "Premature eof on file %s\n", font);
			break;
		}
		if (c < 1 || c > 3) {
			fprintf(stderr, "Illegal format on font %s\n", font);
			break;
		}
		ft = c;
		len = 0;
		if (ft != 3) {
			for (i = 0; i < 4; i++) {
				c = getc(ifp);
				if (c == -1) {
					fprintf(stderr, "Premature eof on file %s\n", font);
					goto fin;
				}
				len |= (c << (i*8));
			}
		}
/* printf("Segment type %d, len %d\n", ft, len); */
		switch (ft) {
		case 1:
			for (k = 0; k < len; k++) {
				c = getc(ifp);
				if (c == -1) {
					fprintf(stderr, "Premature eof on file %s\n", font);
					goto fin;
				}
				if (c == '\r')
					c = '\n';
				putc(c, ofp);
			}
			break;

		case 2:
			j = 0;
			for (k = 0; k < len; k++) {
				c = getc(ifp);
				if (c == -1) {
					fprintf(stderr, "Premature eof on file %s\n", font);
					goto fin;
				}
				i = c & 0xf;
				c >>= 4;
				if (c > 9)
					putc(c - 10 + 'A', ofp);
				else
					putc(c + '0', ofp);
				if (i > 9)
					putc(i - 10 + 'A', ofp);
				else
					putc(i + '0', ofp);
				j++;
				if (j == 80) {
					putc('\n', ofp);
					j = 0;
				}
			}
			break;

		case 3:
			goto fin;
		}
	}
 fin:
	fclose(ifp);
	fclose(ofp);
}