/*************************************************************** 
 *  convert.c     portrait font to landscape  HP LaserJet II format 
 *	Compiled under Lattice C Compiler vers 5.04 
 *
 * Call: convert(portrait,landscape)
 *
 *  by Tom Lynch, 
 *  25 January 1989
 ***************************************************************/

#include <stdio.h>
#include <exec/types.h>
#include <libraries/dos.h>

extern long DOSBase;

FILE *fp,*fl;

int filepos = 0; /* position in file being read  (32 bits in AMIGA */
int filend = 0; /* file end flag  */
int n = 0;	/* value counter */

char c;		/* character read from portrait font file */
char rdchar(); 	/* define function to return char */
char skip(); 	/* define function to return char */
char change(); 	/* define function to return char */


char convert(char *p, char *l)	/* CLI format: PORTOLAN font.port(old) font.land(new) */
	     {

	extern char c;		/* reference externals */
	extern FILE *fp, *fl; 
	extern int n;
	extern char rdchar();
	extern char skip();
	extern char change();
	extern int measureto();

	DOSBase = OpenLibrary("dos.library",0);
       
	if ((fp = fopen(p,"r")) == NULL)	{
             return(4); 
	     }
       	if ((fl = fopen(l,"w")) == NULL)	{
             return(5); 
	     }
	     
	/* files ready, convert portrait file to landscape */

	rdchar();	  /* look for initial escape character */
	if (c != (char)'\033') return(6);
       	skip(2);	  /* esc, ')', and 's' of font descriptor */
       	measureto ('W');  /* read index 'n' that points to next esc */
       	skip(12);	  /* skip to orientation byte */
	change(0,1);	  /* change from port(0) to land(1) */
	skip(n-13);	  /* skip to end of font descriptor */
	rdchar();	  /* read first byte of charater descriptor */
	while (filend == 0) {	/* test for end of charaters in font */
		skip (2);	/* '*' and 'c' of char descriptor */
		measureto('E');	/* read charater number */
		skip(3);	/* bypass esc, '(', 's' */
		measureto('W');	/* read char descriptor length */
		skip(1);	/* skip length byte */
		rdchar();	/* read continuation byte */
		if (c == 0) {	/*   field =0 for new char */
			skip(2);  	/* skip descriptor size, class */
			change(0,1); 	/* change charater orientation */
		}
		else	{	/* data from prev char if data ln>32768 bytes */
			skip(3);	/* therefore skip over orientation byte */
		}
             	skip (n-5);
             	rdchar ();	/* read, expecting escape for next char */
	}
		c = fcloseall();	/* if filend = 0, success */
		return NULL;
}

char rdchar () {	/* read byte store in char 'c' */
	extern char c;
	extern FILE *fp, *fl; 
	int r;
	filepos++;
	c = fgetc(fp);
	if ((filend = feof(fp)) != 0) 	return 0; 
	r = fputc(c,fl);
     return   c;
}

char skip(int n)      {		/* skip 'n' bytes in file */
	extern char c;
	extern FILE *fp, *fl; 
	int x, r;
	for (x = 0; x < n; ++x)
		{ 
		filepos++;
		c = fgetc(fp);
		if ((filend = feof(fp)) != 0) return 0; 
		r = fputc(c,fl);
	}
	return c;
} 
       
char change(int prev, int new) /* change byte  */
       {
	extern char c;
	extern FILE *fp, *fl; 
	int r;
	filepos++;
	c = fgetc(fp);
	if (c == prev) r = fputc(new,fl);
	else  r = fputc(new,fl);
	return c;
} 



measureto (char test)	/* calculate ascii format number (base 10) */
       {
	char rdchar();
	extern char c;
	rdchar();
	n = (c - '0');		/* convert from ASCII to integer */
	rdchar();			/* good for 1 to 5 character number */
	if (c == test) return n;
	n = n*10 + (c - '0');
	rdchar();
	if (c == test) return n;
	n = n*10 + (c - '0');
	rdchar();
	if (c == test) return n;
	n = n*10 + (c - '0');
	rdchar();
	if (c == test) return n;
	n = n*10 + (c - '0');
	rdchar();			/* this will read 'test' character */
	return n;			/* with 5 character ascii number   */
}
