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

#include	"PS.h"

static	Bool	fNewPrint;
static	Bool	fKanjiInit;
static	word	SetOld;

static	char	sbuf[100];

static	char	*PaperTray
		,		*Font;
static	Bool	fLandscape
		,		fBorder;
static	double	FontSize
		,		TopMargine
		,		LeftMargine;
static	int		cSheet;

static	char	*PrintTemplate =
"%!  PostScript Source Code\n"
"%\n"
"%  File: imag:/users/local/a2ps/header.ps\n"
"%  Created: Tue Nov 29 12:14:02 1988 by miguel@imag (Miguel Santana)\n"
"%  Version: 2.0\n"
"%  Description: PostScript prolog for a2ps ascii to PostScript program.\n"
"% \n"
"%  Edit History:\n"
"%  - Original version by evan@csli (Evan Kirshenbaum).\n"
"%  - Modified by miguel@imag to:\n"
"%    1) Correct an overflow bug when printing page number 10 (operator\n"
"%	cvs).\n"
"%    2) Define two other variables (sheetwidth, sheetheight) describing\n"
"%	the physical page (by default A4 format).\n"
"%    3) Minor changes (reorganization, comments, etc).\n"
"%  - Modified by tullemans@apolloway.prl.philips.nl\n"
"%    1) Correct stack overflows with regard to operators cvs and copy.\n"
"%       The resulting substrings where in some cases not popped off \n"
"%       the stack, what can result in a stack overflow.\n"
"%    2) Replaced copypage and erasepage by showpage. Page througput\n"
"%       degrades severely (see red book page 140) on our ps-printer\n"
"%       after printing sheet 16 (i.e. page 8) of a file which was \n"
"%       actually bigger. For this purpose the definitions of startdoc\n"
"%       and startpage are changed.\n"
"%  - Modified by Tim Clark <T.Clark@uk.ac.warwick> to:\n"
"%    1) Print one page per sheet (portrait) as an option.\n"
"%    2) Reduce size of file name heading, if it's too big.\n"
"%    3) Save and restore PostScript state at begining/end. It now uses\n"
"%	conventional %%Page %%Trailer markers.\n"
"%    4) Print one wide page per sheet in landscape mode as an option.\n"
"%  - Modified by miguel@imag.fr to\n"
"%    1) Add new option to print n copies of a file.\n"
"%    2) Add new option to suppress heading printing.\n"
"%    3) Add new option to suppress page surrounding border printing.\n"
"%    4) Add new option to change font size. Number of lines and columns\n"
"%	are now automatically adjusted, depending on font size and\n"
"%	printing mode used.\n"
"%    5) Minor changes (best layout, usage message, etc).\n"
"%  - Modified by kanazawa@sra.co.jp to:\n"
"%    1) Handle Japanese code\n"
"%  - Modified by utashiro@sra.co.jp to:\n"
"%    1) Fix bug in printing long label\n"
"%    2) Handle carriage-return\n"
"%    3) Specify kanji-ascii character retio\n"
"%    4) Add footer label\n"
"%    5) Change filename->fname becuase ghostscript has operator filename\n"
"%    6) Support three different font style\n"
"%    7) Incorporate B4 paper support and punchmark contributed\n"
"%       by Masami Ueno <cabbage@kki.esi.yamanashi.ac.jp>\n"
"%\n"
"\n"
"% Copyright (c) 1988, Miguel Santana, miguel@imag.imag.fr\n"
"%\n"
"% Permission is granted to copy and distribute this file in modified\n"
"% or unmodified form, for noncommercial use, provided (a) this copyright\n"
"% notice is preserved, (b) no attempt is made to restrict redistribution\n"
"% of this file, and (c) this file is not distributed as part of any\n"
"% collection whose redistribution is restricted by a compilation copyright.\n"
"%\n"
"\n"
"\n"
"% General macros.\n"
"/xdef {exch def} bind def\n"
"/getfont {exch findfont exch scalefont} bind def\n"
"\n"
"% Page description variables and inch function are defined by a2ps program.\n"
"\n"
"% Character size for differents fonts.\n"
"   landscape\n"
"   { /filenamefontsize 12 def }\n"
"   { /filenamefontsize 16 def }\n"
"ifelse\n"
"/datefontsize filenamefontsize 0.8 mul def\n"
"/headermargin filenamefontsize 0.25 mul def\n"
"/bodymargin bodyfontsize 0.7 mul def\n"
"\n"
"% Font assignment to differents kinds of objects\n"
"/filenamefontname /Helvetica-Bold def\n"
"/stdfilenamefont filenamefontname filenamefontsize getfont def\n"
"/datefont /Helvetica datefontsize getfont def\n"
"/footerfont /Helvetica-Bold datefontsize getfont def\n"
"/mag { doasciimag { [ 1 0 0 asciimagsize 0 0 ] makefont } if } def\n"
"/bodynfont /Courier bodyfontsize getfont mag def\n"
"/bodybfont /Courier-Bold bodyfontsize getfont mag def\n"
"/bodyofont /Courier-BoldOblique bodyfontsize getfont mag def\n"
"/fontarray [ bodynfont bodybfont bodyofont ] def\n"
"/bodyfont bodynfont def\n"
"\n"
"% Initializing kanji fonts\n"
"/kanji_initialized false def\n"
"/kanji_init {\n"
"   kanji_initialized not\n"
"   {\n"
"      /bodykfontsize bodyfontsize kanjiAsciiRatio mul def\n"
"      /bodyknfont /Ryumin-Light-H bodykfontsize getfont def\n"
"      /bodykbfont /GothicBBB-Medium-H bodykfontsize getfont def\n"
"      /bodykofont bodykbfont [ 1 0 .2 1 0 0 ] makefont def\n"
"      /KanjiRomanDiff 1.2 bodyfontsize mul 1.0 bodykfontsize mul sub def\n"
"      /KanjiRomanDiffHalf KanjiRomanDiff 2 div def\n"
"      /kfontarray [ bodyknfont bodykbfont bodykofont ] def\n"
"      /kanji_initialized true def\n"
"   } if\n"
"} def\n"
"\n"
"% Backspace width\n"
"/backspacewidth\n"
"   bodyfont setfont (0) stringwidth pop\n"
"   def\n"
"\n"
"% Logical page attributs (a half of a real page or sheet).\n"
"/pagewidth\n"
"   bodyfont setfont (0) stringwidth pop columns mul bodymargin dup add add\n"
"   def\n"
"/pageheight\n"
"   bodyfontsize 1.1 mul lines mul bodymargin dup add add headersize add\n"
"   def\n"
"\n"
"% Coordinates for upper corner of a logical page and for sheet number.\n"
"% Coordinates depend on format mode used.\n"
"% In twinpage mode, coordinate x of upper corner is not the same for left\n"
"% and right pages: upperx is an array of two elements, indexed by sheetside.\n"
"/rightmargin smargin 3 div def\n"
"/leftmargin smargin 2 mul 3 div def\n"
"/topmargin lmargin twinpage {3} {2} ifelse div def\n"
"landscape\n"
"{  % Landscape format\n"
"   /punchx .4 inch def           % for PunchMark\n"
"   /punchy sheetwidth 2 div def  % for PunchMark\n"
"   /uppery rightmargin pageheight add bodymargin add def\n"
"   /sheetnumbery sheetwidth leftmargin pageheight add datefontsize add sub def\n"
"   twinpage\n"
"   {  % Two logical pages\n"
"      /upperx [ topmargin 2 mul			% upperx for left page\n"
"		dup topmargin add pagewidth add	% upperx for right page\n"
"	      ] def\n"
"      /sheetnumberx sheetheight topmargin 2 mul sub def\n"
"   }\n"
"   {  /upperx [ topmargin dup ] def\n"
"      /sheetnumberx sheetheight topmargin sub datefontsize sub def\n"
"   }\n"
"   ifelse\n"
"}\n"
"{  % Portrait format\n"
"   /punchx .3 inch def\n"
"   /punchy sheetheight 2 div def\n"
"   /uppery topmargin pageheight add def\n"
"   /upperx [ leftmargin dup ] def\n"
"   /sheetnumberx sheetwidth rightmargin sub datefontsize sub def\n"
"   /sheetnumbery\n"
"	 sheetheight \n"
"	 topmargin pageheight add datefontsize add headermargin add\n"
"      sub\n"
"      def\n"
"}\n"
"ifelse\n"
"\n"
"% Strings used to make easy printing numbers\n"
"/pnum 12 string def\n"
"/empty 12 string def\n"
"\n"
"% Other initializations.\n"
"datefont setfont\n"
"/datewidth date stringwidth pop def\n"
"/pagenumwidth (Page 999) stringwidth pop def\n"
"/filenameroom\n"
"         pagewidth\n"
"	 filenamefontsize 4 mul datewidth add pagenumwidth add\n"
"      sub\n"
"   def\n"
"\n"
"\n"
"% Function startdoc: initializes printer and global variables.\n"
"/startdoc\n"
"    { /sheetside 0 def			% sheet side that contains current page\n"
"   } bind def\n"
"\n"
"% Function setlabel: initialize label in header\n"
"/setlabel\n"
"    { stdfilenamefont setfont\n"
"      /fname xdef\n"
"      /filenamewidth fname stringwidth pop def\n"
"      /filenamefont\n"
"	 filenamewidth filenameroom gt\n"
"	 {\n"
"	       filenamefontname\n"
"	       filenamefontsize filenameroom mul filenamewidth div\n"
"	    getfont\n"
"	 }\n"
"	 {  stdfilenamefont }\n"
"	 ifelse\n"
"	 def\n"
"    } bind def\n"
"\n"
"% Function printpage: Print a physical page.\n"
"/printpage\n"
"    { /sheetside 0 def\n"
"      showpage \n"
"    } bind def\n"
"\n"
"% Function cleanup: terminates printing, flushing last page if necessary.\n"
"/cleanup\n"
"    { twinpage sheetside 1 eq and\n"
"         { printpage }\n"
"      if\n"
"    } bind def\n"
"\n"
"%\n"
"% Function startpage: prints page header and page border and initializes\n"
"% printing of the file lines.  Page number is stored on the top of stack,\n"
"% and file name is next.\n"
"/startpage\n"
"    { /pagenum exch def\n"
"      sheetside 0 eq\n"
"	{ % /pagesave save def\n"
"	  landscape\n"
"	    { sheetwidth 0 inch translate	% new coordinates system origin\n"
"	      90 rotate				% landscape format\n"
"	      paper_adjust neg 0 translate\n"
"	    } if\n"
"	} if\n"
"      noborder not { printborder } if\n"
"      noheader not { printheader } if\n"
"      nofooter not { printfooter } if\n"
"      nopunchmark not { punchmark } if\n"
"	 upperx sheetside get  bodymargin  add\n"
"	    uppery\n"
"	    bodymargin bodyfontsize add  noheader {0} {headersize} ifelse  add\n"
"	 sub\n"
"      moveto\n"
"    } bind def\n"
"\n"
"% Function printheader: prints page header.\n"
"/printheader\n"
"    { upperx sheetside get  uppery headersize sub 1 add  moveto\n"
"      datefont setfont\n"
"      gsave\n"
"        datefontsize headermargin rmoveto\n"
"	date show					% date/hour\n"
"      grestore\n"
"      gsave\n"
"	pagenum pnum cvs pop\n"
"	   pagewidth pagenumwidth sub\n"
"	   headermargin\n"
"	rmoveto\n"
"        (Page ) show pnum show				% page number\n"
"      grestore\n"
"      empty pnum copy pop\n"
"      gsave\n"
"        filenamefont setfont\n"
"	      filenameroom fname stringwidth pop sub 2 div datewidth add\n"
"	      filenamefontsize 2 mul \n"
"	   add \n"
"	   headermargin\n"
"	rmoveto\n"
"        fname show						% file name\n"
"      grestore\n"
"    } bind def\n"
"\n"
"% Function printfooter: prints page footer.\n"
"/printfooter\n"
"    { upperx 0 get sheetnumbery moveto\n"
"      footerfont setfont\n"
"      fname show\n"
"    } bind def\n"
"\n"
"% Function printborder: prints border page.\n"
"/printborder \n"
"    { upperx sheetside get uppery moveto\n"
"      gsave					% print the four sides\n"
"        pagewidth 0 rlineto			% of the square\n"
"        0 pageheight neg rlineto\n"
"        pagewidth neg 0 rlineto\n"
"        closepath stroke\n"
"      grestore\n"
"      noheader not\n"
"         { 0 headersize neg rmoveto pagewidth 0 rlineto stroke }\n"
"      if\n"
"    } bind def\n"
"\n"
"% Punch Marker\n"
"/punchmark {\n"
"  gsave\n"
"    newpath punchx punchy moveto\n"
"    punchx 2 add punchy -0.5 add lineto\n"
"    punchx 2 add punchy 0.5 add lineto\n"
"    punchx punchy lineto\n"
"    closepath\n"
"    0 setgray .8 setlinewidth stroke\n"
"  grestore\n"
"  } bind def\n"
"\n"
"%\n"
"% Function endpage: adds a sheet number to the page (footnote) and prints\n"
"% the formatted page (physical impression). Activated at the end of each\n"
"% source page (lines reached or FF character).\n"
"/endpage\n"
"   { twinpage  sheetside 0 eq  and\n"
"        { /sheetside 1 def }\n"
"        { printpage }\n"
"     ifelse\n"
"   } bind def\n"
"\n"
"% Function sheetnumber: prints the sheet number.\n"
"/sheetnumber\n"
"    { sheetnumberx sheetnumbery moveto\n"
"      datefont setfont\n"
"      sheet pnum cvs\n"
"	 dup stringwidth pop (0) stringwidth pop sub neg 0 rmoveto show\n"
"      empty pnum copy pop\n"
"    } bind def\n"
"\n"
"% Function bs: go back one character width to emulate BS\n"
"/bs { backspacewidth neg 0 rmoveto } bind def\n"
"\n"
"% Function s: print a source string\n"
"/s  { fontarray fonttype get setfont\n"
"      show\n"
"    } bind def\n"
"\n"
"% Function ks: print a kanji source string\n"
"/ks { kfontarray fonttype get setfont\n"
"      KanjiRomanDiffHalf 0 rmoveto\n"
"      KanjiRomanDiff 0 3 -1 roll ashow\n"
"      KanjiRomanDiffHalf neg 0 rmoveto\n"
"    } def\n"
"\n"
"% Function bl: beginning of line\n"
"/bl { gsave } bind def\n"
"\n"
"% Function nl: newline\n"
"/nl {\n"
"      grestore\n"
"      0 bodyfontsize 1.1 mul neg rmoveto\n"
"    } bind def\n";

