/*	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	"ESCP.h"

#define	KANJI_SIZE		24
#define	ANK_HALF		11
#define	ANK_12CPI		15
#define	ANK_10CPI		18
#define	ANK_15CPI		12

static	char	aAnk[] = {
	0x21,0x21		/*		*/
,	0x21,0x2a		/*	!	*/
,	0x21,0x2b		/*	"	*/
,	0x22,0x74		/*	#	*/
,	0x21,0x70		/*	$	*/
,	0x21,0x73		/*	%	*/
,	0x21,0x75		/*	&	*/
,	0x21,0x47		/*	'	*/
,	0x21,0x4a		/*	(	*/
,	0x21,0x4b		/*	)	*/
,	0x21,0x76		/*	*	*/
,	0x21,0x5c		/*	+	*/
,	0x21,0x24		/*	,	*/
,	0x21,0x5d		/*	-	*/
,	0x21,0x25		/*	.	*/
,	0x21,0x3f		/*	/	*/
,	0x23,0x30		/*	0	*/
,	0x23,0x31		/*	1	*/
,	0x23,0x32		/*	2	*/
,	0x23,0x33		/*	3	*/
,	0x23,0x34		/*	4	*/
,	0x23,0x35		/*	5	*/
,	0x23,0x36		/*	6	*/
,	0x23,0x37		/*	7	*/
,	0x23,0x38		/*	8	*/
,	0x23,0x39		/*	9	*/
,	0x21,0x27		/*	:	*/
,	0x21,0x28		/*	;	*/
,	0x21,0x63		/*	<	*/
,	0x21,0x61		/*	=	*/
,	0x21,0x64		/*	>	*/
,	0x21,0x29		/*	?	*/
,	0x21,0x77		/*	@	*/
,	0x23,0x41		/*	a	*/
,	0x23,0x42		/*	b	*/
,	0x23,0x43		/*	c	*/
,	0x23,0x44		/*	d	*/
,	0x23,0x45		/*	e	*/
,	0x23,0x46		/*	f	*/
,	0x23,0x47		/*	g	*/
,	0x23,0x48		/*	h	*/
,	0x23,0x49		/*	i	*/
,	0x23,0x4a		/*	j	*/
,	0x23,0x4b		/*	k	*/
,	0x23,0x4c		/*	l	*/
,	0x23,0x4d		/*	m	*/
,	0x23,0x4e		/*	n	*/
,	0x23,0x4f		/*	o	*/
,	0x23,0x50		/*	p	*/
,	0x23,0x51		/*	q	*/
,	0x23,0x52		/*	r	*/
,	0x23,0x53		/*	s	*/
,	0x23,0x54		/*	t	*/
,	0x23,0x55		/*	u	*/
,	0x23,0x56		/*	v	*/
,	0x23,0x57		/*	w	*/
,	0x23,0x58		/*	x	*/
,	0x23,0x59		/*	y	*/
,	0x23,0x5a		/*	z	*/
,	0x21,0x4e		/*	[	*/
#ifdef	yen
,	0x21,0x6f		/*	\	*/
#else
,	0x21,0x40		/*	\	*/
#endif
,	0x21,0x4f		/*	]	*/
,	0x21,0x30		/*	^	*/
,	0x21,0x32		/*	_	*/
,	0x21,0x46		/*	`	*/
,	0x23,0x61		/*	a	*/
,	0x23,0x62		/*	b	*/
,	0x23,0x63		/*	c	*/
,	0x23,0x64		/*	d	*/
,	0x23,0x65		/*	e	*/
,	0x23,0x66		/*	f	*/
,	0x23,0x67		/*	g	*/
,	0x23,0x68		/*	h	*/
,	0x23,0x69		/*	i	*/
,	0x23,0x6a		/*	j	*/
,	0x23,0x6b		/*	k	*/
,	0x23,0x6c		/*	l	*/
,	0x23,0x6d		/*	m	*/
,	0x23,0x6e		/*	n	*/
,	0x23,0x6f		/*	o	*/
,	0x23,0x70		/*	p	*/
,	0x23,0x71		/*	q	*/
,	0x23,0x72		/*	r	*/
,	0x23,0x73		/*	s	*/
,	0x23,0x74		/*	t	*/
,	0x23,0x75		/*	u	*/
,	0x23,0x76		/*	v	*/
,	0x23,0x77		/*	w	*/
,	0x23,0x78		/*	x	*/
,	0x23,0x79		/*	y	*/
,	0x23,0x7a		/*	z	*/
,	0x21,0x50		/*	{	*/
,	0x21,0x43		/*	|	*/
,	0x21,0x51		/*	}	*/
,	0x21,0x31		/*	~	*/
,	0x21,0x21		/*	DEL	*/

