home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / GhostScript / !GhostScr / 6_01 / lib / ps2epsi.ps < prev    next >
Text File  |  2000-03-09  |  9KB  |  259 lines

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