static	time_t	nowtime;
static	void
PS_Init(void)
{

	time(&nowtime);

	PrintString("%!PS-Adobe-1.0\n");
	sprintf(sbuf,"%%%%Title: %s\n",fnInput);
	PrintString(sbuf);
	PrintString("%%Creator: pf\n");
	sprintf(sbuf,"%%%%CreationDate: %s",ctime(&nowtime));
	PrintString(sbuf);
	PrintString("%%Pages: (atend)\n");
	PrintString("%%PageOrder: Ascend\n");
	sprintf(sbuf,"%%%%DocumentPaperSizes: %s\n",PaperTray);
	PrintString(sbuf);
	PrintString("%%Orientation: ");
	if		(  fLandscape  ) {
		PrintString("Landscape\n");
	} else {
		PrintString("Portrait\n");
	}
	PrintString("%%EndComments\n\n");
	PrintString("/$a2psdict 100 dict def\n");
	PrintString("$a2psdict begin\n");
	PrintString("% Initialize page description variables.\n");
	PrintString("/inch {72 mul} bind def\n");

	fNewPrint = TRUE;
	cSheet = 1;
	SetOld = SET_NULL;
}


static	void
PS_SetPaper(
	PrinterControl	*p)
{
	PrintString("%% SelectTray\n");
	sprintf(sbuf,"statusdict /%stray known { statusdict begin %stray end } if\n"
			,PaperTray,PaperTray);
	PrintString(sbuf);
	sprintf(sbuf,"/landscape %s def\n", ( fLandscape ? "true" : "false" ));
	PrintString(sbuf);
	sprintf(sbuf,"/twinpage %s def\n", ( fLandscape ? "true" : "false" ));
	PrintString(sbuf);
	sprintf(sbuf,"/sheetheight %g inch def\n", p->PageLength / 180.0);
	PrintString(sbuf);
	sprintf(sbuf,"/sheetwidth %g inch def\n", p->PageWidth / 180.0);
	PrintString(sbuf);
	sprintf(sbuf,"/lmargin %g inch def\n",TopMargine);
	PrintString(sbuf);
	sprintf(sbuf,"/smargin %g inch def\n",LeftMargine);
	PrintString(sbuf);
	PrintString("/paper_adjust 0.05 inch def\n");						/*	yet support	*/
	sprintf(sbuf,"/noborder %s def\n",( fBorder ? "false" : "true" ));
	PrintString(sbuf);
	PrintString("/noheader true def\n");
	PrintString("/headersize 0.0 def\n");
	PrintString("/nofooter true def\n");
	PrintString("/nopunchmark true def\n");
	sprintf(sbuf,"/bodyfontsize %g def\n",FontSize);
	PrintString(sbuf);
	PrintString("/kanjiAsciiRatio 1 def\n");
	sprintf(sbuf,"/lines %d def\n", p->PageLines);
	PrintString(sbuf);
	sprintf(sbuf,"/columns %d def\n", p->LineColumns);
	PrintString(sbuf);
	sprintf(sbuf,"/date ( ) def\n");
	PrintString(sbuf);
	PrintString("/doasciimag false def\n");
	/*	printf("/doasciimag true def /asciimagsize %f def\n", $ascii_mag);	*/

	PrintString(PrintTemplate);

//	PrintString("/#copies 1 def\n");

	PrintString("/R { /fonttype 0 def } bind def\n");
	PrintString("/B { /fonttype 1 def } bind def\n");
	PrintString("/I { /fonttype 2 def } bind def\n");
	sprintf(sbuf,"%s\n",Font);
	PrintString(sbuf);
	PrintString("/docsave save def\n");
	PrintString("startdoc\n");
	PrintString("%%EndProlog\n\n");
}

