[comp.lang.postscript] need *simple* way to print ascii files on laserwriter

wytten@umn-cs.cs.umn.edu (Dale R. Wyttenbach) (02/03/90)

Suppose you want to print the file /etc/hosts on a laserwriter that
is hooked up to your unix box on /dev/ttyh1.

What postscript commands do you put in FILE1 to make the command:

	cat FILE1 /etc/hosts > /dev/ttyh1

spit out /etc/hosts on the printer at 66 lines per page?  I want the
result to look as if you are looking at the file with vi: newlines
in the appropriate places, and tabs expanded to 8 spaces.  I don't
care what you do with other unprintable ascii characters; maybe print
their octal representation.

I'm assuming this can be done with < 10 lines of postscript code,
using a default resident font on the laserwriter.  (I already have
fancy programs like enscript that will do the job better)

Please respond via email.

Thanks

dale


 Dale Wyttenbach		     |		...rutgers!umn-cs!wytten
 wytten@cs.umn.edu		     |		   wytten@umnacvx.bitnet
Computer Science Department Systems Staff--University of Minnesota, Minneapolis

woody@rpp386.cactus.org (Woodrow Baker) (02/04/90)

In article <1990Feb2.220910.16236@umn-cs.cs.umn.edu>, wytten@umn-cs.cs.umn.edu (Dale R. Wyttenbach) writes:
> Suppose you want to print the file /etc/hosts on a laserwriter that
> is hooked up to your unix box on /dev/ttyh1.
>...with CR's

> in the appropriate places, and tabs expanded to 8 spaces.  I don't
> care what you do with other unprintable ascii characters; maybe print
> their octal representation.
> 
This question is so common, that we need to find a simple shorty to
send to people.  I propose that we sort of run an informal contest here
to see who can write the shortest file printer in PS.  At the minimum,
it should
1.	understand cr/lf's
2.	use courier as it's font
3.	expand tabs to blanks.

A routine from the Green book handles 1 and 2 nicely.  I thing it was
written by Glenn.  I have entered it below, and cleaned up the
bracketing so that it makes sense....

/buff 128 string def
/emulate1
	{
			{
			currentfile buff readstring
			exch show not {exit} if
			} loop
			showpage
	} bind def

72 750 moveto
/Courier findfont 10 scalefont setfont emulate1
..
....text follows......

Cheers
Woody
u
> Please respond via email.
> 
> Thanks
> 
> dale
> 
> 
>  Dale Wyttenbach		     |		...rutgers!umn-cs!wytten
>  wytten@cs.umn.edu		     |		   wytten@umnacvx.bitnet
> Computer Science Department Systems Staff--University of Minnesota, Minneapolis

zben@umd5.umd.edu (Ben Cranston) (02/06/90)

%!-Adobe-1.0
%%Title: asciiprint.ps
%%Creator: Ben Cranston <zben@umd2.umd.edu>
%%CreationDate: Mon Jul 31 1989
%%Pages 0

/Courier findfont 10 scalefont setfont
/colwid (m) stringwidth pop def
36 756 moveto
256 string
{ %exec
  { %loop over file
    dup currentfile exch readstring exch
    { %loop FF
      (\f) search exch
      { %loop LF
	(\n) search exch
	{ %loop CR
	  (\r) search exch
	  { %loop TAB
	    (\t) search exch
	    { %loop BS
	      (\b) search exch
	      show
	      not { exit } if
	      pop currentpoint exch colwid sub exch moveto
	    } loop %BS
	    not { exit } if
	    pop currentpoint exch 36 sub colwid div .49 add cvi
	    8 add 248 and colwid mul 36 add exch moveto
	  } loop %TAB
	  not { exit } if
	  pop 36 currentpoint exch pop moveto
	} loop %CR
	not { exit } if
	pop 36 currentpoint exch pop
	11 sub dup 25 le { %if
	  pop showpage 756
	} if
	moveto
      } loop %LF
      not { exit } if
      pop showpage 36 756 moveto
    } loop %FF
    not { exit } if
  } loop %over file
  pop showpage
} bind exec

asciiprint.ps
Ben Cranston <zben@umd2.umd.edu>

In article <17862@rpp386.cactus.org> Woodrow Baker <woody@rpp386.cactus.org>
caterwauls:

> [printing vanilla ascii files on PS printers] is so common, that we need
> to find a simple shorty ... I propose we sort of run an informal contest
> here to find who can write the shortest file printer in PS.  At a minimum
> it should understand cr/lf's, use courier as its font, expand tabs to blanks.

I actually wrote this several months ago as part of a project to print
output from an evil IBM VM/CMS system on LaserWriters.  The stupid code
(see below) in the LW input routine that converts carriage returns to
linefeeds makes it impossible to support overprinting on a line-by-line
basis by using bare CR as a move to column one.  Because of this the project
decided to use a C filter on the Unix machine to do the job, and I deleted
the machine readable of this in disgust and anger.  I redeveloped it this
afternoon as an entry to the informal contest.

It does FF, LF, CR, TAB, and BS, in that heirarchical order.  If you can
get the damn CR in, it will do CR to column one for overprinting.  The BS
support means ul-bs-letter or letter-bs-ul underlining will work too.

Charles B. (Ben) Cranston
Network Infrastructures Group
Computer Science Center
University of Maryland at College Park
of Ulm


Tabs:
1	22	333	4444	55555	666666	7777777	88888888	9999

Underlining type 1:

U_n_d_e_r_l_i_n_i_n_g_ _t_y_p_e _2_:

There is a formfeed at the end of this line --->