,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/

,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/
,	0x21,0x21		/*		*/

,	0x21,0x21		/*	SPC	*/
,	0x21,0x23		/*	。	*/
,	0x21,0x56		/*	「	*/
,	0x21,0x57		/*	」	*/
,	0x21,0x22		/*	、	*/
,	0x21,0x26		/*	・	*/
,	0x25,0x72		/*	ヲ	*/
,	0x25,0x21		/*	ァ	*/
,	0x25,0x23		/*	ィ	*/
,	0x25,0x25		/*	ゥ	*/
,	0x25,0x27		/*	ェ	*/
,	0x25,0x29		/*	ォ	*/
,	0x25,0x63		/*	ャ	*/
,	0x25,0x65		/*	ュ	*/
,	0x25,0x67		/*	ョ	*/
,	0x25,0x43		/*	ッ	*/

,	0x21,0x3C		/*	ー	*/
,	0x25,0x22		/*	ア	*/
,	0x25,0x24		/*	イ	*/
,	0x25,0x26		/*	ウ	*/
,	0x25,0x28		/*	エ	*/
,	0x25,0x2A		/*	オ	*/
,	0x25,0x2B		/*	カ	*/
,	0x25,0x2D		/*	キ	*/
,	0x25,0x2F		/*	ク	*/
,	0x25,0x31		/*	ケ	*/
,	0x25,0x33		/*	コ	*/
,	0x25,0x35		/*	サ	*/
,	0x25,0x37		/*	シ	*/
,	0x25,0x39		/*	ス	*/
,	0x25,0x3B		/*	セ	*/
,	0x25,0x3D		/*	ソ	*/

,	0x25,0x3F		/*	タ	*/
,	0x25,0x41		/*	チ	*/
,	0x25,0x44		/*	ツ	*/
,	0x25,0x46		/*	テ	*/
,	0x25,0x48		/*	ト	*/
,	0x25,0x4A		/*	ナ	*/
,	0x25,0x4B		/*	ニ	*/
,	0x25,0x4C		/*	ヌ	*/
,	0x25,0x4D		/*	ネ	*/
,	0x25,0x4E		/*	ノ	*/
,	0x25,0x4F		/*	ハ	*/
,	0x25,0x52		/*	ヒ	*/
,	0x25,0x55		/*	フ	*/
,	0x25,0x58		/*	ヘ	*/
,	0x25,0x5B		/*	ホ	*/
,	0x25,0x5E		/*	マ	*/

,	0x25,0x5F		/*	ミ	*/
,	0x25,0x60		/*	ム	*/
,	0x25,0x61		/*	メ	*/
,	0x25,0x62		/*	モ	*/
,	0x25,0x64		/*	ヤ	*/
,	0x25,0x66		/*	ユ	*/
,	0x25,0x68		/*	ヨ	*/
,	0x25,0x69		/*	ラ	*/
,	0x25,0x6A		/*	リ	*/
,	0x25,0x6B		/*	ル	*/
,	0x25,0x6C		/*	レ	*/
,	0x25,0x6D		/*	ロ	*/
,	0x25,0x6F		/*	ワ	*/
,	0x25,0x73		/*	ン	*/
,	0x21,0x2B		/*	゛	*/
,	0x21,0x2C		/*	゜	*/
};

static	Bool	fNewPaper;

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

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

static	void
PutESCP(
	char	*pre,
	int		par)
{	int		high;

	PrintString(pre);
	PrintChar(LOWBYTE(par));
	if		(  ( high = HIGHBYTE(par) )  !=  0  )	{
		PrintChar(high);
	}
}