static	void
PS_NewPage(
	Bool	fLast)
{

	if		(  !fNewPrint  ) {
		PrintString("\nendpage\n");
		if		(	(  !fLandscape  )
				 ||	(  ( cPage % 2 )  ==  1  ) ) {
			PrintString("cleanup\n");
			cSheet ++;
		}
	}
	if		(  !fLast  ) {
		if		(	(  !fLandscape  )
				 ||	(  ( cPage % 2 )  ==  1  ) ) {
			sprintf(sbuf,"%%%%Page: %d %d\n",cSheet,cSheet);
			PrintString(sbuf);
			sprintf(sbuf,"/sheet %d def\n",cSheet);
			PrintString(sbuf);
			sprintf(sbuf,"%s setlabel\n",fnInput);
			PrintString(sbuf);
		}
		sprintf(sbuf,"%d startpage\n",cPage);
		PrintString(sbuf);
	} else {
		PrintString("cleanup\n");
		PrintString("%%Trailer\n");
		PrintString("docsave restore end\n");
	}
	fKanjiInit = FALSE;
}

static	void
PS_NewLine(void)
{
	if		(  fNewLine  ) {
		PrintString("bl ( ) s nl\n");
	} else {
		PrintString(" nl\n");
	}
	SetOld = SET_NULL;
}

