home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 September / PCWorld_2001-09_cd.bin / Software / Vyzkuste / rychlokurz / gs700w32.exe / gs7.00 / lib / ps2epsi.ps < prev    next >
Text File  |  2001-04-03  |  9KB  |  264 lines

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