home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / gs403osk.tgz / gs403osk.tar / pdf_2ps.ps < prev    next >
Text File  |  1996-10-12  |  8KB  |  266 lines

  1. %    Copyright (C) 1994, 1996 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % pdf_2ps.ps
  16. % PDF to PostScript additions to PDF reader.
  17.  
  18. GS_PDF_ProcSet begin
  19. pdfdict begin
  20.  
  21. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  22. .currentglobal true .setglobal
  23.  
  24. userdict /pdf2psdict 30 dict put
  25. pdf2psdict begin
  26.  
  27. % Generate a sufficiently unique name (at least unique within the current
  28. % save/restore environment).
  29. /uniqueid#
  30.  { userdict length
  31.     { dup neg =string cvs dup 0 (_) putinterval cvn
  32.       userdict 1 index known not { exch pop exit } if pop 1 add
  33.     }
  34.    loop
  35.  } bdef
  36.  
  37. % "Wrap" all the runtime operators so they call #exec.
  38. % Make sure we have enough room in the current dictionary for this.
  39. currentdict dup maxlength numargsdict length add .setmaxlength
  40. numargsdict
  41.  { 1 index load exch 2 index exch /#exec cvx 4 packedarray cvx def
  42.  }
  43. forall
  44. /pdfmark
  45.   /pdfmark load dup type /operatortype eq { 1 packedarray cvx } if
  46.    { /pdfmark counttomark 1 sub #exec } bind
  47.   aload length 1 add packedarray cvx
  48. def
  49. % Define #exec so it also prints out its arguments.
  50. /dictwrite#     % <file> <dict> dictwrite# -
  51.  { dup length 240 le
  52.     { 1 index (mark ) writestring
  53.        { exch 2 index exch write#
  54.      1 index ( ) writestring 1 index exch write#
  55.      2 index (\n) writestring
  56.         }
  57.       forall dup (.dicttomark) writestring
  58.     }
  59.     { 2 copy length write=only 1 index ( dict\n) writestring
  60.        { exch 2 index dup (dup ) writestring exch write#
  61.      1 index dup ( ) writestring exch write#
  62.      dup ( put\n) writestring
  63.        }
  64.       forall
  65.     }
  66.    ifelse pop
  67.  } bdef
  68. /fontwrite#     % <file> <font> fontwrite# -
  69.  {      % Find the named font and then modify it.
  70.    2 copy /FontName get =string cvs
  71.     { dup dup length 1 sub 1 getinterval (%) ne { exit } if
  72.       0 1 index length 1 sub getinterval
  73.     }
  74.    loop cvn write===only 1 index ( findfont ) writestring
  75.         % Load the appropriate Encoding, by name if possible.
  76.    dup /Encoding get
  77.    dup dup StandardEncoding eq exch ISOLatin1Encoding eq or
  78.         % Stack: file font encoding stdbool
  79.     { StandardEncoding eq { (StandardEncoding) } { (ISOLatin1Encoding) } ifelse
  80.       2 index exch writestring
  81.     }
  82.     { 2 index exch write#
  83.     }
  84.    ifelse
  85.         % Check for modified Metrics.
  86.    dup /Metrics .knownget
  87.     { 2 index ( ) writestring
  88.       2 index exch write#
  89.     }
  90.     { 1 index ( null) writestring
  91.     }
  92.    ifelse
  93.    1 index ( .updatefont { /_ exch definefont } if\n) writestring
  94.    pop pop
  95.  } bdef
  96. /write#dict 10 dict dup begin
  97.   /arraytype
  98.    { dup xcheck { (}) ({) } { (]) ([) } ifelse
  99.      2 index length 0 eq
  100.       { 3 index exch writestring exch
  101.       }
  102.       { 3 -1 roll
  103.      { 3 index 2 index writestring 3 index exch write# pop (\n) }
  104.     forall
  105.       }
  106.      ifelse pop writestring
  107.    } bdef
  108.   /dicttype
  109.    { null userdict { 3 index eq { exch pop exit } if pop } forall
  110.      dup null eq
  111.       { pop 2 copy dup /FID known { fontwrite# } { dictwrite# } ifelse
  112.     1 index ( userdict ) writestring
  113.     uniqueid# 2 index 1 index write# 2 index ( 2 index put) writestring
  114.     userdict exch 3 -1 roll put pop
  115.       }
  116.       { exch pop cvx write===only
  117.       }
  118.      ifelse
  119.    } bdef
  120.   /marktype
  121.    { pop ([) writestring
  122.    } bdef
  123.   /packedarraytype
  124.    /arraytype load def
  125.   /realtype
  126.    { dup abs 16#ffffff le { dup dup cvi eq { cvi } if } if write=only
  127.    } bdef
  128. end def
  129. /write#
  130.  { dup type //write#dict exch .knownget { exec } { write===only } ifelse
  131.  } bind def
  132.  
  133. % Rebind the procedures that conditionally write out PostScript.
  134.  
  135. /#          % <arg1> ... <argN> <opname> <N> # -
  136.  { 1 index load 3 1 roll #exec
  137.  } bdef
  138. /#?         % - #? <writing>
  139.  { /PSout where { pop true } { false } ifelse
  140.  } bdef
  141. /defined#       % <name> defined# <bool>
  142.  { dup where { exch get } { pop false } ifelse
  143.  } bdef
  144. /#exec      % <arg1> ... <argN> <proc|operator> <opname> <N> #exec -
  145.  { /PSout where
  146.     { pop dup ([) eq { pop counttomark 1 sub } if
  147.       -1 1 { 1 add index PSout exch write# PSout ( ) writestring } for
  148.       PSout exch write=
  149.     }
  150.     { pop pop
  151.     }
  152.    ifelse exec
  153.  } bdef
  154. /#dsc       % mark <obj1> ... #dsc -
  155.  { /PSout where
  156.     { pop counttomark
  157.        { counttomark -1 roll PSout exch write=only }
  158.       repeat pop PSout (\n) writestring
  159.     }
  160.     { cleartomark
  161.     }
  162.    ifelse
  163.  } bdef
  164. /copyfile#  % <filename> copyfile# -
  165.     { findlibfile
  166.        { exch pop }
  167.        { (r) file }     % let the error happen
  168.       ifelse
  169.        { dup =string readline pop (%BEGIN) eq { exit } if
  170.        }
  171.       loop
  172.        { dup =string readline not { pop exit } if
  173.          dup (%END) eq { pop exit } if
  174.       { ( ) anchorsearch
  175.          { pop }
  176.          { (\t) anchorsearch { pop } { exit } ifelse }
  177.         ifelse
  178.       }
  179.      loop
  180.      dup () eq { true } { dup 0 1 getinterval (%) eq } ifelse
  181.       { pop }
  182.       { (%) search { exch pop exch pop } if mark exch #dsc }
  183.      ifelse
  184.        }
  185.       loop closefile
  186.     } bdef
  187. /#dscfile   % <filename> #dscfile -
  188.  { /PSout where
  189.     { pop /PSNoProcSet defined#
  190.        { PSout exch write===only PSout ( runlibfile\n) writestring }
  191.        { copyfile# }
  192.       ifelse
  193.     }
  194.     { pop
  195.     }
  196.    ifelse
  197.  } bdef
  198.  
  199. % Rebind Is, which constructs a data source for an image.
  200. % pdf_draw defined it to simply retrieve the stream.
  201. /ID_draw /ID load def
  202. /Is_draw /Is load def
  203. /EI_draw /EI load def
  204. userdict /Is_string null put    % establish a binding
  205. userdict /Is_data null put  % ditto
  206. /ID_proc1 {/ASCIIHexDecode filter} def      % no bind
  207. /ID_proc2 {/ASCII85Decode filter /RunLengthDecode filter} def   % no bind
  208. /ID
  209.  { /PSout where
  210.     { pop dup length 1 add dict copy
  211.       dup /FilterProc PSLevel1 { /ID_proc1 } { /ID_proc2 } ifelse load
  212.       /PSBinaryOK defined# { dup length 2 sub 2 exch getinterval } if
  213.       dup length 0 eq { pop pop pop } { put } ifelse
  214.     }
  215.    if ID_draw
  216.  } bdef
  217. /Is     % <imagedict> Is <imagedict> <datasource>
  218.  { /PSout where
  219.     { pop dup /DataSource get string /Is_string exch store
  220.       /Is_data [ PSout
  221.       PSLevel1
  222.        { /PSBinaryOK defined#
  223.       { /NullEncode } { /ASCIIHexEncode } ifelse
  224.      filter
  225.        }
  226.        { /PSBinaryOK defined#
  227.       { Is_string length /RunLengthEncode filter
  228.       }
  229.       { /ASCII85Encode filter
  230.         dup Is_string length /RunLengthEncode filter exch
  231.       }
  232.      ifelse
  233.        }
  234.       ifelse ] store
  235.       Is_draw
  236.        { Is_string readstring pop Is_data 0 get 1 index writestring }
  237.       aload length 1 add packedarray cvx
  238.     }
  239.     { Is_draw
  240.     }
  241.    ifelse
  242.  } bdef
  243. /EI
  244.  { /PSout where { pop Is_data { closefile } forall } { EI_draw } ifelse
  245.  } bdef
  246.  
  247. % Rebind readfontfilter, which constructs the filter that
  248. % reads the text of an embedded Type 1 (and eventually Type 3) font.
  249. /readfontfilter_orig /readfontfilter load def
  250. /readfontfilter     % <proc> readfontfilter <filter>
  251.  { /copyfontdata cvx 2 array astore cvx
  252.    0 () /SubFileDecode filter
  253.  } bdef
  254. /copyfontdata       % <string> <origproc> copyfontdata <substring>
  255.  { exec /PSout where { pop PSout 1 index writestring } if
  256.  } bdef
  257.  
  258. currentdict readonly pop end    % pdf2psdict
  259.  
  260. .setglobal
  261. end     % pdfdict
  262. end     % GS_PDF_ProcSet
  263.