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 / ps2epsi.ps < prev    next >
Text File  |  1996-09-22  |  7KB  |  236 lines

  1. %    Copyright (C) 1990, 1995 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. % Convert an arbitrary PostScript file to an EPSI file.
  16. %
  17. % Original version contributed by
  18. %   George Cameron <george@bio-medical-physics.aberdeen.ac.uk>
  19. % Patched 7/26/95 by
  20. %   Greg P. Kochanski <gpk@hce.hst.mh.att.com> or <gpk@physics.att.com>
  21. %   to add many new DSC comments and make the comments conforming.
  22. % Please contact these users if you have questions.
  23. %
  24.  
  25. % Initialize, and redefine copypage and showpage.
  26.  
  27. % ps2edict is defined in the pre-loaded code created by the ps2epsi script.
  28. % /ps2edict 25 dict def
  29. ps2edict begin
  30.  
  31.                 % The main procedure
  32.   /ps2epsi
  33.    {                % Open the file
  34.      outfile (w) file /epsifile exch def
  35.                     % Get the device parameters
  36.      currentdevice getdeviceprops .dicttomark
  37.      /HWSize get aload pop
  38.        /devheight exch def
  39.        /devwidth exch def
  40.      matrix defaultmatrix
  41.        /devmatrix exch def
  42.                 % Make a corresponding memory device
  43.      devmatrix devwidth devheight <ff 00>
  44.      makeimagedevice
  45.      /arraydevice exch def
  46.      arraydevice setdevice  % (does an erasepage)
  47.      /rowwidth devwidth 7 add 8 idiv def
  48.      /row rowwidth string def
  49.                 % Replace the definition of showpage
  50.      userdict /showpage { ps2edict begin epsipage end } bind put
  51.      userdict /setfont { ps2edict begin epsisetfont end } bind put
  52.    } def
  53.  
  54.  /epsifontdict 100 dict def
  55.  
  56.  /epsisetfont
  57.  {
  58.  % code here keeps a list of font names in dictionary epsifontdict
  59.  /tmpfont exch def
  60.  /tmpfontname tmpfont /FontName get def
  61.  epsifontdict tmpfontname known not { epsifontdict tmpfontname 0 put } if
  62.  epsifontdict tmpfontname
  63.     epsifontdict tmpfontname get 1 add put
  64.  tmpfont setfont
  65.  } bind def
  66.  
  67.  /margintest
  68.  {
  69.       { dup arraydevice exch row copyscanlines pop
  70.     -1 0 1 rowwidth 1 sub
  71.     { dup row exch get 0 ne { exit }{ pop } ifelse
  72.     } for
  73.     dup -1 ne { exch pop } if
  74.     -1 ne { exit } if pop
  75.       } for
  76.  } bind def
  77.  
  78.  
  79.   /epsiNameStr 200 string def
  80.   /epsiNpages 0 def
  81.   /epsiNpageStr 20 string def
  82.   /epsipage
  83.    {
  84.      /epsiNpages epsiNpages 1 add def
  85.      /loopcount devheight 1 sub def
  86.  
  87.      % Find top margin
  88.      -1 0 1 loopcount margintest
  89.      dup -1 eq { (blank page!!\n) print quit }{ exch pop } ifelse 
  90.      /tm exch def
  91.  
  92.      % Find bottom margin
  93.      loopcount -1 0 margintest
  94.      /bm exch def
  95.      
  96.      % Initialise limit variables
  97.      /loopcount rowwidth 1 sub def
  98.      /lm loopcount def /lmb 0 def
  99.      /rm 0 def /rmb 0 def
  100.  
  101.      % Find left and right boundaries of image
  102.      tm 1 bm
  103.       { % Get more data
  104.     arraydevice exch row copyscanlines pop
  105.     % Scan from left to find first non-zero element
  106.     % We save first the element, then the index
  107.     -1 0 1 loopcount
  108.     { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  109.     } for
  110.     % If we found -1, row is blank ..
  111.     dup -1 ne 
  112.     { % Find the leftmost index
  113.           dup lm lt
  114.           % If the new index is less, we save index and element
  115.           { /lm exch def /lmb exch def }
  116.           % If the index is equal, we or the bits together
  117.           { lm eq { lmb or /lmb exch def }{ pop } ifelse
  118.           } ifelse
  119.       % Now find the rightmost index
  120.       loopcount -1 0
  121.           { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  122.           } for
  123.       dup rm gt
  124.           % If the new index is greater, we save index and element
  125.           { /rm exch def /rmb exch def }
  126.           % If the index is equal, or the bits
  127.           { rm eq { rmb or /rmb exch def } { pop } ifelse
  128.           } ifelse
  129.     } if
  130.     pop
  131.       } for
  132.  
  133.      % Now we find the real left & right bit positions
  134.      256 0 1 7
  135.      { exch 2 div dup lmb le { pop exit }{ exch pop } ifelse
  136.      } for
  137.      /lmb exch def
  138.  
  139.      1 7 -1 0
  140.      { exch dup dup rmb and eq { pop exit }{ 2 mul exch pop } ifelse
  141.      } for
  142.      /rmb exch def
  143.  
  144.      % Calculate the bounding box values
  145.      /llx lm 8 mul lmb add  1 sub def
  146.      /lly devheight bm sub  1 sub def
  147.      /urx rm 8 mul rmb add 1 add def
  148.      /ury devheight tm sub 1 add def
  149.  
  150.     % Write out the magic string and bounding box information
  151.      epsifile (%!PS-Adobe-2.0 EPSF-1.2\n) writestring
  152.      epsifile epsititle writestring
  153.      epsifile epsicreator writestring
  154.      epsifile epsicrdt writestring
  155.      epsifile epsifor writestring
  156.      epsifile flushfile
  157.  
  158.     % Write out the page count:
  159.      epsifile (%%Pages: ) writestring
  160.      epsifile epsiNpages epsiNpageStr cvs writestring
  161.      epsifile (\n) writestring
  162.      epsifile flushfile
  163.  
  164.     % Write out the list of used fonts:
  165.      epsifile (%%DocumentFonts:) writestring
  166.      epsifontdict {
  167.                     epsifile ( ) writestring
  168.                     pop epsiNameStr cvs epsifile exch writestring
  169.                     } forall
  170.      epsifile (\n) writestring
  171.      epsifile flushfile
  172.  
  173.      epsifile (%%BoundingBox: ) writestring
  174.      epsifile llx write==only epsifile ( ) writestring
  175.      epsifile lly write==only epsifile ( ) writestring
  176.      epsifile urx write==only epsifile ( ) writestring
  177.      epsifile ury write==
  178.      epsifile (%%BeginPreview: ) writestring
  179.      epsifile urx llx sub 1 add write==only epsifile ( ) writestring
  180.      epsifile bm tm sub 1 add write==only epsifile ( 1 ) writestring
  181.      epsifile bm tm sub 1 add write==
  182.      epsifile flushfile
  183.  
  184.     % Define character and bit widths for the output line buffer:
  185.      /cwidth rm lm sub 1 add 8 mul 7 add 8 idiv def
  186.      /bwidth cwidth 8 mul def
  187.      /owidth urx llx sub 1 add 7 add 8 idiv def
  188.      /out cwidth string def
  189.  
  190.      % Create a 1-bit-high device for bitblt to align with the bbox
  191.      gsave
  192.      matrix cwidth 8 mul 1 <00 ff> makeimagedevice setdevice
  193.  
  194.      % 'image' a zero string to clear the line device
  195.      bwidth 1 1 matrix cwidth string image
  196.  
  197.      tm 1 bm
  198.       { % Get a scan line interval from the array device
  199.     arraydevice exch row copyscanlines lm cwidth getinterval
  200.     lmb 0 gt
  201.     { % 'image' it into the line device with the lmb offset
  202.       bwidth 1 1 [1 0 0 1 lmb 0] 5 -1 roll image
  203.       % Now we get the modified scan line
  204.       currentdevice 0 out copyscanlines 0 owidth getinterval
  205.     } if
  206.     % Write out the hex data
  207.     epsifile (% ) writestring 
  208.     epsifile exch writehexstring
  209.     epsifile (\n) writestring
  210.       } for
  211.  
  212.      epsifile (%%EndImage\n) writestring
  213.      epsifile (%%EndPreview\n) writestring
  214.      epsifile flushfile
  215.      grestore
  216.      erasepage initgraphics
  217.  
  218.      DonePage 0 1 put
  219.    } bind def
  220.  
  221.  
  222. (outfile) getenv
  223.   { /outfile exch def 
  224.     ps2epsi
  225.  
  226.     /DonePage 1 string def
  227.     (%stdin) (r) file cvx execute0
  228.     DonePage 0 get 0 eq { showpage } if
  229.   } if
  230.  
  231. end
  232. quit
  233.