[comp.lang.postscript] Quick "text-to-PS" PostScript header ala Glenn Reid

jipping@frodo.cs.hope.EDU (Mike Jipping) (02/19/88)

I've had a lot of requests for this -- so I'll repost it.  This is Glenn
Reid's work, not mine.  Direct all kudos, questions, and criticisms to him.
Enjoy.
         Mike Jipping
         Department of Computer Science
         Hope College
         jipping@cs.hope.edu
===============================CUT HERE==================================
>From adobe!greid%decwrl.dec.com%washington.ARPA@CSNET-RELAY Wed Apr 23 00:19:16 1986
Date: 22 Apr 1986 1650-PST (Tuesday)
To: Info-PostScript <glacier!info-postscript@SU-Score.ARPA>,
        Laser Lovers <laser-lovers@Washington.ARPA>
Subject: Quick "text-to-PS" PostScript header
Cc: Me <greid@decwrl.DEC.COM>
>From: Glenn Reid <adobe!greid@decwrl.DEC.COM>


This is a file which will allow a PostScript printer to behave like a
line printer for text files.  It is designed to be as short as possible
while maintaining great flexibility.  It works by reading from
"currentfile", and may simply be PREpended to the beginning of a text
file and shipped to the printer.
Unix(tm) example:

	prompt: cat printfile.ps textfile | lpr

There are a number of default parameters for page setup which may
easily be given alternate definitions by either prepending a short
text file or by downloading some permanenent definitions outside the
server loop of your PostScript printer (NOTE:  if you are using
TranScript(tm), make sure the included file starts with "%!").  These
include: top, left, and bottom margins, default font, point size,
leading, the capability for a header line and/or page numbering (as
well as specification of a default font and pointsize for the header
fields).  These are redefined by simply prepending lines like:

/header (printfile.ps Tue Apr 22 16:30:05 1986) def
/pagenumber false def
/fontname /Palatino-Roman def
/pointsize 12 def

I have tried to avoid complexity where possible, but it might be of use
to define a special dictionary for these parameters if they are to be
declared outside the serverloop.

NOTE:  this file MUST end with the line containing only the word
"printfile".  Everything after this line in the file will be considered
part of the DATA and will be printed on the page (include blank lines).

BUGS:  This is a very simple program.  It does not deal with tabs,
in-line font changes, or a host of other troublesome things.  You may
add whatever features you like to it.  Please mail me any
improvements you make.

Enough.  Herein follow two copies of the code, one fully commented, and
one with all unnecessary comments and whitespace stripped out (for
compactness)

---------------------- cut once -----------------------
%!-Adobe-1.0
%%Title: printfile.ps
%%Creator: Glenn Reid, Adobe Systems Incorporated
%%CreationDate: Tue Apr 22 15:35:53 1986
%%Pages: 0
%% Copyright (c) 1986 Glenn Reid and Adobe Systems Incorporated.
%% Use it, distribute it (with this message), but don't sell it.
%%
%% This program will take miscellaneous text concatenated at its
%% end and print it in some default font.  It is designed to be
%% minimal in its functionality, but flexible enough to be easily
%% adapted to other uses.

/in.	{72 mul}	def
/line	512	string def	% for input line buffering
/buff	10	string def	% for page number conversion

%% CHANGE THESE IF YOU LIKE

%% If any of these parameters are defined ahead of this file, then
%% those values will be used.  A typical way to use this is to
%% allow an application to redefine some of the parameters by
%% inserting definitions ahead of this file in the print stream.
%% Examples of redefinitions look like:
%% 
%%     /fontname /Palatino-Roman def
%%     /pointsize /13 def
%%     /header (document.ps) def
%%     /pagenumber false def
%%     /leftmargin 36 def
%% 
%% Any number (including 0) of these parameters may be defined to
%% be different from the defaults.  The defaults are quite
%% reasonable for general use, and will print in 11-point Courier
%% with a 12-point Courier-Bold header (only prints a header of
%% /header string is defined by the user).  Margins top, left and
%% bottom are: .75 in.,  .75 in., and .5 in.
%% 
%%  Otherwise, the following defaults will be used:

