/*	PF -- printer filter

Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 Ogochan .

This module is part of PF.

	PF is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves
any particular purpose or works at all, unless he says so in writing.
Refer to the GNU General Public License for full details. 

	Everyone is granted permission to copy, modify and redistribute
MACE, but only under the conditions described in the GNU General
Public License.  A copy of this license is supposed to have been given
to you along with PF so you can know your rights and
responsibilities.  It should be in a file named COPYING.  Among other
things, the copyright notice and this notice must be preserved on all
copies. 
*/
#include	<stdio.h>
#include	"types.h"
#include	"pf.h"

#include	"PCPR.h"

static	Bool	fNewPaper;

static	void
PCPR_NewPage(
	Bool	fLast)
{
	if		(  !fNewPaper  )	{
		PrintString("\x00C");
	}
	fNewPaper = FALSE;
}

static	void
PCPR_NewLine(void)
{
	PrintString("\r\n");
}

static	void
PCPR_Init(void)
{
	PrintString("\x01B\x063\x031");
	fNewPaper = TRUE;
}

static	void
PCPR_PrintSBCS_HalfCode(
	Char	ch)
{	word	code;

	code = CODE_Char(ch);
	PrintChar(0);
	PrintChar(CODE_LOWER(code));
}

static	void
PCPR_SetPaper(
	PrinterControl	*p)
{
	int		sDot
	,		i
	,		lp;
	char	sbuf[10];

	sDot = ( (p->PitchDBCS - 24) * 160L ) / 180;
	PrintChar('\x01B');
	PrintChar((byte)sDot);
	sDot = ( p->LinePitch * 120 ) / 180;
	sprintf(sbuf,"\x01B\x054%2d",sDot);
	PrintString(sbuf);
	PrintChar('\x01D');
	PrintChar('\x041');
	PrintChar('\x000');
	lp = p->PageLength / p->LinePitch;
	for	( i = 0 ; i < lp ; i ++ )	{
		PrintChar('\x040');
		PrintChar('\x000');
	}
	PrintChar('\x041');
	PrintChar('\x000');
	PrintChar('\x01E');
}

static	void
PCPR_Select_R(
	word	set)
{
	if		(  set  ==  SET_ASCII  ) {
		PrintString("\x01B\x045");
	} else
	if		(  set  ==  SET_JIS83  ) {
		PrintString("\x01B\x04B");
	} else {
		PrintString("");
	}
}

static	void
PCPR_Select_H(
	word	set)
{
	if		(  set  ==  SET_ASCII  ) {
		PrintString("\x01B\x04B");
	} else
	if		(  set  ==  SET_JIS83  ) {
		PrintString("\x01B\x04B");
	} else {
		PrintString("");
	}
}

static	void
PCPR_Select_D(
	word	set)
{
	if		(  set  ==  SET_ASCII  ) {
		PrintString("\x01B\x04E");
	} else
	if		(  set  ==  SET_JIS83  ) {
		PrintString("\x01B\x04B");
	} else {
		PrintString("");
	}
}

PrinterControl	PCPR_R = {
	"PCPR-R",
	PCPR_Select_R,
	PCPR_NewPage,
	PCPR_Init,
	PCPR_NewLine,
	PrintSBCS_Direct,
	PrintDBCS_Direct,
	PCPR_SetPaper,
};

PrinterControl	PCPR_H = {
	"PCPR-H",
	PCPR_Select_H,
	PCPR_NewPage,
	PCPR_Init,
	PCPR_NewLine,
	PCPR_PrintSBCS_HalfCode,
	PrintDBCS_Direct,
	PCPR_SetPaper,
};

PrinterControl	PCPR_D = {
	"PCPR-D",
	PCPR_Select_D,
	PCPR_NewPage,
	PCPR_Init,
	PCPR_NewLine,
	PrintSBCS_Direct,
	PrintDBCS_Direct,
	PCPR_SetPaper,
};


static	void
Help(void)
{
	fprintf(stderr,"\tR\tpretty ANK font\n");
	fprintf(stderr,"\tH\tNIHONGO like ANK font\n");
	fprintf(stderr,"\tD\tdraft ANK font\n");
}

static	PrinterControl	*
Select(char *parameter,
	int		LinePitch,
	int		PitchSBCS,
	int		PitchDBCS,
	int		PageLength,
	int		PageWidth,
	int		PageLines,
	int		LineLength,
	int		TopMargineInLines,
	int		LeftMargine,
	int		TabWidth,
	Bool	fPrintPageHeader)
{
	PrinterControl	*ret;

	if		(  !strcmp(parameter,"R")  ) {
		ret = &PCPR_R;
	} else
	if		(  !strcmp(parameter,"H")  ) {
		ret = &PCPR_H;
	} else
	if		(  !strcmp(parameter,"D")  ) {
		ret = &PCPR_D;
	} else {
		ret = NULL;
	}
	if		(  ret  !=  NULL  ) {
		ret->LinePitch = LinePitch;
		ret->PitchSBCS = PitchSBCS;
		ret->PitchDBCS = PitchDBCS;
		ret->PageLength = PageLength;
		ret->PageWidth = PageWidth;
		ret->PageLines = PageLines;
		ret->LineLength = LineLength;
		ret->LineColumns = LineLength / PitchSBCS;
		ret->TopMargineInLines = TopMargineInLines;
		ret->LeftMargine = LeftMargine;
		ret->TabWidth = TabWidth;
		ret->fPrintPageHeader = fPrintPageHeader;
	}
	return	(ret);
}

PrinterClass	PCPR = {
	"PCPR",
	Help,
	Select
};

