home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frozen Fish 2: PC
/
frozenfish_august_1995.bin
/
bbs
/
d03xx
/
d0342.lha
/
Softfont
/
portolan_cli.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-04-15
|
12KB
|
402 lines
/***************************************************************
* convert.c portrait font to landscape HP LaserJet II format
* Compiled under Lattice C Compiler vers 5.04
*
* Call: convert(portrait[file],landscape[file])
*
* by Tom Lynch,
* version 4 April 90
***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <exec/types.h>
#include <libraries/dos.h>
#define MAX 200 /* byte X byte array for individual character image */
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 debug;
unsigned int n = 0; /* value counter */
unsigned char c; /* character read from portrait font file */
unsigned char rdchar(); /* define function to return char */
unsigned char skip(); /* define function to return char */
unsigned char change(); /* define function to return char */
unsigned int measureto();
unsigned int _measureto();
char bitmap(); /* read, rotate, and measure length of image in bytes */
unsigned char base2[10][1]; /* temporary storage for binary string */
unsigned char _read();
TEXT file_name[34];
TEXT dir_name[67];
TEXT portrait[67];
TEXT landscape[67];
TEXT slash[10] = "/";
int SLASH = '\057'; /* decimal 42, forward slash (UNIX, AMIGA) */
char *LAND = ".land";
char *temp;
unsigned char far map[MAX][MAX][1]; /* 'far' is REQUIRED for large arrays */
void main(argc, argv)
int argc;
char *argv[];
{
extern convert();
int error;
if (argc > 1) {
if (argc == 3) {
if (error = convert(argv[1],argv[2]) != 0) { /* process the request */
printf("Failure to process font\n");
printf("\nError message: %d\n ",error);
switch(error) {
case '4':
printf("Failure to open portrait font file\n");
break;
case '5':
printf("Landscape file exists; rename.\n");
break;
case '6':
printf("Not font file?? First char should be 'escape'\n");
break;
default:
break; }
exit(0);
}
printf("\nFont converted: %s --> %s\n",argv[1],argv[2]);
exit(0);
}
else {
printf("\nArgument ERROR. Correct form is:");
printf("\n'path'PORTOLAN PORTRAIT LANDSCAPE\n\n");
exit(0); }
}
}
convert(p,l) /* CLI format: PORTOLAN font.port(old) font.land(new) */
char *p;
char *l;
{
extern unsigned char c; /* reference externals */
extern FILE *fp, *fl;
extern unsigned int n;
extern unsigned char rdchar();
extern unsigned char skip();
extern unsigned char change();
extern char bitmap();
extern unsigned int measureto();
extern unsigned int _measureto();
extern unsigned char _read();
extern unsigned char base2[][];
unsigned char descriptor[20][1];
unsigned int LeftOffset;
unsigned int TopOffset;
unsigned int width, h_dots_p;
unsigned int height, w_bytes_p;
int size;
unsigned int w_dots; /* landscape width in dots equal to height */
unsigned int w_bytes; /* integral number of bytes */
unsigned int h_dots; /* landscape height equal to portrait */
unsigned int h_bytes;
int a,h,i;
char *res = "01234567890";
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 (esc) 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' */
/* store char descriptor; must measure length to correct old length
then write new descriptor */
_measureto('W'); /* read char descriptor length */
size = n - 16; /* bitmap size */
descriptor[0][1] = 'W';
_read(); descriptor[1][1] = c; /* Format byte = 4 */
_read(); descriptor[2][1] = c; /* no continuation byte = 0 */
_read(); descriptor[3][1] = c; /* descriptor size = 14 */
_read(); descriptor[4][1] = c; /* class, format of data = 1 */
_read(); descriptor[5][1] = '\001'; /* Orientation 1 = land */
_read(); descriptor[6][1] = c; /* reserved by HP = 0 */
/* left offset (portrait) = */
_read(); descriptor[7][1] = c; /* left offset (P) hi byte */
_read(); descriptor[8][1] = c; /* left offset (P) lo byte */
/* top offset (port) = */
_read(); descriptor[9][1] = c; /* top offset (P) hi byte */
_read(); descriptor[10][1] = c; /* top offset (P) lo byte */
/* portrait width is read into landscape height descriptor bits */
_read(); descriptor[13][1] = c; /* char width land hi */
_read(); descriptor[14][1] = c; /* char width land hi */
/* land */ height = 256 * descriptor[13][1] + descriptor[14][1]; /* dots */
h_dots = height; /* landscape height pixels */
h_bytes = height/8; if(height % 8) h_bytes += 1; /* bytes */
w_bytes_p = h_bytes;
/* portrait height is read into landscape width descriptor bits */
_read(); descriptor[11][1] = c; /* char height land */
_read(); descriptor[12][1] = c; /* char height land */
/* land */ width = 256 * descriptor[11][1] + descriptor[12][1]; /* dots */
w_dots = width;
h_dots_p = width;
w_bytes = width/8; if(width % 8) w_bytes += 1; /* bytes */
_read(); descriptor[15][1] = c; /* Delta X hi byte */
_read(); descriptor[16][1] = c; /* Delta X lo byte */
/* top offset (land) = left offset(P) + char width(P) - 1 + byte offset, 16 bit hex */
TopOffset = 256 * descriptor[7][1] + descriptor[8][1] +
256 * descriptor[13][1] + descriptor[14][1] - 1;
/* left offset (landscape) = 0xFFFF - top offset(P) 16 bit hex */
LeftOffset = 65535 - (256 * descriptor[9][1] + descriptor[10][1]);
descriptor[7][1] = (unsigned char) (LeftOffset / 256); /* hi byte */
descriptor[8][1] = (unsigned char) (LeftOffset % 256); /* lo byte */
descriptor[9][1] = (unsigned char) TopOffset / 256; /* hi byte */
descriptor[10][1] = (unsigned char) TopOffset % 256; /* lo byte */
/* estimate of bit image size after rotation 8/
/* landscape bit_image size = width[bytes] * height[dot-rows] */
n = w_bytes * h_dots + 16; /* bytes for descriptor and bit image */
h = stci_d(res,n); /* convert to decimal string */
a = 1; if(n>9) a = 2; if(n>99) a = 3; if(a>999) a = 4;
/* write char size to landscape file, ascii format */
for(i=0; i<a; i++) { /* truncate escape byte with 'i<a' */
h = fputc(res[i],fl);
}
/* write descriptor string to landscape file */
for(i=0; i<=16; i++) {
h = fputc(descriptor[i][1],fl);
}
/* read image, rotate, and write bitmap to landscape file */
bitmap(w_bytes, h_bytes, h_dots, h_dots_p, w_bytes_p);
rdchar();
} /* 'while' loop */
printf("Font %s created\n",l);
c = fcloseall(); /* if filend = 0, success */
return 0;
} /* main() */
/* read bit image of font character and store in map[height(L)][width(L)][1] array */
char bitmap(w_bytes, h_bytes, h_dots, h_dots_p, w_bytes_p)
unsigned w_bytes;
unsigned h_bytes;
unsigned h_dots;
unsigned h_dots_p;
unsigned w_bytes_p;
{
extern unsigned char c;
extern FILE *fp,*fl;
unsigned int a,b;
unsigned char ZERO = '\000';
int i,j,k,m,p,q;
/* clear only necessary byte pad work space to zero
in preparation for reading character data.
This is along bottom of portrait character */
for(i=h_dots_p; i<=h_dots_p+8; i++) {
for(j=0; j<=w_bytes_p +1; j++) {
map[j][i][1] = ZERO;
}
}
/* read portrait format pixel data into workspace */
for(i=1; i<=h_dots_p; i++) { /* rows in font image */
for(j=1; j<=w_bytes_p; j++) {
_read();
map[j][i][1] = c; /* map[w_field(P)][dot-row height(P)] */
} /* column fields */
} /* rows down image starting with index of one 1*/
/* write rotated pixel data to landscape file
This routine forms bytes of rotated image along the
landscape image width axis. The excess byte padding
in the origonal portrait image is stripped off; i.e.
the pixel data is taken from h_dots, not h_bytes.
h_dots is equal to portrait image width in dots. */
for(p=h_dots; p>=1; p--) {
i = p / 8; /* points to byte, contingent to mod below */
q = p % 8; /* points to bit as computed in switch below */
switch(q) {
case 0: /* points to next lower byte, bit = LSB */
k = 1; /* k = value of bit position of */
break; /* byte used to mask correct bit */
case 1: /* points to next higher byte, msb */
i += 1;
k = 128; /* first bit is MSB in HP format */
break;
case 2:
i += 1;
k = 64;
break;
case 3:
i += 1;
k = 32;
break;
case 4:
i += 1;
k = 16;
break;
case 5:
i += 1;
k = 8;
break;
case 6:
i += 1;
k = 4;
break;
case 7:
i += 1;
k = 2;
break;
} /* there is no possible default */
for(j=0; j<w_bytes; j++) {
c = 0; /* c is used to form byte for landscape */
for(m=0,a=128; m<=7; m++,a/=2) {
b = (unsigned int) map[i][j*8+m][1];
c += a*((k & b)>0); /* calculate byte value */
/* extract bit from portrait and
assign value 'a' for bit */
} /* bit of pixel data along old port height */
n = fputc(c,fl); /* put computed byte
into landscape file */
} /* j = byte along width (old portrait height) */
/* k = bit pixel data value used in bitwise mask */
} /* i = byte along height (old portrait width) from max */
return NULL;
}
unsigned char rdchar () { /* read byte store in char 'c' */
extern unsigned 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;
}
unsigned char _read() { /* read byte store in char 'c' */
extern unsigned char c;
extern FILE *fp;
filepos++;
c = fgetc(fp);
if ((filend = feof(fp)) != 0) return 0;
return c;
}
unsigned char skip(int n) { /* skip 'n' bytes in file */
extern unsigned 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;
}
unsigned char change(int prev, int new) /* change byte */
{
extern unsigned 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;
}
unsigned int measureto (char test) /* calculate ascii format number (base 10) */
{
unsigned char rdchar();
extern unsigned 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 */
}
unsigned int _measureto (char test) /* calculate ascii format number (base 10) */
{
unsigned char _read();
extern unsigned char c;
_read();
n = (c - '0'); /* convert from ASCII to integer */
_read(); /* good for 1 to 5 character number */
if (c == test) return n;
n = n*10 + (c - '0');
_read();
if (c == test) return n;
n = n*10 + (c - '0');
_read();
if (c == test) return n;
n = n*10 + (c - '0');
_read();
if (c == test) return n;
n = n*10 + (c - '0');
_read(); /* this will read 'test' character */
return n; /* with 4 character ascii number */
}