/*
 *  TEX Device Driver  ver 2.02-
 *  copyright(c) 1988,89 by TSG, 1990- by SHIMA
 *  1991 changed by hideki
 *
 *  dd.h : common header file
 *
 *  need to include <stdio.h> previously
 */

/* flags */

#define MSDOS
 /* MS-DOS ver 3.x
     */

#ifndef _DEF_STDIO_H_
#include <stdio.h>
#define _DEF_STDIO_H_
#endif

/* standard definitons */

typedef enum {
	FALSE = 0, TRUE = 1
} BOOL;

#define uchar   unsigned char
#define uint    unsigned int
#define ulong   unsigned long
typedef void (*void_func_ptr) ();
typedef int (*int_func_ptr) ();

#define TRUE    1
#define FALSE   0
#define NOTHING 0
#define FAILURE (-1)

#ifndef SEEK_SET
#define SEEK_SET    0
#define SEEK_CUR    1
#define SEEK_END    2
#endif

#ifdef LIPS3
#define LBP
#endif
#ifdef ESCPAGE
#define LBP
#endif

#ifdef	LBP
#define	FDOWN
#else
#undef	JDWN
#endif

#ifdef	HIRES98
#define	PC9801
#endif

/*#define	NOTATEGAKI*/
/*#define	NOVFONTADJ*/

/* definitons for device driver */

#ifdef x68k
#define far
#define huge
#endif

#define BUFFER    uchar far
#define HUGE_BUF  uchar huge
 /*   Both BUFFER pointer and HUGE_BUF pointer are 32bit-pointer.
     *   HUGE_BUF will be used for larger buffers ( correctly speaking,
     * ones which keep more data than 64Kbytes ).
     *   And it is noteworthy that the pointers of these types are impossible
     * to express continually ( ex. BUFFER *a, *b ), because they are not
     * "typedef" but "#define."  This foolish error is because of 8086CPU,
     * which has only 16bit-pointer, so we must add "far" or "huge" for use of
     * 32bit-pointer. However, "far" and "huge" are permitted to "typedef."
     * How wonderful SEGMENT !!
     */
#ifdef	NOFLOAT
#define SIZE_PARA   long
#else
#define SIZE_PARA   double
#endif
 /*   This device driver calculate units with double.
     */
#define F_SIZE_PARA   long
 /*   This device driver calculate units with double.
     */
#define PIXEL       int
#define SCALED_PT   long
 /*   the units of pixel and scaled-point
     */

#define HEADER_DEPTH    0xe0000L
#define FOOTER_DEPTH    0xe0000L
 /*   the header depth and the footer depth ( scaled point )
     */

typedef enum {
	UNKNOWN, PK_FONT, PXL1001, PXL1002, PXL1003, PKD_FONT, JXL,
#ifdef VFD		/* Fonts before JXL should be unscalable */
	VFD_FONT,
#endif
	ROM_FONT, ZS_FONT,
#ifdef BEZIERFONT
	JG_FONT,
#endif
	JLBP, JIS_FONT, JIS_PK, JIS_PKH, GAIJI
} FONT_TYPE;

 /*   FONT_TYPE shows the type of fonts */

typedef enum {
	HORIZONTAL, VERTICAL
} PRINT_DIR;

 /*   Direction at printing, HORIZONTAL normally.
     */

typedef enum {
	PreviousPart, NextPart,
	PreviousPage, NextPage,
	NextPageDirection,
	EscapeQuit
} NEXT_ACTION;

 /*   These values are returned by device-output module, which says
     * what to do next.
     */

#define PREAMBLE struct PREAMBLE_REC far
 /*   Information about Preamble of a font-file
     */
struct PREAMBLE_REC {
	PIXEL width, height, pitch_offset, depth_offset;
	/*   the size of the character, and the offset for typesetting.
         */
	BUFFER *raster;
	/*   the pointer of Raster-data buffer
         */
	long tfm_width;
	int byte_width;
	/*   equal width/8, raised decimal
         */
	PIXEL shift_up_ptex, shift_right_ptex;
	/*  the offset when lateral-Kanji used (when in pTeX tate mode)
         */
	int rotate_ptex;
	/* the flag of rotation (when in pTeX tate mode)
         */
};

#define CHAR_INFO struct CHAR_INFO_REC far
 /*   This buffer is a linked-list one, which keeps pointers of unpacked
     * fonts.
     */
struct CHAR_INFO_REC {
	int code;
	/*   character code
         */
	CHAR_INFO *next_char;
	CHAR_INFO *former_char;
	/*   needless to say? */
	struct PREAMBLE_REC pre;
	/*   preamble of an unpacked font
         */
};

#define	f_HUGE	0x8000
#define	f_EMS	0x4000

#define BUF_INFO  struct BUF_INFO_REC
 /*   Information about buffers
     */
