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

  1. %    Copyright (C) 1989, 1996, 2002 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: gs_epsf.ps 8954 2008-08-08 04:22:38Z ray $
  14. % Allow the interpreter to encapsulate EPS files, to recognize MS-DOS 
  15. % EPSF file headers, and skip to the PostScript section of the file.
  16.  
  17. % Encapsulate EPS files and optionally resize page or rescale image.
  18. % To display an EPS file cropped to the bounding box:
  19. %  gs -dEPSCrop file.eps
  20. % To display an EPS file scaled to fit the page:
  21. %  gs -dEPSFitPage file.eps
  22. % To display a file without EPS encapsulation:
  23. %  gs -dNOEPS file.ps
  24.  
  25. % When starting to process an EPS file, state is 0.
  26. % After %%BoundingBox processed, state is 1 if OK or 2 if cropped.
  27. % After %%HiResBoundingBox processed, state is 3 if OK or 4 if cropped.
  28. % After %%EndComments processed, state is 5.
  29. /EPSBoundingBoxState 5 def
  30. /EPSBoundingBoxSetState {
  31.   //systemdict /EPSBoundingBoxState 3 -1 roll .forceput
  32. } .bind odef % .forceput must be bound and hidden
  33.  
  34. % Parse 4 numbers for a bounding box
  35. /EPSBoundingBoxParse { % (llx lly urx ury) -- llx lly urx ury true OR false
  36.     mark exch
  37.     token {exch token {exch token {exch token {exch pop} if} if} if} if
  38.     counttomark
  39.     4 eq {
  40.       5 -1 roll pop % remove mark
  41.       true
  42.     } {
  43.       cleartomark false
  44.     } ifelse
  45. } bind def
  46.  
  47. % Rescale and translate to fit the BoundingBox on the page
  48. /EPSBoundingBoxFitPage { % llx lly urx ury --
  49.   EPSDEBUG { (gs_epsf.ps: Rescaling EPS to fit page\n) print flush } if
  50.   clippath pathbbox newpath
  51.   % translate to new origin at lower left of clippath
  52.   3 index 3 index translate
  53.   % calculate scale to fit smaller of width or height
  54.   exch 4 -1 roll sub 3 1 roll exch sub 
  55.   4 2 roll 5 index 5 index 4 2 roll
  56.   exch 4 -1 roll sub 3 1 roll exch sub 
  57.   4 2 roll
  58.   exch 4 -1 roll div 3 1 roll exch div
  59.   1 index 1 index lt {pop}{exch pop} ifelse
  60.   dup scale
  61.   % translate to EPS -llx,-lly
  62.   exch neg exch neg translate
  63. } bind def
  64.  
  65. % Crop the page to the BoundingBox
  66. /EPSBoundingBoxCrop { % llx lly urx ury --
  67.   EPSDEBUG { 
  68.     (gs_epsf.ps: Setting pagesize from EPS bounding box\n) print flush 
  69.   } if
  70.   exch 3 index sub exch 2 index sub % stack: llx lly urx-llx ury-lly
  71.   << /PageSize [ 5 -2 roll ] >> setpagedevice
  72.   neg exch neg exch translate
  73. } bind def
  74.  
  75.  
  76. /EPSBoundingBoxProcess { % (llx lly urx ury) state --
  77.   //systemdict /EPSBoundingBoxState get 1 index lt {
  78.     exch EPSBoundingBoxParse 
  79.     {
  80.       //systemdict /EPSCrop known {
  81.         EPSBoundingBoxCrop
  82.       } {
  83.         //systemdict /EPSFitPage known {
  84.           EPSBoundingBoxFitPage
  85.     } {
  86.       % Warn if some of the EPS file will be clipped
  87.       clippath pathbbox newpath
  88.       { % context for exit
  89.         5 -1 roll lt { 6 { pop } repeat true exit } if
  90.         4 -1 roll lt { 4 { pop } repeat true exit } if
  91.         3 -1 roll gt { 2 { pop } repeat true exit } if
  92.         exch gt { true exit } if
  93.         false exit
  94.       } loop
  95.       QUIET not and /EPSBoundingBoxState .systemvar 1 and 1 eq and {
  96.         (\n   **** Warning: Some of the BoundingBox for the EPS file will be clipped.) =
  97.         (                 Use -dEPSCrop or -dEPSFitPage to avoid clipping.\n) =
  98.         flush
  99.             1 add
  100.           } if
  101.     } ifelse
  102.       } ifelse
  103.       EPSBoundingBoxSetState
  104.     } {
  105.       pop % state
  106.     } ifelse
  107.   } {
  108.     pop pop
  109.   } ifelse
  110. } bind def
  111.  
  112. /ProcessEPSComment { % file comment --  file comment
  113.   //systemdict /EPSBoundingBoxState get 3 lt {
  114.     dup
  115.     (%%EndComments) anchorsearch {
  116.       pop pop
  117.       % ignore any following bounding boxes
  118.       5 EPSBoundingBoxSetState
  119.     } {
  120.       (%%BoundingBox:) anchorsearch {
  121.     pop 
  122.     EPSDEBUG { (gs_epsf.ps: found %%BoundingBox\n) print flush } if
  123.     1 EPSBoundingBoxProcess
  124.       } {
  125.     (%%HiResBoundingBox:) anchorsearch {
  126.       pop 
  127.       EPSDEBUG { (gs_epsf.ps: found %%HiResBoundingBox\n) print flush } if
  128.     3 EPSBoundingBoxProcess
  129.     } { 
  130.       pop % Not interested in this DSC comment
  131.     } ifelse
  132.       } ifelse
  133.     } ifelse
  134.   } if
  135. } bind def
  136.  
  137. % Install EPS handler for DSC comments, which we do later
  138. /EPSBoundingBoxInit {
  139.   systemdict /NOEPS known not {
  140.     % Merge ProcessEPSComment with existing handler
  141.     /ProcessEPSComment load /exec load
  142.     currentuserparams /ProcessDSCComment get 
  143.     dup null eq {pop {pop pop}} if /exec load
  144.     4 array astore cvx readonly
  145.     << /ProcessDSCComment 3 -1 roll >> setuserparams
  146.   } if
  147. } bind def
  148.  
  149. /.runNoEPS /run load def
  150.  
  151. /.runEPS { % file OR string --
  152.   /runEPS_save save def
  153.   /runEPS_dict_count countdictstack def
  154.   /runEPS_op_count count 2 sub def
  155.   /runEPS_page_count currentpagedevice /PageCount get def 
  156.   0 EPSBoundingBoxSetState
  157.   .runNoEPS
  158.   currentpagedevice /PageCount get runEPS_page_count sub 0 eq 
  159.   { /showpage load exec } if
  160.   count runEPS_op_count sub {pop} repeat
  161.   countdictstack runEPS_dict_count sub {end} repeat
  162.   runEPS_save restore
  163. } bind def
  164.  
  165. /run { % file OR string --
  166.   dup type /filetype ne { (r) file } if
  167.   dup (%!PS-Adobe-) .peekstring {
  168.     (%!PS-Adobe-) eq {
  169.       dup (%!PS-Adobe-X.X EPSF-X.X) .peekstring {
  170.       (EPSF) search {
  171.         pop pop pop
  172.         EPSDEBUG {(runEPS: Found EPS\n) print flush} if
  173.         systemdict /NOEPS known {
  174.           cvx .runNoEPS
  175.         } {
  176.           cvx .runEPS
  177.         } ifelse
  178.       } {
  179.         EPSDEBUG {(runEPS: Normal DSC\n) print flush} if
  180.         pop
  181.           cvx .runNoEPS
  182.  
  183.       } ifelse
  184.       } {
  185.         EPSDEBUG {(runEPS: Short DSC\n) print flush} if
  186.       pop
  187.         cvx .runNoEPS
  188.       } ifelse
  189.     } {
  190.       EPSDEBUG {(runEPS: Not DSC\n) print flush} if
  191.       cvx .runNoEPS
  192.     } ifelse
  193.   } {
  194.     EPSDEBUG {(runEPS: Short non-DSC\n) print flush} if
  195.     pop
  196.     cvx .runNoEPS
  197.   } ifelse
  198. } bind odef
  199.  
  200.  
  201. % Handle DOS EPS files.
  202.  
  203. /.runnoepsf /run load def
  204. /.epsfheader <C5D0D3C6> def
  205. /run
  206.  { dup type /filetype ne { (r) file } if
  207.         % Check for MS-DOS EPSF file (see Red Book p. 729).
  208.  dup (    ) .peekstring 
  209.   { .epsfheader eq { dup (    ) readstring exch pop } { false } ifelse }
  210.   { pop false }
  211.  ifelse
  212.         % Stack: file true/false
  213.     {        % This block is executed if the file is MS-DOS EPSF.
  214.         % Build up the little-endian byte offset and length.
  215.       2
  216.     { 1 0 4
  217.        { 2 index read not { pop exit } if % if EOF, let error happen
  218.          2 index mul add exch 256 mul exch
  219.        }
  220.       repeat exch pop exch
  221.     }
  222.       repeat
  223.         % Stack: offset length file
  224.         % Use flushfile to skip quickly to the start of the
  225.         % PostScript section.
  226.       dup 4 -1 roll 12 sub () /SubFileDecode filter flushfile
  227.         % Now interpret the PostScript.
  228.       exch () /SubFileDecode filter cvx run
  229.     }
  230.     { .runnoepsf
  231.     }
  232.    ifelse
  233.  } odef
  234.  
  235. % rebind .runstdin to use redefined run
  236. userdict begin
  237. /.runstdin {
  238.   { (%stdin) run } execute0
  239. } bind def
  240. end
  241.