#include "tdef.h"
extern
#include "d.h"
extern
#include "v.h"
/*
troff10.c

Output interface
*/

extern int *olinep;
extern int oline[];
extern int *pslp;
extern int back;
extern int xpts;
extern int mpts;
extern int po;
extern int xflg;
extern int line[];
extern int lss;
extern int xbitf;
extern char obuf[];
extern char *obufp;
extern int esct;
extern int trflg;
extern int cs;
extern int smnt;
extern int mfont;
extern int xfont;
extern int code;
extern int mcase;
extern int esc;
extern int lead;
extern int paper;
extern int cps;
extern int psflg;
extern int ptid;
extern int verm;
extern int escm;
extern char pstab[], psctab[];
extern int dpn;
extern int ascii;
extern int nofeed;
extern int gflag;
extern int fontlab[];
int papflg;
extern int pfont;
extern int ppts;
extern int oldbits;
extern int bd;
extern int vflag;
extern int stopmesg;
extern int xxx;
static int fontchange	=	0;

ptinit(){
	if(ascii || gflag)return;
	oput(4);	/* INIT code */
	esct = 0;
	esc = po;
}
ptout(i)
int i;
{
	register *k, lw, *j;
	int ds, de, inith, temp, *slp, dv;
	int psl[16];

	if((i & CMASK) != '\n'){
		*olinep++ = i;
		return;
	}
	if(olinep == oline){
		lead += lss;
		return;
	}
	pslp = psl;
	*pslp = lw = inith = dv = 0;
	for(k=oline; k<olinep; k++){
		trflg++;
		xbitf = 1;
		lw += width(*k);
		if((*k & (MOT | VMOT)) == (MOT | VMOT)){
			temp = *k & ~MOTV;
			if(*k & NMOT)temp = -temp;
			dv += temp;
		}
		if(!(*k & MOT) && xflg)for(j=psl; j<=pslp; j++){
			if(xpts == *j)break;
			if(j == pslp){
				*j = xpts;
				*++pslp = 0;
				break;
			}
		}
	}
	if(dv){
		vflag++;
		*olinep++ = makem(-dv);
		vflag = 0;
	}
	if(xflg){
	--pslp;
		for(j=psl; j<=pslp; j++){
			if(*j == mpts){
				temp = *j;
				*j = *pslp;
				*pslp = temp;
				break;
			}
		}
	}
	for(k=oline; k<olinep; k++){
		if(!(*k & MOT) || (*k & VMOT))break;
		*k &= ~MOT;
		if(*k & NMOT){
			*k &= ~NMOT;
			*k = -*k;
		}
		inith += *k;
	}
	lead += dip->blss + lss;
	dip->blss = 0;
	slp = k;
scan:
	temp = esct - po;
	ds = temp - inith;
	de = lw - temp;
	if(de >= ds){
		back = 0;
		esc = -ds;
		for(k=slp; k<olinep; k++)ptout0(*k);
	}else{
		back = 1;
		esc = de;
		for(k = olinep-1; k>=slp; --k)ptout0(*k);
	}
	if(xflg && (--pslp >= psl))goto scan;
	olinep = oline;
	lead += dip->alss;
	dip->alss = 0;
}
ptout0(i)
int i;
{
	register j, k, w;
	int z;

	if(i & MOT){
		j = i & ~MOTV;
		if(i & NMOT)j = -j;
		if(back)j = -j;
		if(i & VMOT)lead += j;
		else esc += j;
		return;
	}
	xbitf = 2;
	if((i>>BYTE) == oldbits){
		xfont = pfont;
		xpts = ppts;
		xbitf = 0;
	}else xbits(i);
	if((k = (i & CMASK)) < 040){
		return;
	}
	w = getcw(k-32);
	code = k;

	if(cs){
		if(bd)w += bd - 1;
		j = (cs-w)/2;
		w = cs - j;
		if(bd)w -= bd - 1;
	}else j = 0;
	if(i & ZBIT){
		if(cs)w = -j; else w = 0;
		z = 1;
	}else z = 0;
	if(back){
		k = j;
		j = -w;
		w = -k;
	}
	esc += j;
	if((!xflg || (xpts == *pslp)) && code){
		if (xfont == FT  &&  !fontchange)  {
			fontchange = 1;
			mfont = -1;
			}
		if(xfont != mfont){
			mfont = xfont;
			oput(5);	/* Change fonts */
			oput(fontlab[mfont] & BMASK);
			oput((fontlab[mfont] >> BYTE) & BMASK);
		}
		if(xpts != mpts)ptps();
		if(lead)ptlead();
		if(esc)ptesc();
		oput(code);
		if(bd){
			bd -= 1;
			if(back && !z)bd = -bd;
			if(esc += bd)ptesc();
			oput(code);
			if(z)esc -= bd;
		}
	}else if(bd && !z){
		bd -= 1;
		if(back)bd = -bd;
		esc += bd;
	}
p1:
	esc += w;
	return;
}
ptps(){
	register i, j, k;

	if(psflg)return;
	if(cps){
		psflg++;
		i = findps(cps);
	}else i = xpts;
	oput(3);	/* Point size code */
	oput(i);	/* arg to ps */
	mpts = i;
}
ptlead(){
	register i, k;

	oput(2);		/* Leading code */
	for (k=3; k>= 0; k--)
		oput((lead>>(k*8)) & 0xff);
	paper += lead;
	lead = 0;
}
ptesc(){
	register i, j, k;

	oput(1);	/* Escape code */
	for (k=3; k>=0; k--)
		oput((esc>>(k*8)) & 0xff);
	esct += esc;
	esc = 0;
}
dostop(){
	register i;

	if(ascii)return;
	flusho();
	if(stopmesg)prstr("Pages finished.\n");
	report();
	paper = 0;
	esct = 0;
	esc = po;
}