struct BUF_INFO_REC {
	BUFFER *start;
	BUFFER *current;
	BUFFER *end;
	/*   Each means starting, current, and ending pointer of the buffer.
         * Especially, 'end' means 'start+size', so the pointer above ( or
         * which equals )'end' will be out of the buffer.
         */
	long size;
	/*   'size' can have "larger" data ( above segment, hum !! ).
         */
	int type;
 	int	ems_bank;
 	int	ems_top;
 	int	ems_last;
 	BUFFER*	ems_start;
 	BUFFER*	ems_end;
};

typedef struct {
	long offset;
	int  number;
}

PAGE_INDEX_REC;

#define PAGE_INDEX PAGE_INDEX_REC far

 /* page index buffer */

#define DVIFILE_INFO struct DVIFILE_INFO_REC
 /*   Information about Preamble of DVI-file. ( to say the truth, this is
     * the same thing as Preamble itself. So, you'd better see
     * "TeX : The Program." )
     */
struct DVIFILE_INFO_REC {
	FILE *file_ptr;
	char *file_name;
	long post, last_bop;
	long num, den, mag;
	SCALED_PT l, u;
	int stack_depth;
	int total_page;
};

#define DIMENSION struct DIMENSION_REC
 /*  Information about device-output
     */

struct DIMENSION_REC {
	int dpi;
	/* is horizontal dot-per-inch ratio of the device
         */
	int DPI;
	/* is vertical dot-per-inch ratio of the device .
         */
	PIXEL max_width, max_height;
	/*   These tell us the maximum size the device can output.
         * They are represented by pixel-unit.
         */
	PIXEL output_width, output_height;
	/* is the final size to be output to the device
	 	 */
	PIXEL buf_width, buf_height;
	/*   These tell us the maximum size Bitmap-buffer can hold.
		 * oriented by text direction
         */
	PIXEL text_width, text_height;
	/*   These are the maxmum size directed in DVI-file.
         */
	int mag;
	/*  is the magnification for output. This is the same value as 'mag' in
         * DVI-file ( so equal the one in DVI_INFO ), when no '-mag' & '-half'
         * options directed.
         */
	int split;
	/*   Our Device-driver divides the text to several parts, if that can't
         * be output at once. 'split' is the number of parts. Then, if 'split'
         * holds '1', Device-driver will put the text at a time.
         */
	PRINT_DIR print_direction;
	/*   This shows the direction for output, responsing '-V' option.
         * So there is no meaning for PREVIEWER.
         */
	SIZE_PARA size_para;
	/* is size-parameter for conversion pixel<-->scaled pt.
         * ( equal "num * den / mag" )
         */
	PIXEL x_offset, y_offset;
	/*   the offset of the head of text. These are directed by '-X' and
         * '-Y' options.
         */
	PAGE_INDEX *page_index;
	/* the index of file-offset of pages in dvifile
         */
	int total_page;
	/* same as 'total_page' in DVI_INFO.
         */
	int start_page, end_page;
	/* the starting and the ending page to print.
         */
	int prt_type;
	/* the type of the printer, ESC/P, PC-PR, PC-MN, LIPS3 or Previewer etc
		 */
};

#define OUTPUT_INFO struct OUTPUT_INFO_REC
 /*   Information about output. Interpreting-module and Device-output-module
     * receive the pointer of this.
     */
struct OUTPUT_INFO_REC {
	FILE *dvifile_ptr;
	/* DVI-file pointer
         */
	long page_start_offset;
	/* the file-offset at the position the page starts from.
         */
	PRINT_DIR print_direction;
	/* equal the one in DIMENSION.
         */
	PIXEL h_0, v_0;
	/* the original point for output.
         */
	PIXEL width, height;
	/* the size for output.; it is assigned for all the size of the text
         * to be held in the buffer.
         */
	int byte_width, byte_height;
	/*  equal width/8 and height/8.
         */
	int split, page;
	/* the current part and page.
         */
	HUGE_BUF *bitmap_ptr;
	/* Bitmap pointer
         */
};

typedef struct {	/* KANJI fonts */
	int width;	/* width by dots  (without counting K_DBL) */
	int height;	/* height by dots (without counting K_DBL) */
	int size;	/* size by bytes = (width+7)/8*height */
	int base;	/* number of dots under base line */
	int fh;		/* file handle number */
	char *name;	/* Kanji font file_name */
}
KFONT;

typedef struct {
	int char_type;
	int id;
	long width;		/* character width */
	long height;	/* character height(length above the baseline) */
	long depth;		/* character depth (length under the baseline) */
	long italic;
}
JFM_DATA;

#define	KFONT_SIZE  sizeof(KFONT)

#define FONT_INFO struct FONT_INFO_REC far
 /*   Information about fonts. This is linked-list.
     */

