%    Copyright (C) 1994, 1995 Aladdin Enterprises.  All rights reserved.

% pdf_2ps.ps
% PDF to PostScript additions to PDF reader.

GS_PDF_ProcSet begin
pdfdict begin

/.setlanguagelevel where { pop 2 .setlanguagelevel } if
.currentglobal true .setglobal

% "Wrap" all the runtime operators so they call #exec.
numargsdict
 { 1 index load exch 2 index exch /#exec cvx 4 packedarray cvx def
 }
forall
/pdfmark
  /pdfmark load dup type /operatortype eq { 1 packedarray cvx } if
   { /pdfmark counttomark 1 sub #exec } bind
  aload length 1 add packedarray cvx
def
% Define #exec so it also prints out its arguments.
% Eventually this will move into pdf_base.
/dictwrite#		% <file> <dict> dictwrite# -
 { 2 copy length write=only 1 index ( dict) writestring
    { exch 2 index dup ( dup ) writestring exch write#
      1 index dup ( ) writestring exch write#
      dup ( put) writestring
    }
   forall pop
 } bdef
/write#dict 10 dict dup begin
  /arraytype
   { dup xcheck { (}) ({) } { (]) ([) } ifelse
     2 index length 0 eq
      { 3 index exch writestring exch
      }
      { 3 -1 roll
	 { 3 index 2 index writestring 3 index exch write# pop ( ) }
	forall
      }
     ifelse pop writestring
   } bdef
  /dicttype
   { dictwrite#
   } bdef
  /marktype
   { pop ([) writestring
   } bdef
end def
/write#
 { dup type //write#dict exch .knownget { exec } { write==only } ifelse
 } bind def

% Rebind the procedures that conditionally write out PostScript.

/#			% <arg1> ... <argN> <opname> <N> # -
 { 1 index load 3 1 roll #exec
 } bdef
/#exec		% <arg1> ... <argN> <proc|operator> <opname> <N> #exec -
 { /PSout where
    { pop -1 1 { 1 add index PSout exch write# PSout ( ) writestring } for
      PSout exch write=
    }
    { pop pop
    }
   ifelse exec
 } bdef
/#dsc		% mark <obj1> ... #dsc -
 { /PSout where
    { pop counttomark
       { counttomark -1 roll PSout exch write=only }
      repeat pop PSout (\n) writestring
    }
    { cleartomark
    }
   ifelse
 } bdef

% Rebind Is, which constructs a data source for an image.
% pdf_draw already redefined it to retrieve the stream.
/Is_draw /Is load def
/Ie_draw /Ie load def
userdict /Is_string null put	% establish a binding
/Is		% <imagedict> Is <imagedict> <datasource>
 { /PSout where
    { dup /DataSource get string /Is_string exch store
      /PStext PSout /ASCII85Encode filter def
      /PSdata PStext Is_string length /RunLengthEncode filter def
      Is_draw
       { Is_string readstring pop PSdata 1 index writestring }
      aload length 1 add packedarray cvx
    }
    { Is_draw
    }
   ifelse
 } bdef
/Ie
 { /PSout where { pop PSdata closefile PStext closefile } { Ie_draw } ifelse
 } bdef

% Rebind readfontfilter, which constructs the filter that
% reads the text of an embedded Type 1 (and eventually Type 3) font.
/readfontfilter_orig /readfontfilter load def
/readfontfilter		% <proc> readfontfilter <filter>
 { /copyfontdata cvx 2 array astore cvx
   0 () /SubFileDecode filter
 } bdef
/copyfontdata		% <string> <origproc> copyfontdata <substring>
 { exec /PSout where { pop PSout 1 index writestring } if
 } bdef

end		% pdfdict

.setglobal
end		% GS_PDF_ProcSet