static	void
ESCP_Init(void)
{
	PrintString("\x01B\x040");
	fNewPaper = TRUE;
}

static	void
ESCP_SetHalfPaper(
	PrinterControl	*p)
{
	PutESCP("\x01C\x054",0);
	PrintChar(LOWBYTE(p->PitchSBCS - ANK_HALF));
	PutESCP("\x01C\x053",0);
	PrintChar(LOWBYTE(p->PitchDBCS - KANJI_SIZE));
	PutESCP("\x01B\x033",p->LinePitch);
	PutESCP("\x01B\x043",(p->PageLength/p->LinePitch));
}

static	void
ESCP_Set10Paper(
	PrinterControl	*p)
{
	PutESCP("\x01B\x020",(p->PitchSBCS - ANK_10CPI));
	PutESCP("\x01C\x053",0);
	PrintChar(LOWBYTE(p->PitchDBCS - KANJI_SIZE));
	PutESCP("\x01B\x033",p->LinePitch);
	PutESCP("\x01B\x043",(p->PageLength/p->LinePitch));
}

static	void
ESCP_Set12Paper(
	PrinterControl	*p)
{
	PutESCP("\x01B\x020",(p->PitchSBCS - ANK_12CPI));
	PutESCP("\x01C\x053",0);
	PrintChar(LOWBYTE(p->PitchDBCS - KANJI_SIZE));
	PutESCP("\x01B\x033",p->LinePitch);
	PutESCP("\x01B\x043",(p->PageLength/p->LinePitch));
}

static	void
ESCP_PrintSBCS_KanjiHalf(
	Char	ch)
{	word	code;

	code = CODE_Char(ch);
	PrintChar(aAnk[(code - 0x20) * 2 + 0]);
	PrintChar(aAnk[(code - 0x20) * 2 + 1]);
}

static	void
ESCP_Select_R(
	word	set)
{
	if		(  set  ==  SET_ASCII  ) {
		PrintString("\x01C\x02E\x01B\x074\x003\x01B\x050");
	} else
	if		(  set  ==  SET_JIS83  ) {
		PrintString("\x01C\x026");
	} else {
		PrintString("");
	}
}

static	void
ESCP_Select_H(
	word	set)
{
	if		(  set  ==  SET_ASCII  ) {
		PrintString("\x01C\x02E\x01C\x00F\x01C\x026");
	} else
	if		(  set  ==  SET_JIS83  ) {
		PrintString("\x01C\x012\x01C\x026");
	} else {
		PrintString("");
	}
}

static	void
ESCP_Select_D(
	word	set)
{
	if		(  set  ==  SET_ASCII  ) {
		PrintString("\x01C\x02E\x01B\x074\x003\x01B\x04D");
	} else
	if		(  set  ==  SET_JIS83  ) {
		PrintString("\x01C\x026");
	} else {
		PrintString("");
	}
}

static	PrinterControl	ESCP_R = {
	"ESCP-R",
	ESCP_Select_R,
	ESCP_NewPage,
	ESCP_Init,
	ESCP_NewLine,
	PrintSBCS_Direct,
	PrintDBCS_Direct,
	ESCP_Set10Paper,
};

static	PrinterControl	ESCP_H = {
	"ESCP-H",
	ESCP_Select_H,
	ESCP_NewPage,
	ESCP_Init,
	ESCP_NewLine,
	ESCP_PrintSBCS_KanjiHalf,
	PrintDBCS_Direct,
	ESCP_SetHalfPaper,
};

static	PrinterControl	ESCP_D = {
	"ESCP-D",
	ESCP_Select_D,
	ESCP_NewPage,
	ESCP_Init,
	ESCP_NewLine,
	PrintSBCS_Direct,
	PrintDBCS_Direct,
	ESCP_Set12Paper,
};

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 = &ESCP_R;
	} else
	if		(  !strcmp(parameter,"H")  ) {
		ret = &ESCP_H;
	} else
	if		(  !strcmp(parameter,"D")  ) {
		ret = &ESCP_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	ESCP = {
	"ESCP",
	Help,
	Select
};