/leftmargin where not
    { /leftmargin .75 in. def } {pop} ifelse
/bottom where not
    { /bottom .5 in. def } {pop} ifelse
/startX where not
    { /startX leftmargin def } {pop} ifelse
/startY where not
    { /startY 10 in. def } {pop} ifelse
/fontname where not
    { /fontname /Courier def } {pop} ifelse
/pointsize where not
    { /pointsize 11 def } {pop} ifelse
/leading where not
    { /leading pointsize 2 add def } {pop} ifelse
/headerfontname where not
    { /headerfontname /Courier-Bold def } {pop} ifelse
/headerpointsize where not
    { /headerpointsize 12 def } {pop} ifelse
/headerfont
    headerfontname findfont headerpointsize scalefont
def
/page where not
    { /page 1 def } {pop} ifelse
/pagenumber where not
    { /pagenumber true def } {pop} ifelse

% This routine will print a header at the top of each page
/printheader {
    gsave
	currentdict /header known {
	    36 10.5 in. moveto
	    headerfont setfont
	    header show
	} if
	pagenumber {
	    8 in. 10.5 in. moveto
	    headerfont setfont
	    page buff cvs show
	    /page page 1 add def
	} if
    grestore
    startX startY moveto
} bind def

/crlf {
    leftmargin currentpoint exch pop	% replace X with leftmargin
    leading sub
    dup bottom le
    { pop pop showpage printheader }
    { moveto } ifelse
} bind def

/printfile {
    line readline		% read a line from the file
    {				% 'if' procedure body (+boolean)
	{			% 'while' procedure body
	    show crlf		% show what's left by readline
	    currentfile cvlit	% use as operand
	    line readline	% and read the next line
	    not {exit} if	% exit if no more input to be had
	} loop			% end of while-proc
    } if
    showpage			% when input file is exhausted
} bind def			% /printfile

fontname findfont pointsize scalefont setfont

printheader		% execute header
currentfile cvlit	% uses rest of this file as input

% everything following 'PrintFile' is treated as data, through EOF
printfile
---------------------- cut twice -----------------------
%! Adobe PasteUp Version 2.0 (35)
/in.{72
mul}def/line
512
string
def/buff
10
string
def/leftmargin
where
not{/leftmargin
.75
in.
def}{pop}ifelse/bottom
where
not{/bottom
.5
in.
def}{pop}ifelse/startX
where
not{/startX
leftmargin
def}{pop}ifelse/startY
where
not{/startY
10
in.
def}{pop}ifelse/fontname
where
not{/fontname/Courier
def}{pop}ifelse/pointsize
where
not{/pointsize
11
def}{pop}ifelse/leading
where
not{/leading
pointsize
2
add
def}{pop}ifelse/headerfontname
where
not{/headerfontname/Courier-Bold
def}{pop}ifelse/headerpointsize
where
not{/headerpointsize
12
def}{pop}ifelse/headerfont
headerfontname
findfont
headerpointsize
scalefont
def/page
where
not{/page
1
def}{pop}ifelse/pagenumber
where
not{/pagenumber
true
def}{pop}ifelse/printheader{gsave
currentdict/header
known{36
10.5
in.
moveto
headerfont
setfont
header
show}if
pagenumber{8
in.
10.5
in.
moveto
headerfont
setfont
page
buff
cvs
show/page
page
1
add
def}if
grestore
startX
startY
moveto}bind
def/crlf{leftmargin
currentpoint
exch
pop
leading
sub
dup
bottom
le{pop
pop
showpage
printheader}{moveto}ifelse}bind
def/printfile{line
readline{{show
crlf
currentfile
cvlit
line
readline
not{exit}if}loop}if
showpage}bind
def
fontname
findfont
pointsize
scalefont
setfont
printheader
currentfile
cvlit
printfile