static	void
PS_PrintChar(
	int		c)
{
	switch	(c) {
	  case	'(':
		PrintString("\\(");
		break;
	  case	')':
		PrintString("\\)");
		break;
	  case	'\\':
		PrintString("\\\\");
		break;
	  default:
		PrintChar(c);
		break;
	}
}

static	void
PS_PrintSBCS(
	Char	ch)
{
	word	code;

	code = CODE_Char(ch);
	PS_PrintChar(CODE_LOWER(code));
	fNewPrint = FALSE;
}

extern	void
PS_PrintDBCS(
	Char	ch)
{	word	code;

	code = CODE_Char(ch);
	PS_PrintChar(CODE_UPPER(code));
	PS_PrintChar(CODE_LOWER(code));
	fNewPrint = FALSE;
}

static	void
PS_Select(
	word	set)
{
	if		(  fNewLine  ) {
		PrintString("bl ( ) s");
	}
	if		(  SetOld  ==  SET_ASCII  ) {
		PrintString(") s");
	} else
	if		(  SetOld  ==  SET_JIS83  ) {
		PrintString(") ks");
	} else {
		PrintString("");
	}
	if		(  set  ==  SET_JIS83  ) {
		if		(  !fKanjiInit  ) {
			PrintString(" kanji_init");
			fKanjiInit = TRUE;
		}
	}
	SetOld = set;
	if		(  set  !=  SET_NULL  ) {
		PrintString(" (");
	}
}

