%!
%%Title: Lineprinter emulation
%%Notice: Copyright (C) 1991 Adobe Systems Incorporated. All rights reserved
%
% This file may be freely copied and redistributed as long as:
%   1) This entire notice continues to be included in the file, 
%   2) If the file has been modified in any way, a notice of such
%      modification is conspicuously indicated.
%
% PostScript, Display PostScript, and Adobe are registered trademarks of
% Adobe Systems Incorporated.
% 
% ************************************************************************
% THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO CHANGE WITHOUT
% NOTICE, AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY ADOBE SYSTEMS
% INCORPORATED. ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY OR 
% LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO WARRANTY OF ANY 
% KIND (EXPRESS, IMPLIED OR STATUTORY) WITH RESPECT TO THIS INFORMATION, 
% AND EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY, 
% FITNESS FOR PARTICULAR PURPOSES AND NONINFINGEMENT OF THIRD PARTY RIGHTS.
% ************************************************************************
%

%%BeginProcSet: LinePrinter 0 0
%
% The procedure EmulateLinePrinter, defined below, effectively converts a
% PostScript printer into a lineprinter for the duration of the current job.
% The procedure consumes the input byte stream, treating it as a series of
% lines, and producing a series of pages as necessary.  CTRL-L (^L) in the
% input stream is treated as a page break.
%
% The characteristics of the emulated lineprinter are specified in an array
% which is passed to EmulateLinePrinter.  For example:
%    [
%      /Courier    % Font name (the font may need to be downloaded)
%      14          % Pitch (characters per inch)
%      60          % Length (lines per page)
%      140         % Width (characters per line)
%      8           % Line spacing (lines per inch)
%      .5          % Left margin of form (inches)
%      .5          % Top margin of form (inches)
%      8.5         % X shift (inches) [portrait=0, landscape=paper width]
%      90          % Rotation (degrees) [portrait=0, landscape=90]
%      8.5         % Form height (inches; after landscape rotation)
%    ] EmulateLinePrinter
% 
% In this way a library of forms could be downloaded to the printer together
% with the EmulateLineprinter definition instead of sending them for each job. 
% e.g:
% 
%    /S612P [ ... ] def
%    /R814L [ ... ] def
%    /LG612P [ ... ] def
% 
% A given job would then download just the following simple line in front of
% the actual job (to use form R184L, for example):
% 
%    R814L EmulateLinePrinter
% 
% Note:
%    1. Lines longer than the specified width are truncated, not wrapped.
%    2. No control characters apart from ^M, ^J, ^L are recognized.
%    3. The specified font is assumed to be fixed pitch.

