/*	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	<string.h>
#include	<time.h>
#include	<ctype.h>
#include	<stdlib.h>
#include	"types.h"
#include	"pf.h"

static	char	sbuf[80];
static	int		sColumn
		,		cColumn;
static	int		CurrentSet;

extern	void
InitPrinter(
	PrinterControl	*p)
{
	CurrentSet = SET_NULL;
	cPage = 0;
	cLine = 1000;	/*	>= lines in page	*/
	sColumn = 0;
	fNewLine = TRUE;
	p->Init();
	p->SetPaper(p);
}

static	void
ChangeCode(
	PrinterControl	*p,
	word	set)
{
	if		(  set  !=  CurrentSet  )	{
		p->SelectCodeset(set);
		CurrentSet = set;
	}
}

extern	void
DoPrint(
	PrinterControl	*p,
	Char	c)
{

	ChangeCode(p,SET_Char(c));
	if		(  IsDBCS(c)  ) {
		p->PrintDBCS(c);
		sColumn += p->PitchDBCS;
		cColumn += 2;
	} else {
		p->PrintSBCS(c);
		sColumn += p->PitchSBCS;
		cColumn += 1;
	}
	fNewLine = FALSE;
}

static	void
PrintSBCS_String(
	PrinterControl	*p,
	char	*s)
{
	while	(  *s  !=  0  )	{
		DoPrint(p,ToAscii(*s));
		s ++;
	}
}

static	void
PrintSpace(
	PrinterControl	*p,
	int		n)
{
	for	( ; n  >  0 ; n -- )	{
		DoPrint(p,Char_SPACE);
	}
}

extern	void
NewLine(
	PrinterControl	*p)
{
	ChangeCode(p,SET_NULL);
	p->NewLine();
	cLine ++;
	sColumn = 0;
	cColumn = 0;
	fNewLine = TRUE;
}

static	void
SkipLineHead(
	PrinterControl	*p)
{
	if		(  p->LeftMargine  >  0  ) {
		PrintSpace(p,p->LeftMargine);
	}
}

static	void
PrintHeader(
	PrinterControl	*p,
	int				page)
{
	time_t	nowtime;
	tTime	*Now;
	int		i
	,		d
	,		col;

	time(&nowtime);
	Now = localtime(&nowtime);
	if		(  Now->tm_year  <  90  )	{
		Now->tm_year += 2000;
	} else {
		Now->tm_year += 1900;
	}

	SkipLineHead(p);
	sprintf(sbuf,"FILE:%s",fnInput);
	col = strlen(sbuf);
	PrintSBCS_String(p,sbuf);
	sprintf(sbuf,"TIME:%02d:%02d:%02d   DATE:%04d/%02d/%02d   PAGE:%4d"
			, Now->tm_hour,Now->tm_min,Now->tm_sec
			, Now->tm_year,Now->tm_mon,Now->tm_mday
			, page);
	col += strlen(sbuf);
	if		(  p->LineColumns  ==  0  )	{
		PrintSpace(p,2);
	} else {
		PrintSpace(p,( p->LineColumns - col ));
	}
	PrintSBCS_String(p,sbuf);
	NewLine(p);

	SkipLineHead(p);
	for	( i = 1 ; i <= p->LineColumns ; i ++ )	{
		if		(  ( i % 10 )  ==  0  )	{
			d = ( i % 100 ) / 10 ;
			DoPrint(p,ToAscii('0' + d));
		} else
		if		(  ( i % 5 )  ==  0  )	{
			DoPrint(p,ToAscii('+'));
		} else {
			DoPrint(p,ToAscii('-'));
		}
	}
	NewLine(p);
}

extern	void
NewPage(
	PrinterControl	*p,
	Bool	fLast)
{	int		i;

	cPage ++;
	ChangeCode(p,SET_NULL);
	p->NewPage(fLast);
	if		(  !fLast  )	{
		for	( i = 0 ; i < p->TopMargineInLines ; i ++ )	{
			NewLine(p);
		}
		if		(  p->fPrintPageHeader  )	{
			PrintHeader(p,cPage);
		}
		cLine = 0;
		sColumn = 0;
		cColumn = 0;
	}
	fNewLine = TRUE;
}

extern	void
CheckLayout(
	PrinterControl	*p)
{
	if		(	(  p->LineColumns  !=  0               )
			 &&	(  cColumn         >=  p->LineColumns  ) )	{
		NewLine(p);
	}
	if		(	(  p->PageLines  !=  0             )
			 &&	(  cLine         >=  p->PageLines  ) )	{
		NewPage(p,FALSE);
	}
	if		(  fNewLine  )	{
		SkipLineHead(p);
	}
}

extern	void
DoTab(
	PrinterControl	*p)
{	int		n;

	if		(  p->TabWidth  !=  0  )	{
		n = p->TabWidth - ( cColumn % p->TabWidth );
		PrintSpace(p,n);
	} else {
		p->PrintSBCS(CHAR_TAB);
		cColumn ++;
		sColumn += p->PitchSBCS;
	}
}