PrinterControl	PS_D = {
	"PS",
	PS_Select,
	PS_NewPage,
	PS_Init,
	PS_NewLine,
	PS_PrintSBCS,
	PS_PrintDBCS,
	PS_SetPaper,
};

static	void
Help(void)
{
	fprintf(stderr,"\tR\tpretty ANK font\n");
	fprintf(stderr,"\tB\tbold font\n");
	fprintf(stderr,"\tI\titallic font\n");
	fprintf(stderr,"\tUS\tpaper size is US letter\n");
	fprintf(stderr,"\tA4\tpaper size is A4\n");
	fprintf(stderr,"\tB4\tpaper size is B4\n");
	fprintf(stderr,"\tLANDSCAPE\tlandscape\n");
	fprintf(stderr,"\tBORDER\tprint paper border line\n");
}

typedef	struct {
	char	*name;
	double	FontSize
	,		TopMargine
	,		LeftMargine;
	int		LinePitch
	,		PitchSBCS
	,		PitchDBCS
	,		PageLength
	,		PageWidth
	,		PageLines
	,		LineLength;
	char	*tray;
}	PaperType;

PaperType	PaperTypes[] = {
{	"US",	10.5,	0.65,	1.2,	30,		18,		36,		1580,	1990,	66,	1990,	"letter"},
{	"USL",	6.85,	1.20,	0.65,	30,		18,		36,		1990,	1580,	52,	1580,	"letter"},
{	"A4",	10,		1.20,	1.20,	30,		18,		36,		2095,	1488,	70,	1488,	"a4"},
{	"A4L",	6.60,	1.20,	1.20,	30,		18,		36,		2095,	1488,	68,	1488,	"a4"},
{	"B4",	9.5,	1.40,	1.20,	30,		18,		36,		2575,	1827,	86,	1827,	"b4"},
{	"B4L",	8.50,	1.20,	1.40,	30,		18,		36,		1827,	2575,	60,	2575,	"b4"},
{	NULL,	9.5,	0.64,	1.2,	30,		18,		36,		1440,	1980,	66,	1980,	"letter"}
};