union EXT {
	BUFFER *dir;	/* PXL1001, PXL1002, PXL1003
					   address of the directory for PXLFONT 	*/
	KFONT *kdir;	/* JLBP, JIS_FONT, JIS_PK, JIS_PKH, GAIJI	*/
	int fh;			/* JXL, PKD_FONT, VFD_FONT (file handle)	*/
					/* ZS_FONT, JG_FONT (use as ID)				*/
	long count;
};

#define	CHAR_ROOT	8		/* should be a power of 2 */

struct FONT_INFO_REC {		/* 4*CHAR_ROOT + 46 = 78 bytes
											   +  4 = 82 bytes (FDOWN)
											   +  6 = 88 bytes (NTTRPL)*/
	int font_code;
	/*   sent from dvi-file, when the font is defined.
         */
	FONT_TYPE font_type;
	/*   type of font.
         */
	int f_goth;
	/* flag for gothic etc.
		 */
	char *name;
	/*   the file-name of the font.
         */
	BUFFER *pk;
	/*   the pointer of unpacked data of the font.
         * it is NULL, when data is unloaded or flushed.
         */
	CHAR_INFO *char_info[CHAR_ROOT];
	/*   the pointers of the first CHAR_INFO of the font.
         * Also, it is NULL, when data is unloaded or flushed.
         */
	FONT_INFO *k_top;
	FONT_INFO *k_next;

	F_SIZE_PARA size_para;
	/*  design size.
         */
	FONT_INFO *next_font;
	/*   equal NULL, when no next font.
         */
	FONT_INFO *before_font;
	/*	 equal NULL, when no former font
		 */
	FONT_INFO *after_font;
	/*	 equal NULL, when no after font
		 */
	int k_width;
	/*   normal allowable maximal width of Kanji character by bits
	     *   counting k_scale
		 *   in PTeX TATE-KUMI fonts, it shows height of fonts.
		 */
	int k_height;
	/*   normal allowable maximal height of Kanji character by bits
	     *   counting k_scale
		 *   in PTeX TATE-KUMI fonts, it shows width of fonts.
		 */
	int last;
	/* last JIS CODE of Kanji font
		 */
	long c;
	/* checksum (id)
 		 */
	union EXT ext;

#ifdef	LBP
	int	lbp_font_inf;	/*0-3bit:フォント番号-80H, 
					  	  4,5:bold flag (0:normal, 1:1dot, 2:2dot)
					  	  6:ouline flag 8-15:フォント縦横比(0-200)*/
#endif
#ifdef	FDOWN
	uchar *d_flag;
#endif
#ifndef NO_NTTRPL
	int code_offset;
	int pt_size;
	int family_code;
#endif
};

#ifdef LBP
#ifdef FDOWN
typedef struct {	/*ダウンロード情報*/
	int assign_num;		/*割当番号*/
	int maxwidth;		/*フォント最大幅*/
	int maxheight;		/*フォント最大高*/
	int mincharcode;	/*最小コード*/
	int maxcharcode;	/*最大コード*/
	long total_send;	/*ダウンロードしたトータルバイト数*/
	int numoffonts;		/*ダウンロードしたフォントの数*/
	long total_off;		/*実サイズを使う場合のバイト数の差*/
	long total_mem;		/*セルサイズで計ったトータルバイト数*/
}
DL_INFO;

struct DJFONT_COUNT {	/*和文フォントの文字の出現頻度情報
						  : 8 byte -> should be even size*/
	uint code;						/*文字コード*/
	unsigned char font_code;		/*フォント番号*/
	unsigned char count;			/*出現回数-1*/
	uint after;						/*次のノード*/
	uint before;					/*前のノード*/
};

struct DJFONT_TABLE {	/*和文フォントのダウンロードした文字情報: 5 byte*/
	uint code;						/*元の文字コード*/
	unsigned char font_code;		/*元のフォント番号*/
	unsigned char new_code;			/*ダウンロードで埋め込んだ文字コード*/
	unsigned char new_font_code;	/*ダウンロードで埋め込んだフォント番号*/
};

#define	DJFONT	struct DJFONT_COUNT far
#define	DJTABLE	struct DJFONT_TABLE far

struct REAL_SIZE {	/*文字の左右の空白を削った実ボックスサイズ、左上が原点*/
	int left;			/*最左端のx-座標*/
	int	top;			/*最上端のy-座標*/
	int	right;			/*最右端のx-座標 + 1*/
	int	bottom;			/*最下端のy-座標 + 1*/
};

# endif/*FDOWN*/