/EmulateLinePrinter
 % form EmulateLinePrinter --
{
  aload pop            % Unpack form array onto the stack

  % Put emulation parameters into named variables for convenient reference.

  /formHeight exch 72 mul def
  /pageRotation exch def
  /xShift exch 72 mul def
  /topMargin exch 72 mul def
  /leftMargin exch 72 mul def
  /lineSpacing exch 72 exch div neg def
  /pageWidth exch def
  /pageLength exch def
  /pitch exch def
  /fontName exch def

  % Establish font for printing.  Compute size based on emulation parameters.

  fontName findfont 

  dup 1 scalefont setfont    % Establish 1 pt font.
  ( ) stringwidth pop        % Width of fixed-pitch characters at 1pt.
  72 pitch div            % Intended width of characters (inch/pitch)
  exch div            % Font scaling factor

  dup /fontSize exch def    % For testing only

  scalefont setfont        % Establish actual printing font

  /buffer pageWidth 4 mul string def

  /finished false def        % Flag set true at EOF.
  /truncateLine false def    % Flag set true when current line too long.

  /BeginPage
    % -- BeginPage --
  {
    xShift 0 translate
    pageRotation rotate
    leftMargin formHeight topMargin sub translate
    /linesOnPage 0 def
  } def

  /EndPage
    % -- EndPage --
  {
    linesOnPage 0 ne {showpage} if
  } def

  /ShowLine
    % string ShowLine --
  {
    0 lineSpacing translate    % Move origin to beginning of new line
    0 0 moveto            % Current point at origin.
    show            % Render characters onto page.
    /linesOnPage linesOnPage 1 add def
  } def
 

  {%loop -- foreach page of input
    % Set page orientation and position origin for first line.

    BeginPage

    {%loop -- foreach line of input

      {currentfile buffer readline} stopped    {
    exch pop        % Assume rangecheck error: line too long
    true
        /truncateLine true def
      } if

      not {
        /finished true def
        dup length 0 eq {pop exit} if % Skip show if last line is empty
      } if

      (\014) search {
    {
          ShowLine        % Show characters before ^L
      pop            % Discard ^L
      EndPage        % Eject page
      BeginPage
      (\014) search not {    % Handle multiple ^L's in one buffer
        dup length 0 gt {
          ShowLine        % Show line after ^L
        }{
          pop        % Discard empty string
        } ifelse
        exit
      } if
    } loop
      }{
        ShowLine
      } ifelse

      truncateLine {          % Discard rest of long line
    {
      {currentfile buffer readline} stopped not
      {
        not {/finished true def} if
        pop
        exit
          } if
    } loop
        /truncateLine false def
      } if

      linesOnPage pageLength eq finished or {exit} if
    } loop

    EndPage
    finished {exit} if
  } loop
} bind def
%%EndProcSet

%
% Stephen Herzog   - herzogs@ucs.orst.edu
%
% Secure Design          (503) 758-0955
% PO Box 475
% Corvallis, OR 97339
%
% This file may be freely distributed as stated in Adobe's header!
%
%
% The rest of this file will set the correct parameters, and sets it up
% so you can prepend this to a text file and print it to a postscript
% printer.  The following definition was derived from the above
% instructions.  Mail ps-file-server@adobe.com to get this kind of stuff.
% Thanks Adobe!
%
% The definations added are:
%
% S814P   Standard 8.5x11, 8 lines per inch, 14 characters per inch, portrate
% S612P   Standard 8.5x11, 6 lines per inch, 12 characters per inch, portrate
% S814L   Standard 8.5x11, 8 lines per inch, 14 characters per inch, landscape
%
% Feel free to add your own!
%


/S814P   [  /Courier  % Font name (the font may need to be downloaded)
            14        % Pitch (characters per inch)
            80        % Length (lines per page)
           140        % Width (characters per line)
             8        % Line spacing (lines per inch)
             1        % Left margin of form (inches)
             1        % Top margin of form (inches)
             0        % X shift (inches) [portrait=0, landscape=paper width]
             0        % Rotation (degrees) [portrait=0, landscape=90]
            11        % Form height (inches; after landscape rotation)
    ] def


/S612P   [  /Courier  % Font name (the font may need to be downloaded)
            12        % Pitch (characters per inch)
            60        % Length (lines per page)
            80        % Width (characters per line)
             6        % Line spacing (lines per inch)
             1        % Left margin of form (inches)
             1        % Top margin of form (inches)
             0        % X shift (inches) [portrait=0, landscape=paper width]
             0        % Rotation (degrees) [portrait=0, landscape=90]
            11        % Form height (inches; after landscape rotation)
    ] def


/S814L   [  /Courier  % Font name (the font may need to be downloaded)
            14        % Pitch (characters per inch)
            52        % Length (lines per page)
           126        % Width (characters per line)
             8        % Line spacing (lines per inch)
             1        % Left margin of form (inches)
             1        % Top margin of form (inches)
           8.5        % X shift (inches) [portrait=0, landscape=paper width]
            90        % Rotation (degrees) [portrait=0, landscape=90]
           8.5        % Form height (inches; after landscape rotation)
    ] def