static	void
SetPaperDimension(
	PrinterControl	*p,
	char	*PaperSize,
	int		LinePitch,
	int		PitchSBCS,
	int		PitchDBCS,
	int		PageLength,
	int		PageWidth,
	int		PageLines,
	int		LineLength)
{
	int		i;

	sprintf(sbuf,"%s%s",PaperSize, ( fLandscape ? "L" : "" ) );
	for	( i = 0 ; PaperTypes[i].name  !=  NULL ; i ++ ) {
		if		(  !stricmp(PaperTypes[i].name,sbuf)  ) {
			break;
		}
	}
	if		(  PaperTypes[i].name  !=  NULL  ) {
		FontSize = PaperTypes[i].FontSize;
		LeftMargine = PaperTypes[i].LeftMargine;
		TopMargine = PaperTypes[i].TopMargine;
		p->LinePitch = PaperTypes[i].LinePitch;
		p->PitchSBCS = PaperTypes[i].PitchSBCS;
		p->PitchDBCS = PaperTypes[i].PitchDBCS;
		p->PageLength = PaperTypes[i].PageLength;
		p->PageWidth = PaperTypes[i].PageWidth;
		p->PageLines = PaperTypes[i].PageLines;
		p->LineLength = PaperTypes[i].LineLength;
		p->LineColumns = PaperTypes[i].LineLength / PaperTypes[i].PitchSBCS;
		PaperTray = PaperTypes[i].tray;
	} else {
		FontSize = PaperTypes[i].FontSize;
		LeftMargine = 0;
		TopMargine = 0;
		p->LinePitch = LinePitch;
		p->PitchSBCS = PitchSBCS;
		p->PitchDBCS = PitchDBCS;
		p->PageLength = PageLength;
		p->PageWidth = PageWidth;
		p->PageLines = PageLines;
		p->LineLength = LineLength;
		p->LineColumns = LineLength / PitchSBCS;
		PaperTray = PaperTypes[i].tray;
	}
}

