#define banner "\nThis is hbf2hbf Version 0.2  (c) 1994 by Werner Lemberg\n\n" \

/*3:*/

/*4:*/

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

/*:4*/
;


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

{/*6:*/

HBF*hbf;
FILE*out1,*out2;
char font_name[1024],hbf_name[1024];


/*:6*//*9:*/

HBF_CHAR min_char,max_char;


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

char b2_codes[256];
unsigned char min_2_byte,max_2_byte;


/*:11*//*13:*/

int char_count= 0;

/*:13*/
;


printf(banner);

if(argc!=3)
{/*5:*/

fprintf(stderr,"Usage: hbf2bmf output_font_name hbf_file\n\n");
exit(-1);


/*:5*/
;
}

/*7:*/

hbfDebug= 1;

if((hbf= hbfOpen(argv[2]))==NULL)
exit(-1);

hbfDebug= 0;

strcpy(font_name,argv[1]);
strcat(font_name,".bmf");
if((out1= fopen(font_name,"wb"))==NULL)
{fprintf(stderr,
"Error: couldn't open output font file:\n"
"       %s\n"
"\n",font_name);
exit(-1);
}

strcpy(hbf_name,argv[1]);
strcat(hbf_name,".hbf");
if((out2= fopen(hbf_name,"wt"))==NULL)
{fprintf(stderr,
"Error: couldn't open output HBF header file:\n"
"       %s\n"
"\n",hbf_name);
exit(-1);
}


/*:7*/
;

/*10:*/

{const void*cp;
HBF_CHAR dummy;


cp= hbfGetCodeRange(hbf,NULL,&min_char,&max_char);
for(;cp!=NULL;cp= hbfGetCodeRange(hbf,cp,&dummy,&max_char))
;
}


/*:10*/
;
/*12:*/

{const void*b2r;
unsigned char dummy;
int i;


for(i= 0;i<256;i++)
b2_codes[i]= 0;

b2r= hbfGetByte2Range(hbf,NULL,&min_2_byte,&max_2_byte);
dummy= min_2_byte;
for(;b2r!=NULL;b2r= hbfGetByte2Range(hbf,b2r,&dummy,&max_2_byte))
{for(i= dummy;i<=max_2_byte;i++)
b2_codes[i]= 1;
}
}


/*:12*/
;

/*14:*/

{HBF_CHAR i;
int j;
unsigned int size;
const unsigned char*bitmap;


size= HBF_BitmapSize(hbf);

i= (min_char&0xff00)+min_2_byte;

while(i<=max_char)
{while((i&0xff)<=max_2_byte)
{if(b2_codes[i&0xff])
{bitmap= hbfGetBitmap(hbf,i);

if(bitmap==NULL)
{for(j= 0;j<size;j++)
fputc('\0',out1);
}
else
fwrite(bitmap,size,1,out1);

char_count++;
}
i++;
}
i+= 0xff-(max_2_byte-min_2_byte);
}
}

/*:14*/
;
/*15:*/

{const char*property;
HBF_BBOX*boxp;


property= hbfProperty(hbf,"HBF_START_FONT");
fprintf(out2,"HBF_START_FONT %s\n",property);
property= hbfProperty(hbf,"HBF_CODE_SCHEME");
fprintf(out2,"HBF_CODE_SCHEME %s\n",property);
property= hbfProperty(hbf,"FONT");
fprintf(out2,"FONT %s\n",property);
property= hbfProperty(hbf,"SIZE");
fprintf(out2,"SIZE %s\n",property);
boxp= hbfBitmapBBox(hbf);
fprintf(out2,"HBF_BITMAP_BOUNDING_BOX %i %i %i %i\n",
boxp->hbf_width,boxp->hbf_height,
boxp->hbf_xDisplacement,boxp->hbf_yDisplacement);
boxp= hbfFontBBox(hbf);
fprintf(out2,"FONTBOUNDINGBOX %i %i %i %i\n",
boxp->hbf_width,boxp->hbf_height,
boxp->hbf_xDisplacement,boxp->hbf_yDisplacement);

fprintf(out2,"CHARS %i\n",char_count);

fprintf(out2,"HBF_START_BYTE_2_RANGES 1\n");
fprintf(out2,"HBF_BYTE_2_RANGE 0x00-0xFF\n");
fprintf(out2,"HBF_END_BYTE_2_RANGES\n");
fprintf(out2,"HBF_START_CODE_RANGES 1\n");
fprintf(out2,"HBF_CODE_RANGE 0x0100-0x%04X %s 0\n",
char_count+0x100,font_name);
fprintf(out2,"HBF_END_CODE_RANGES\n");
fprintf(out2,"HBF_END_FONT\n");
}

/*:15*/
;

/*8:*/

hbfClose(hbf);
fclose(out1);
fclose(out2);


/*:8*/
;
}


/*:3*/
