home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2002 February / INTERNET88.ISO / pc / software / windows / bits / pdf995 / data1.cab / Program_Executable_Files / res / ps2epsi.ps < prev    next >
Encoding:
Text File  |  2001-12-08  |  8.8 KB  |  265 lines

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