/*
 *	PS:A4:LANDSCAPE:BORDER
 */
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;
	char	*p
	,		*q;
	char	*PaperSize;

	fLandscape = FALSE;
	fBorder = FALSE;

	p = parameter;
	PaperSize ="";
	Font = "R";
	do {
		if		(  ( q = strchr(p,':') )  !=  NULL  ) {
			*q = 0;
		}
		if		(  !stricmp(p,"A4")  ) {
			PaperSize = "A4";
		} else
		if		(  !stricmp(p,"B4")  ) {
			PaperSize = "B4";
		} else
		if		(  !stricmp(p,"US")  ) {
			PaperSize = "US";
		} else
		if		(  !stricmp(p,"landscape")  ) {
			fLandscape = TRUE;
		}
		if		(  !stricmp(p,"border")  ) {
			fBorder = TRUE;
		}
		if		(  !stricmp(p,"R")  ) {
			Font = "R";
		}
		if		(  !stricmp(p,"B")  ) {
			Font = "B";
		}
		if		(  !stricmp(p,"I")  ) {
			Font = "I";
		}
		if		(  q  !=  NULL  ) {
			p = q + 1;
		}
	}	while	(  q  !=  NULL  );
	ret = &PS_D;
	if		(  ret  !=  NULL  ) {

		ret->TabWidth = TabWidth;
		ret->fPrintPageHeader = fPrintPageHeader;
		ret->TopMargineInLines = TopMargineInLines;
		ret->LeftMargine = LeftMargine;

		SetPaperDimension(ret,PaperSize,LinePitch,PitchSBCS,PitchDBCS,
						  PageLength,PageWidth,PageLines,LineLength);
	}
	return	(ret);
}

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

