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

static	void
TEXT_SetPaper(
	PrinterControl	*p)
{
	/*	NOP	*/
}

static	void
TEXT_Init(void)
{
	/*	NOP	*/
}

static	void
TEXT_NewPage(
	Bool	fLast)
{
	/*	NOP	*/
}

static	void
TEXT_Select(
	word	set)
{
	/*	NOP	*/
}

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

static	void
CR_NewLine(void)
{
	PrintString("\n");
}

static	void
TEXT_PrintSJIS(
	Char	ch)
{
	char	sjis[2];

	CharToSJIS(ch,sjis);
	PrintChar(sjis[0]);
	PrintChar(sjis[1]);
}

static	void
TEXT_PrintEUC(
	Char	ch)
{
	char	euc[2];

	CharToEUC(ch,euc);
	PrintChar(euc[0]);
	PrintChar(euc[1]);
}

static	void
TEXT_Select_JIS83(
	word	set)
{
	if		(  set  ==  SET_ASCII  ) {
		PrintString("\x01B(J");
	} else
	if		(  set  ==  SET_JIS83  ) {
		PrintString("\x01B\x024\x042");
	} else {
		PrintString("");
	}
}

/*	force conver JIS78 escape	*/
static	void
TEXT_Select_JIS78(
	word	set)
{
	if		(  set  ==  SET_ASCII  ) {
		PrintString("\x01B(J");
	} else
	if		(  set  ==  SET_JIS83  ) {
		PrintString("\x01B\x024\x040");
	} else {
		PrintString("");
	}
}

PrinterControl	JIS83 = {
	"JIS",
	TEXT_Select_JIS83,
	TEXT_NewPage,
	TEXT_Init,
	CR_NewLine,
	PrintSBCS_Direct,
	PrintDBCS_Direct,
	TEXT_SetPaper,
};

PrinterControl	JIS83_CRLF = {
	"JIS-CRLF",
	TEXT_Select_JIS83,
	TEXT_NewPage,
	TEXT_Init,
	CRLF_NewLine,
	PrintSBCS_Direct,
	PrintDBCS_Direct,
	TEXT_SetPaper,
};

PrinterControl	JIS78 = {
	"JIS78",
	TEXT_Select_JIS78,
	TEXT_NewPage,
	TEXT_Init,
	CR_NewLine,
	PrintSBCS_Direct,
	PrintDBCS_Direct,
	TEXT_SetPaper,
};

PrinterControl	JIS78_CRLF = {
	"JIS78-CRLF",
	TEXT_Select_JIS78,
	TEXT_NewPage,
	TEXT_Init,
	CRLF_NewLine,
	PrintSBCS_Direct,
	PrintDBCS_Direct,
	TEXT_SetPaper,
};

PrinterControl	SJIS = {
	"SJIS",
	TEXT_Select,
	TEXT_NewPage,
	TEXT_Init,
	CR_NewLine,
	PrintSBCS_Direct,
	TEXT_PrintSJIS,
	TEXT_SetPaper,
};

PrinterControl	SJIS_CRLF = {
	"SJIS-CRLF",
	TEXT_Select,
	TEXT_NewPage,
	TEXT_Init,
	CRLF_NewLine,
	PrintSBCS_Direct,
	TEXT_PrintSJIS,
	TEXT_SetPaper,
};

PrinterControl	EUC = {
	"EUC",
	TEXT_Select,
	TEXT_NewPage,
	TEXT_Init,
	CR_NewLine,
	PrintSBCS_Direct,
	TEXT_PrintEUC,
	TEXT_SetPaper,
};

PrinterControl	EUC_CRLF = {
	"EUC-CRLF",
	TEXT_Select,
	TEXT_NewPage,
	TEXT_Init,
	CRLF_NewLine,
	PrintSBCS_Direct,
	TEXT_PrintEUC,
	TEXT_SetPaper,
};

static	void
Help(void)
{
	fprintf(stderr,"\tEUC\n");
	fprintf(stderr,"\tJIS\n");
	fprintf(stderr,"\tSJIS\n");
	fprintf(stderr,"\tJIS78\n");
	fprintf(stderr,"\tif you want EOL code is CRLF,\n");
	fprintf(stderr,"\tyou should add -CRLF\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,"EUC")  ) {
		ret = &EUC;
	} else
	if		(  !strcmp(parameter,"EUC-CRLF")  ) {
		ret = &EUC_CRLF;
	} else
	if		(  !strcmp(parameter,"JIS")  ) {
		ret = &JIS83;
	} else
	if		(  !strcmp(parameter,"JIS-CRLF")  ) {
		ret = &JIS83_CRLF;
	} else
	if		(  !strcmp(parameter,"SJIS")  ) {
		ret = &SJIS;
	} else
	if		(  !strcmp(parameter,"SJIS-CRLF")  ) {
		ret = &SJIS_CRLF;
	} else
	if		(  !strcmp(parameter,"JIS78")  ) {
		ret = &JIS78;
	} else
	if		(  !strcmp(parameter,"JIS78-CRLF")  ) {
		ret = &JIS78_CRLF;
	} else {
		ret = NULL;
	}
	if		(  ret  !=  NULL  ) {
		ret->LinePitch = 0;
		ret->PitchSBCS = 0;
		ret->PitchDBCS = 0;
		ret->PageLength = 0;
		ret->PageWidth = 0;
		ret->PageLines = 0;
		ret->LineLength = 0;
		ret->LineColumns = 0;
		ret->TopMargineInLines = 0;
		ret->LeftMargine = 0;
		ret->TabWidth = TabWidth;
		ret->fPrintPageHeader = FALSE;
	}
	return	(ret);
}

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