The operating principle can be understood just as the source program can be 
parsed: as a heirarchy of control character handlers.  All handlers have 
this same basic form.  First the remaining string is searched for the 
control character, but the result of the search is immediately buried on
the stack while the {remainder of the buffer|string before the control} is
processed.  After this, the boolean result of the search is used to control
exit from the loop.  If control remains in the loop, the control character
was indeed found, so the remainder of the loop does the specific action for
the control character and then resumes the loop.

This organization engenders certain consequences.  For example, the model of
handling TAB heirarchically over BS means that an attempt to backspace over
the space created by a TAB actually causes a backspace one normal character
width INTO the space created by the TAB.  This case is sufficiently weird
that I did not consider it a problem.

The hierarchy is:

1. FF (form feed) causes a break to the first character position on a new
page.  If FF is given at the very end of a file a blank page is produced.

2. LF (line feed) causes a break to the first character position of a new
line of the page, except when that would be below a certain point, in which
case a break to the top of the next page occurs instead (unmarked "if").

3. CR (carriage return) causes a break back to the first character position
of the CURRENT line.  The intent was to allow underlining and overprinting
on a line by line basis by using a bare CR between overprint segments.
However, the LaserWriter designers, in their infinite wisdom and bearing in
mind everything they had ever been taught about the operating system standing
between the user and the hardware*, saw fit to convert CRs to LFs when read
by the serial chip.  Strangely enough, this does not happen during feed from
the Appletalk(tm) network.  However, the Macintosh uses CRs instead of LFs
between lines, so the code doesn't work with the Mac SendPS programs either...

4. TAB (horizontal tab) causes a break to the next multiple of 8 characters
from the left margin.  The magic constant 248 is 0xF8 which throws away the
bottom 7 bits.  The tab spacing could probably be changed if one were to
make the appropriate adjustment to this number.

5. BS (backspace) causes the print position to move one character width back
to the left.  Note interaction with TAB described above.

If none of these control characters is found, the rest of the string is
typeset at the current position.  This means that there is nothing special
to break lines wider than 80 characters.  They just go on into the right
margin and fade away.  This is probably the single greatest shortcoming of
this approach, but it is not really obvious to me what the right thing to
do in this case might be...

* This is sarcasm.





Now, just to prove how truly demented I am, I'm going to copy the code into
the file here, just to show that it can process itself (tee hee!).



(dive, dive!)

%!-Adobe-1.0
%%Title: asciiprint.ps
%%Creator: Ben Cranston <zben@umd2.umd.edu>
%%CreationDate: Mon Jul 31 1989
%%Pages 0

/Courier findfont 10 scalefont setfont
/colwid (m) stringwidth pop def
36 756 moveto
256 string
{ %exec
  { %loop over file
    dup currentfile exch readstring exch
    { %loop FF
      (\f) search exch
      { %loop LF
	(\n) search exch
	{ %loop CR
	  (\r) search exch
	  { %loop TAB
	    (\t) search exch
	    { %loop BS
	      (\b) search exch
	      show
	      not { exit } if
	      pop currentpoint exch colwid sub exch moveto
	    } loop %BS
	    not { exit } if
	    pop currentpoint exch 36 sub colwid div .49 add cvi
	    8 add 248 and colwid mul 36 add exch moveto
	  } loop %TAB
	  not { exit } if
	  pop 36 currentpoint exch pop moveto
	} loop %CR
	not { exit } if
	pop 36 currentpoint exch pop
	11 sub dup 25 le { %if
	  pop showpage 756
	} if
	moveto
      } loop %LF
      not { exit } if
      pop showpage 36 756 moveto
    } loop %FF
    not { exit } if
  } loop %over file
  pop showpage
} bind exec

(surface, surface!)
-- 
Sig     DS.L    ('ZBen')       ; Ben Cranston <zben@Trantor.UMD.EDU>
* Network Infrastructures Group, Computer Science Center
* University of Maryland at College Park
* of Ulm

zben@umd5.umd.edu (Ben Cranston) (02/09/90)

%!-Adobe-1.0
%%Title: asciiprint.ps
%%Creator: Ben Cranston <zben@umd2.umd.edu>
%%CreationDate: Thu Feb 8 1990
%%Pages 0

/Courier findfont 10 scalefont setfont
/colwid (m) stringwidth pop def
36 756 moveto
256 string
{ %exec
   { %loop over file
      dup currentfile exch readstring exch
      { %loop FF
         (\f) search exch
         { %loop LF
            (\n) search exch
            { %loop CR
               (\r) search exch
               { %loop TAB
                  (\t) search exch
                  { %loop BS
                     (\b) search exch
                     show
                     not { exit } if
                     pop currentpoint exch colwid sub exch moveto
                  } loop %BS
                  not { exit } if
                  pop currentpoint exch 36 sub colwid div .49 add cvi
                  8 add dup 8 mod sub colwid mul 36 add exch moveto
               } loop %TAB
               not { exit } if
               pop 36 currentpoint exch pop moveto
            } loop %CR
            not { exit } if
            pop 36 currentpoint exch pop
            11 sub dup 25 le { %if
               pop showpage 756
            } if
            moveto
         } loop %LF
         not { exit } if
         pop showpage 36 756 moveto
      } loop %FF
      not { exit } if
   } loop %over file
   pop showpage
} bind exec

The tab code has been changed to make it more readable and modifiable.
The two 8s are the tab stops, you should be able to change them to any
other number.  The only other change is the indentation at three should
be a tad more readable than the old indentation at two.

-- 
Sig     DS.L    ('ZBen')       ; Ben Cranston <zben@Trantor.UMD.EDU>
* Network Infrastructures Group, Computer Science Center
* University of Maryland at College Park
* of Ulm