/* $Log:	downloadfont.c,v $
 * Revision 0.8  92/11/23  19:46:43  19:46:43  bt (Bo Thide')
 * Fixed resolution bug. Portable downloading. Added/changed options. PJXL color support
 * 
 * Revision 0.7  92/11/13  02:41:27  02:41:27  bt (Bo Thide')
 * More bug fixes and improvements. Support for PaintJet XL
 * 
 * Revision 0.6  92/11/10  21:47:44  21:47:44  bt (Bo Thide')
 * Bug fixes. Added -R option. Better font handling.
 * 
 * Revision 0.5  92/11/09  16:25:31  16:25:31  bt (Bo Thide')
 * Rewrite of dospecial.c. Extended \special support
 * 
 * Revision 0.4  92/11/08  02:45:46  02:45:46  bt (Bo Thide')
 * Changed to portable bit manipulations. Replaced strrstr for non-POSIX compliant C. Fixed numerous bugs. Added support for more \special's.
 * 
 * Revision 0.3  92/08/24  12:45:38  12:45:38  bt (Bo Thide')
 * Fixed 8 bit (dc font) support.
 * 
 * Revision 0.2  92/08/23  17:28:56  17:28:56  bt (Bo Thide')
 * Source cleaned up.  Changed certain function calls.  Removed globals.
 * 
 * Revision 0.1  92/08/22  23:58:47  23:58:47  bt (Bo Thide')
 * First Release.
 *  */

#include <math.h>
#include <stdio.h>
#include "globals.h"
#include "macros.h"
#include "pcl.h"

static char rcsid[] = "$Header: downloadfont.c,v 0.8 92/11/23 19:46:43 bt Exp $";