typedef struct {
	void (*init) (void);
	void (*init2) (int, int);
	void (*end) (void);
	void (*set_font) (FONT_INFO *, KFONT *, int, int, int);
	void (*move_cap) (PIXEL, PIXEL);
	void (*move_homepoint) (PIXEL, PIXEL);
	void (*pre_put_char) (FONT_INFO *, PREAMBLE*, uint *, PIXEL *, PIXEL *);
	void (*put_jis_font) (uint);
	void (*put_dl_font) (uint);
	long (*get_dlhsize) (int num);
	void (*pre_download) (FONT_INFO *, DL_INFO *, int, int);
	void (*download_rastor) (FONT_INFO *, DL_INFO *, uint, int);
	void (*post_download) (FONT_INFO *, DL_INFO *);
	void (*drawbox) (PIXEL, PIXEL, PIXEL, PIXEL, int);
	void (*rotate) (int);
	void (*tate_yoko) (int);
	struct LIPS_LFONT *data;
} LBP_FUNC;

struct LIPS_LFONT {
	char *font_name;
	int no;
};

#endif /*LBP*/

/* extern variables */

extern BUF_INFO buffers[];

extern	FONT_INFO *first_font_info;

 /*  the first position of the font-list.
     */

#define	pk_buf_pointer	(&buffers[1])

 /*  the buffer for pk-font-data loaded from disk directly.
     */

#define	raster_buf_pointer	(&buffers[0])

 /*  the buffer for the raster-data of unpacked fonts.
     */

#define	bitmap_buf_pointer	(&buffers[2])

 /*  Virtual VRAM buffer. Interpret-module will draw the figure onto this.
     * This is casted to HUGE_BUF when used.
     */

#define	work_buf_pointer	(&buffers[3])

#define	work_buf_size	(work_buf_pointer->size)
#define	work_buf_addr	(work_buf_pointer->start)

 /*  Buffer for working in far heap: default size  = 0 
     */

extern int max_user_page;
extern uchar far *pf_addr;

 /* Buffer for working in EMS
  */

#define	COMMON_SIZE	2048
extern uchar common_work[COMMON_SIZE];
#define	tmp_buf	common_work

 /*  Buffer for working in near heap
     COMMON_SIZE should not be smaller than 2048
     */

extern BUFFER *get_work(uint);
extern uchar *get_near_work(uint);

#define	F_GOTH		1
#define	K_DBL		2
#define	F_LBP		4
#define	F_DWN		8
#define	F_FILL		16
#define	F_SHADOW	32
#define	F_OUTLINE	64
#define	F_GGOTH		128
#define	K_SCALE		256
#define	F_TATE		512
#define K_SCALE2   1024

#define	F_ATTR	(F_GOTH|F_FILL|F_SHADOW|F_OUTLINE|F_GGOTH)

/* Definition for DVI commands */

#define SET_CHAR_0      0
#define SET1            128
#define SET_RULE        132
#define PUT1            133
#define PUT_RULE        137
#define NOP             138
#define BOP             139
#define EOP             140
#define PUSH            141
#define POP             142
#define RIGHT1          143
#define W0              147
#define W1              148
#define X0              152
#define X1              153
#define DOWN1           157
#define Y0              161
#define Y1              162
#define Z0              166
#define Z1              167
#define FNT_NUM_0       171
#define FNT1            235
#define XXX1            239
#define FNT_DEF_1       243
#define PRE             247
#define POST            248
#define POST_POST       249
#define EOFNC           255	/* end of func */

/* Units for length : mm, inch(in), point(pt), scaled point(sp) and macros
 * for them.
 * by K.Yoshizawa Dec. 26, 1992
 */
#define ROUND(X) ((X)+0.5) /* 浮動小数点数→整数時の四捨五入 */

#define MM_PER_IN 25.4	/* 1in = 25.4mm */
#define PT_PER_IN 72.27	/* 1in = 72.27pt */

#define IN_PER_MM (1.0/MM_PER_IN)
#define PT_PER_MM (PT_PER_IN/MM_PER_IN)

#define MM_PER_PT (MM_PER_IN/PT_PER_IN)
#define IN_PER_PT (1.0/PT_PER_IN)

#define MM_TO_SP(X) PT_TO_SP(MM_TO_PT(X))
#define IN_TO_SP(X) PT_TO_SP(IN_TO_PT(X))
#define PT_TO_SP(X) ((SCALED_PT)ROUND((double)(X)*65536.0))

#define IN_TO_MM(X) ((X)*MM_PER_IN)
#define PT_TO_MM(X) ((X)*MM_PER_PT)
#define SP_TO_MM(X) PT_TO_MM(SP_TO_PT(X))

#define MM_TO_IN(X) ((X)*IN_PER_MM)
#define PT_TO_IN(X) ((X)*IN_PER_PT)
#define SP_TO_IN(X) PT_TO_IN(SP_TO_PT(X))

#define MM_TO_PT(X) ((X)*PT_PER_MM)
#define IN_TO_PT(X) ((X)*PT_PER_IN)
#define SP_TO_PT(X) ((double)(X)/65536.0)

/* end of file : dd.h */
