home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / ghostscript / 8.64 / lib / ps2epsi.ps < prev    next >
Encoding:
Text File  |  2009-04-17  |  8.1 KB  |  253 lines

  1. %    Copyright (C) 1990, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: ps2epsi.ps 8726 2008-05-11 17:00:17Z alexcher $
  14. % Convert an arbitrary PostScript file to an EPSI file.
  15. %
  16. % Please do not contact these users if you have questions.  They no longer
  17. % have the time, interest, or current expertise to keep this code working.
  18. % If you find bugs, please send proposed fixes to bug-gs@ghostscript.com.
  19. %
  20. % Bug fix 2002-04-20 by rayjj: Bounding box was incorrect since it depended
  21. %   on the dither pattern and gray shade at the boundary. Changed to use
  22. %   8-bit grayscale preview image to allow correct bounding box (at the
  23. %   expense of a 8x larger preview image). Also moved .setsafe until after
  24. %   the device and file operations are complete (but still before the input
  25. %   file is processed.
  26. % Bug fix 2000-04-11 by lpd: if a font didn't have a FontName (which is the
  27. %   case for bitmap fonts produced by recent versions of dvips), setfont
  28. %   caused an error.
  29. % Bug fix 8/21/99 by lpd: many of the margin and width computations were
  30. %   wrong (off by 1).  The code only "worked" because the bugs were
  31. %   (mostly) in conservative directions.
  32. % Modified 3/17/98 by lpd to make it possible to run this file without
  33. %   running the ps2epsi script first, for debugging.
  34. % Bug fix 9/29/97 by lpd <ghost@aladdin.com>: if the page size wasn't an
  35. %   exact multiple of 8 bits, an incorrect bounding box (or a rangecheck
  36. %   error) could occur.
  37. % Patched 7/26/95 by
  38. %    Greg P. Kochanski <gpk@bell-labs.com>
  39. %   to add many new DSC comments and make the comments conforming.
  40. % Original version contributed by
  41. %    George Cameron <george@bio-medical-physics.aberdeen.ac.uk>
  42. %
  43.  
  44. % Initialize, and redefine copypage and showpage.
  45.  
  46. % ps2edict is normally defined in the pre-loaded code created by the
  47. % ps2epsi script.
  48. /ps2edict where { pop } { /ps2edict 25 dict def } ifelse
  49. ps2edict begin
  50.  
  51.                 % The main procedure
  52.   /ps2epsi
  53.    {                % Open the file
  54.      outfile (w) file /epsifile exch def
  55.                     % Get the device parameters
  56.      currentdevice getdeviceprops .dicttomark 
  57.      /HWSize get aload pop
  58.        /devheight exch def
  59.        /devwidth exch def
  60.      matrix defaultmatrix
  61.        /devmatrix exch def
  62.                 % Make a corresponding 8-bit deep memory device
  63.      devmatrix devwidth devheight
  64.      256 string 0 1 255 { 1 index exch dup 255 exch sub put } for
  65.      makeimagedevice
  66.      /arraydevice exch def
  67.      arraydevice
  68.      % Turn on anti-aliasing
  69.      mark /TextAlphaBits 4 /GraphicsAlphaBits 4 6 -1 roll
  70.      putdeviceprops
  71.      setdevice    % (does an erasepage)
  72.      /rowwidth devwidth def
  73.      /row rowwidth string def
  74.      /zerorow rowwidth string def    % all zero
  75.                 % Replace the definition of showpage
  76.      userdict /showpage { ps2edict begin epsipage end } bind put
  77.      userdict /setfont { ps2edict begin epsisetfont end } bind put
  78.      userdict /setpagedevice /pop load put
  79.      //systemdict /.setsafe known { .setsafe } if
  80.    } bind def
  81.  
  82.  /epsifontdict 100 dict def
  83.  
  84.  /epsisetfont
  85.  {
  86.  % code here keeps a list of font names in dictionary epsifontdict
  87.  /tmpfont exch def
  88.  tmpfont /FontName known {
  89.    /tmpfontname tmpfont /FontName get def
  90.    epsifontdict tmpfontname known not { epsifontdict tmpfontname 0 put } if
  91.    epsifontdict tmpfontname 2 copy get 1 add put
  92.  } if
  93.  tmpfont setfont
  94.  } bind def
  95.  
  96. % Get a scan line from the memory device, zeroing any bits beyond
  97. % the device width.
  98. /getscanline {        % <device> <y> <string> getscanline <string>
  99.   dup 4 1 roll copyscanlines pop
  100.   16#ff00 devwidth 7 and neg bitshift 255 and
  101.   dup 0 ne {
  102.     1 index dup length 1 sub 2 copy get 4 -1 roll and put
  103.   } {
  104.     pop
  105.   } ifelse
  106. } bind def
  107.  
  108. /margintest {        % <y-start> <step> <y-limit> margintest <y-non-blank>
  109.             % <y-start> <step> <y-limit> margintest -
  110.   { dup arraydevice exch row getscanline
  111.     zerorow ne { exit } if pop
  112.   } for
  113. } bind def
  114.  
  115.  
  116.   /epsiNameStr 200 string def
  117.   /epsiNpages 0 def
  118.   /epsiNpageStr 20 string def
  119.   /epsipage
  120.    {      
  121.      /epsiNpages epsiNpages 1 add def
  122.      /loopcount devheight 1 sub def
  123.  
  124.      % Find top margin -- minimum Y of non-blank scan line.
  125.      -1 0 1 loopcount margintest
  126.      dup -1 eq { (blank page!!\n) print quit }{ exch pop } ifelse 
  127.      /tm exch def
  128.  
  129.      % Find bottom margin -- maximum Y of non-blank scan line.
  130.      loopcount -1 0 margintest
  131.      /bm exch def
  132.      
  133.      % Initialise limit variables
  134.      /loopcount rowwidth 1 sub def
  135.      /lm loopcount def 
  136.      /rm 0 def 
  137.  
  138.      % Find left and right boundaries of image
  139.      tm 1 bm
  140.       { % Get more data
  141.     arraydevice exch row getscanline pop
  142.     % Scan from left to find first non-zero element
  143.     % We save first the element, then the index
  144.     -1 0 1 loopcount
  145.     { dup row exch get 0 ne { exch pop exit }{ pop } ifelse
  146.     } for
  147.     % If we found -1, row is blank ..
  148.     dup -1 ne 
  149.     { % Find the leftmost index
  150.           dup lm lt
  151.           % If the new index is less, we save index and element
  152.           { /lm exch def } { pop } ifelse
  153.       % Now find the rightmost index
  154.       loopcount -1 0
  155.           { dup row exch get 0 ne { exit }{ pop } ifelse
  156.           } for
  157.       dup rm gt
  158.           % If the new index is greater, we save index and element
  159.           { /rm exch def } { pop } ifelse
  160.     } {
  161.           pop
  162.         } ifelse
  163.       } for
  164.  
  165.      % Calculate the bounding box values.
  166.      % Note that these must be corrected to produce closed-open intervals.
  167.      /llx lm def
  168.      /lly devheight bm sub 1 sub def
  169.      /urx rm 1 add def
  170.      /ury devheight tm sub def
  171.  
  172.     % Write out the magic string and bounding box information
  173.      epsifile (%!PS-Adobe-2.0 EPSF-1.2\n) writestring
  174.      /epsititle where { pop epsifile epsititle writestring } if
  175.      /epsicreator where { pop epsifile epsicreator writestring } if
  176.      /epsicrdt where { pop epsifile epsicrdt writestring } if
  177.      /epsifor where { pop epsifile epsifor writestring } if
  178.      epsifile flushfile
  179.  
  180.     % Write out the page count:
  181.      epsifile (%%Pages: ) writestring
  182.      epsifile epsiNpages epsiNpageStr cvs writestring
  183.      epsifile (\n) writestring
  184.      epsifile flushfile
  185.  
  186.     % Write out the list of used fonts:
  187.      epsifile (%%DocumentFonts:) writestring
  188.      epsifontdict {
  189.                     epsifile ( ) writestring
  190.                     pop epsiNameStr cvs epsifile exch writestring
  191.                     } forall
  192.      epsifile (\n) writestring
  193.      epsifile flushfile
  194.  
  195.      epsifile (%%BoundingBox: ) writestring
  196.      epsifile llx write==only epsifile ( ) writestring
  197.      epsifile lly write==only epsifile ( ) writestring
  198.      epsifile urx write==only epsifile ( ) writestring
  199.      epsifile ury write==
  200.  
  201.     % Define character and bit widths for the output line buffer:
  202.      /cwidth rm lm sub 1 add def
  203.      /out cwidth string def
  204.  
  205.      epsifile (%%EndComments\n\n) writestring
  206.      epsifile (%%BeginProlog\n) writestring
  207.      epsifile (%%BeginPreview: ) writestring
  208.      epsifile cwidth write==only epsifile ( ) writestring
  209.      epsifile bm tm sub 1 add write==only epsifile ( 8 ) writestring
  210.      epsifile bm tm sub 1 add
  211.      cwidth 39 add 40 idiv mul write==
  212.      epsifile flushfile
  213.  
  214.      gsave
  215.  
  216.      tm 1 bm
  217.       { % Get a scan line interval from the array device
  218.     arraydevice exch row copyscanlines lm cwidth getinterval
  219.     % Write out the hex data as 40 bytes per line (82 chars)
  220.     0 40 cwidth
  221.      { epsifile (% ) writestring 
  222.        epsifile exch 2 index exch
  223.        dup cwidth exch sub 40 .min getinterval writehexstring
  224.        epsifile (\n) writestring
  225.      } for
  226.         pop
  227.       } for
  228.  
  229.      epsifile (%%EndImage\n) writestring
  230.      epsifile (%%EndPreview\n) writestring
  231.      epsifile flushfile
  232.      grestore
  233.      erasepage initgraphics
  234.  
  235.      DonePage 0 1 put
  236.    } bind def
  237.  
  238.  
  239. (outfile) getenv
  240.   { /outfile exch def 
  241.     ps2epsi
  242.  
  243.     /DonePage 1 string def
  244.     (%stdin) (r) file cvx execute0
  245.     DonePage 0 get 0 eq { showpage } if
  246.   } if
  247.  
  248. end
  249. quit
  250.