/*  Download a font to the printer */
long downloadfont(bitfile, permfontsdown, device)
FILE	*bitfile;
int	permfontsdown;
short	device;
{
	int	i;
	int	depth;
	int	max_depth;
	int	max_width;
	int	max_offset;
	long	memreq;
	double	fh4;
	
	max_depth = max_width = max_offset = 0;
	memreq = 4;

	/* Specify font ID = nextfontdown and download each character */
	fprintf(bitfile, PCL4_FONT_ID, font->down=nextfontdown--, fd.size);

	if(verbose)
		fprintf(stderr,"Downloading %d...\n",font->down);

	for(i = font->bc ; i <= font->ec ; i++)
		if((cbase + i)->use_count) { 
			pkcharinfo(i);
			if(c_voffset > max_offset) max_offset = c_voffset;
			if((depth = c_height-c_voffset) > max_depth)
			  max_depth = depth;
			if(c_width > max_width) max_width = c_width;
		}


	/* Determine the font descriptor values */
	fd.size = 64;
	fd.descriptor = 0;
	if(device == DEV_LJPLUS)
		fd.font_type = 1;
	else
		fd.font_type = 2;
	fd.style_MSB = 0;
	fd.reserved = 0;
	fd.baseline = (ushort)max_offset;
	fd.cell_width = (ushort)(max_width?max_width:1);
	fd.cell_height = (ushort)(max_depth?max_offset+max_depth:max_offset+1);
	fd.orientation = (ubyte)(landscape ? 1 : 0);
	fd.spacing = 1;
	fd.symbol_set = (ushort)(DYNAMICFONTBASE-nextfontdown-permfontsdown)<<5;
	fd.pitch = fd.cell_width << 2;
	fh4 = 4.0*font->height;			/* Temp variable */
	fd.height = (ushort)fh4;
	pkcharinfo('x');   			/* Get global values for 'x' */
	fd.x_height = (ushort)c_height;		/* c_height is for 'x' here! */
	fd.width_type = 0;
	fd.style_LSB = 0;
	fd.stroke_weight = 0;
	fd.typeface_LSB = 0;
	fd.typeface_MSB = 0;
        fd.serif_style = 128;	/* Serif */
        fd.quality = 0;
        fd.placement = 0;
	fd.underl_dist = (byte)-max_depth;
        fd.underl_height = 0;
	fd.text_height = fd.cell_height << 2;
	fd.text_width = fd.cell_width << 2;
        fd.first_code = 1;
        fd.last_code = 255;
        fd.pitch_ext = 0;
	fd.height_ext = (ushort)(1024*(fh4 - fd.height));
        fd.cap_height = 0;
        fd.font_number = 0;
        strcpy(fd.font_name,"-TeX dvi font-");

#ifdef DEBUG2
fprintf(stderr,"downloadfont: fd.size = %d\n", fd.size);
fprintf(stderr,"downloadfont: fd.descriptor = %d\n", fd.descriptor);
fprintf(stderr,"downloadfont: fd.font_type = %d\n", fd.font_type);
fprintf(stderr,"downloadfont: fd.style_MSB = %d\n", fd.style_MSB);
fprintf(stderr,"downloadfont: fd.reserved = %d\n", fd.reserved);
fprintf(stderr,"downloadfont: fd.baseline = %d\n", fd.baseline);
fprintf(stderr,"downloadfont: fd.cell_width = %d\n", fd.cell_width);
fprintf(stderr,"downloadfont: fd.cell_height = %d\n", fd.cell_height);
fprintf(stderr,"downloadfont: fd.orientation = %d\n", fd.orientation);
fprintf(stderr,"downloadfont: fd.spacing = %d\n", fd.spacing);
fprintf(stderr,"downloadfont: fd.symbol_set = %d\n", fd.symbol_set);
fprintf(stderr,"downloadfont: fd.pitch = %d\n", fd.pitch);
fprintf(stderr,"downloadfont: fd.height = %d\n", fd.height);
fprintf(stderr,"downloadfont: fd.x_height = %d\n", fd.x_height);
fprintf(stderr,"downloadfont: fd.width_type = %d\n", fd.width_type);
fprintf(stderr,"downloadfont: fd.style_LSB = %d\n", fd.style_LSB);
fprintf(stderr,"downloadfont: fd.stroke_weight = %d\n", fd.stroke_weight);
fprintf(stderr,"downloadfont: fd.typeface_LSB = %d\n", fd.typeface_LSB);
fprintf(stderr,"downloadfont: fd.typeface_MSB = %d\n", fd.typeface_MSB);
fprintf(stderr,"downloadfont: fd.serif_style = %d\n", fd.serif_style);
fprintf(stderr,"downloadfont: fd.quality = %d\n", fd.quality);
fprintf(stderr,"downloadfont: fd.placement = %d\n", fd.placement);
fprintf(stderr,"downloadfont: fd.underl_dist = %d\n", fd.underl_dist);
fprintf(stderr,"downloadfont: fd.underl_height = %d\n", fd.underl_height);
fprintf(stderr,"downloadfont: fd.text_height = %d\n", fd.text_height);
fprintf(stderr,"downloadfont: fd.text_width = %d\n", fd.text_width);
fprintf(stderr,"downloadfont: fd.first_code = %d\n", fd.first_code);
fprintf(stderr,"downloadfont: fd.last_code = %d\n", fd.last_code);
fprintf(stderr,"downloadfont: fd.pitch_ext = %d\n", fd.pitch_ext);
fprintf(stderr,"downloadfont: fd.height_ext = %d\n", fd.height_ext);
fprintf(stderr,"downloadfont: fd.cap_height = %d\n", fd.cap_height);
fprintf(stderr,"downloadfont: fd.font_number = %d\n", fd.font_number);
fprintf(stderr,"downloadfont: fd.font_name = %s\n", fd.font_name);
#endif /* DEBUG2 */

	/* Send font descriptor escape sequence to printer */
	fprintf(bitfile, PCL4_FONT_DESCR, fd.size);

	/*
	* Download each font descriptor value individually to avoid 
	* possible struct alignment problems.
	*/
	fwrite((void *)&fd.size, sizeof(fd.size), 1, bitfile);
	fwrite((void *)&fd.descriptor, sizeof(fd.descriptor), 1, bitfile);
	fwrite((void *)&fd.font_type, sizeof(fd.font_type), 1, bitfile);
	fwrite((void *)&fd.style_MSB, sizeof(fd.style_MSB), 1, bitfile);
	fwrite((void *)&fd.reserved, sizeof(fd.reserved), 1, bitfile);
	fwrite((void *)&fd.baseline, sizeof(fd.baseline), 1, bitfile);
	fwrite((void *)&fd.cell_width, sizeof(fd.cell_width), 1, bitfile);
	fwrite((void *)&fd.cell_height, sizeof(fd.cell_height), 1, bitfile); 
	fwrite((void *)&fd.orientation, sizeof(fd.orientation), 1, bitfile);
	fwrite((void *)&fd.spacing, sizeof(fd.spacing), 1, bitfile);
	fwrite((void *)&fd.symbol_set, sizeof(fd.symbol_set), 1, bitfile);
	fwrite((void *)&fd.pitch, sizeof(fd.pitch), 1, bitfile);
	fwrite((void *)&fd.height, sizeof(fd.height), 1, bitfile);
	fwrite((void *)&fd.x_height, sizeof(fd.x_height), 1, bitfile);
	fwrite((void *)&fd.width_type, sizeof(fd.width_type), 1, bitfile);
	fwrite((void *)&fd.style_LSB, sizeof(fd.style_LSB), 1, bitfile);
	fwrite((void *)&fd.stroke_weight, sizeof(fd.stroke_weight), 1, bitfile);
	fwrite((void *)&fd.typeface_LSB, sizeof(fd.typeface_LSB), 1, bitfile);
	fwrite((void *)&fd.typeface_MSB, sizeof(fd.typeface_MSB), 1, bitfile);
	fwrite((void *)&fd.serif_style, sizeof(fd.serif_style), 1, bitfile);
	fwrite((void *)&fd.quality, sizeof(fd.quality), 1, bitfile);
	fwrite((void *)&fd.placement, sizeof(fd.placement), 1, bitfile);
	fwrite((void *)&fd.underl_dist, sizeof(fd.underl_dist), 1, bitfile);
	fwrite((void *)&fd.underl_height, sizeof(fd.underl_height), 1, bitfile);
	fwrite((void *)&fd.text_height, sizeof(fd.text_height), 1, bitfile);
	fwrite((void *)&fd.text_width, sizeof(fd.text_width), 1, bitfile);
	fwrite((void *)&fd.first_code, sizeof(fd.first_code), 1, bitfile);
	fwrite((void *)&fd.last_code, sizeof(fd.last_code), 1, bitfile);
	fwrite((void *)&fd.pitch_ext, sizeof(fd.pitch_ext), 1, bitfile);
	fwrite((void *)&fd.height_ext, sizeof(fd.height_ext), 1, bitfile);
	fwrite((void *)&fd.cap_height, sizeof(fd.cap_height), 1, bitfile);
	fwrite((void *)&fd.font_number, sizeof(fd.font_number), 1, bitfile);
	fwrite((void *)fd.font_name, sizeof(fd.font_name), 1, bitfile);

	/* Download each character of curfont which occurs in the .dvi file */
	for(i = font->bc ; i <= font->ec ; i++)
		if((cbase + i)->use_count)
			memreq += downloadchar(bitfile, i, device);
	fprintf(bitfile, PCL4_MAKE_FONT_TEMP);	/* Make the font temporary */
	return(memreq);
}
