#define banner "\nThis is hbf2cjk Version 2.2  (c) 1994 by Werner Lemberg\n  Modified by David Fong to allow independent x and y resolutions\n\n" \

/* Modified by David Fong 20-Jan-95.

   Allows font size (initially defined by DSIZE), to be
   separately defined as an argument.

   Also allows x-resolution and y-resolution to be defined
   independently.

   Actually, `allows' is a misnomer.  Font sizes and separate
   x and y resolutions MUST be supplied as arguments.

   Created filenames are highly MS-DOS incompatible.  This program
   is really designed for superior computers, like the Amiga and Unix
   machines, (I'll grudgingly accept Macintosh and OS/2 if they fix
   up their software and hardware architecture inadequacies respectively).

   */

#define SCALE_FACTOR 0.0144
#define DSIZE_FACTOR 1.2 \
 \

#define FirstByte(code)((code)>>8)
#define SecondByte(code)((code)&0xff)
#define MakeCode(byte1,byte2)(((byte1)<<8)|(byte2)) \

#define hbfWidth(hbf)(hbfBitmapBBox(hbf)->hbf_width)
#define hbfHeight(hbf)(hbfBitmapBBox(hbf)->hbf_height) \

#define DSIZE 10 \

/*4:*/

/*5:*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hbf.h"
#include "hbf.c"

/*:5*/
;
/*6:*/

void makepk(void);


/*:6*//*8:*/

unsigned char*box(HBF*);


/*:8*//*10:*/

void makepbm(HBF*,HBF_CHAR);

/*:10*/
;

double xres,yres,fsize,xscale,yscale,threshold;  /* added fsize, a replacement for DSIZE */
char fname[100],family[10],encoding[10]; /* resplaced res and scale with xres, yres, xscale and yscale */
char parameters[100],options[100],optfile[100],command[500];
FILE*opt;
int hi,lo;


int main(argc,argv)
int argc;
char*argv[];
{HBF*hbf;


printf(banner);

if(argc!=8)
{fprintf(stderr,
"Usage: hbf2cjk HBF_file fontname x-resolution y-resolution size family encoding\n\n");
exit(-1);
}

hbfDebug= 1;
if((hbf= hbfOpen(argv[1]))==NULL)
exit(1);
hbfDebug= 0;

strcpy(fname,argv[2]);    /* David Fong - used to be strncpy, but
                             I've also changed fname's length to 100 to
                             allow long filenames and path definitions
                             to be given in the argument */
xres= atof(argv[3]);
yres= atof(argv[4]);
fsize= atof(argv[5]);
strncpy(family,argv[6],10);
strncpy(encoding,argv[7],10);

/* Perhaps I should have repaced hbfWidth with
   hbfHeight in the second line following this comment?
   As far as I know, (and I haven't really looked),
   Chinese characters are designed to fit squares.
   And if they aren't, using hbfHeight in the second
   line may result in a `distorted' scale anyway! - DF */

xscale= SCALE_FACTOR*fsize*xres/hbfWidth(hbf);
yscale= SCALE_FACTOR*fsize*yres/hbfWidth(hbf);

threshold= 0.501961;

/* the second parameter in the following line is supposed
   to specify `yoff'.  In this case, I think yres is
   appropriate - DF */

sprintf(parameters,"* %.0f * * 1.02 0.75 0.1 *",
DSIZE_FACTOR*fsize*yres/100);

/* This program requires a modified version of pbmtopk.
   The modified pbmtopk (yes, I modified it) requires
   and additional compulsory argument to specify y-resolution - DF */

sprintf(options,
"%d %d -s%f -p7 0 0.5 0 0 0.5 1.0 0.5 -C \"%s\" -F \"%s\" ",
(int)xres,(int) yres,(double)fsize,encoding,family);

hi= -1;

hbfForEach(hbf,makepbm);
makepk();

printf("Done\n");
hbfClose(hbf);
exit(0);
}


/*:4*//*7:*/

void makepk(void)
{fclose(opt);
/* This part is a little personal.  If the x-resolution
   and the y-resolution are the same, then created files
   are of the form `jsfs1001.360pk', where 360 is the resolution.
   However, if the x-resolution and y-resolution are NOT
   the same, then the created filenames are of the form
   `jsfs1210.600x300pk', where 600x300 is the resolution */

if (xres == yres) {
 sprintf(command,"pbmtopk %s%02i.%dpk %s%02i.tfm %s -f %s",
 fname,hi,(int)xres,fname,hi,options,optfile);
 }
else {
 sprintf(command,"pbmtopk %s%02i.%dx%dpk %s%02i.tfm %s -f %s",
 fname,hi,(int)xres,(int)yres,fname,hi,options,optfile);
 }
system(command);
printf("Cleaning up ...\n");
sprintf(command,"delete t:*.opt t:*.pbm quiet");
system(command);
}


/*:7*//*9:*/

unsigned char*box(hbf)
HBF*hbf;
{unsigned char*buf;
unsigned char bit[8]= {128,64,32,16,8,4,2,1};
int size,rowsize,endofrow,i,j;


size= HBF_BitmapSize(hbf);
buf= (unsigned char*)malloc(size);
rowsize= HBF_RowSize(hbf);
endofrow= (hbfWidth(hbf)+7)%8;

for(i= 0;i<rowsize;i++)
buf[i]= 0xff;
while(i<size-rowsize)
{buf[i++]= bit[0];
for(j= 1;j<rowsize;j++)
buf[i++]= 0;
buf[i-1]|= bit[endofrow];
}
while(i<size)
buf[i++]= 0xff;
return buf;
}


/*:9*//*11:*/

void makepbm(hbf,code)
HBF*hbf;
HBF_CHAR code;
{char pbmfile[100],rawpbmfile[100];
FILE*pbm;
unsigned char*bitmap;


if((int)FirstByte(code)>hi)
{if(hi>=0)
makepk();
hi= (int)FirstByte(code);
/* Here is the `personal' bit again.  The filename of the
   created font depends on whether the x-resolution and
   the y-resolution are the same */
if (xres == yres) {
 printf("Creating %s%02i.%dpk and %s%02i.tfm ...\n",
 fname,hi,(int)xres,fname,hi);
 }
else {
 printf("Creating %s%02i.%dx%dpk and %s%02i.tfm ...\n",
 fname,hi,(int)xres,(int)yres,fname,hi);
 }
printf("Preparing inputs for PBMtoPK ...\n");
sprintf(optfile,"t:%02i.opt",hi);
opt= fopen(optfile,"w");
}

lo= (int)SecondByte(code);

fprintf(opt,"=%d\n",lo);
sprintf(pbmfile,"t:%02i%02x.pbm",hi,lo);
sprintf(rawpbmfile,"t:raw%02i%02x.pbm",hi,lo);
fprintf(opt,"%s %s\n",pbmfile,parameters);

pbm= fopen(rawpbmfile,"w");
fprintf(pbm,"P4 %d %d ",hbfWidth(hbf),hbfHeight(hbf));

bitmap= (unsigned char*)hbfGetBitmap(hbf,code);
if(bitmap==NULL)
bitmap= box(hbf);
fwrite(bitmap,1,HBF_BitmapSize(hbf),pbm);
fclose(pbm);

sprintf(command,"pnmscale -xscale %f -yscale %f %s >t:pnmscale",xscale,yscale,rawpbmfile);
system(command);
sprintf(command,"pgmtopbm -t -v %f t:pnmscale >%s",threshold,pbmfile);
system(command);
sprintf(command,"delete t:pnmscale quiet");
system(command);
}


/*:11*/

