home *** CD-ROM | disk | FTP | other *** search
/ jppd.dyndns.org / jppd.dyndns.org.tar / jppd.dyndns.org / QUERYPRO / Actualizar / Impressora_PDF / converter.exe / GPLGS / gs_epsf.ps < prev    next >
Text File  |  2003-04-13  |  7KB  |  243 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,v 1.14 2003/04/12 18:08:18 ray Exp $
  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.
  27. % After %%HiResBoundingBox processed, state is 2.
  28. % After %%EndComments processed, state is 3.
  29. /EPSBoundingBoxState 3 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 {
  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.       } if
  100.     } ifelse
  101.       } ifelse
  102.       EPSBoundingBoxSetState
  103.     } {
  104.       pop % state
  105.     } ifelse
  106.   } {
  107.     pop pop
  108.   } ifelse
  109. } bind def
  110.  
  111. /ProcessEPSComment { % file comment --  file comment
  112.   //systemdict /EPSBoundingBoxState get 3 lt {
  113.     dup
  114.     (%%EndComments) anchorsearch {
  115.       pop pop
  116.       % ignore any following bounding boxes
  117.       3 EPSBoundingBoxSetState
  118.     } {
  119.       (%%BoundingBox:) anchorsearch {
  120.     pop 
  121.     EPSDEBUG { (gs_epsf.ps: found %%BoundingBox\n) print flush } if
  122.     1 EPSBoundingBoxProcess
  123.       } {
  124.     (%%HiResBoundingBox:) anchorsearch {
  125.       pop 
  126.       EPSDEBUG { (gs_epsf.ps: found %%HiResBoundingBox\n) print flush } if
  127.     2 EPSBoundingBoxProcess
  128.     } { 
  129.       pop % Not interested in this DSC comment
  130.     } ifelse
  131.       } ifelse
  132.     } ifelse
  133.   } if
  134. } bind def
  135.  
  136. % Install EPS handler for DSC comments, which we do later
  137. /EPSBoundingBoxInit {
  138.   systemdict /NOEPS known not {
  139.     % Merge ProcessEPSComment with existing handler
  140.     /ProcessEPSComment load /exec load
  141.     currentuserparams /ProcessDSCComment get 
  142.     dup null eq {pop {pop pop}} if /exec load
  143.     4 array astore cvx readonly
  144.     << /ProcessDSCComment 3 -1 roll >> setuserparams
  145.   } if
  146. } bind def
  147.  
  148. /.runNoEPS /run load def
  149.  
  150. /.runEPS { % file OR string --
  151.   /runEPS_save save def
  152.   /runEPS_dict_count countdictstack def
  153.   /runEPS_op_count count 2 sub def
  154.   /runEPS_page_count currentpagedevice /PageCount get def 
  155.   0 EPSBoundingBoxSetState
  156.   .runNoEPS
  157.   currentpagedevice /PageCount get runEPS_page_count sub 0 eq 
  158.   { /showpage load exec } if
  159.   count runEPS_op_count sub {pop} repeat
  160.   countdictstack runEPS_dict_count sub {end} repeat
  161.   runEPS_save restore
  162. } bind def
  163.  
  164. /run { % file OR string --
  165.   dup type /filetype ne { (r) file } if
  166.   dup (%!PS-Adobe-) .peekstring {
  167.     (%!PS-Adobe-) eq {
  168.       dup (%!PS-Adobe-X.X EPSF-X.X) .peekstring {
  169.       (EPSF) search {
  170.         pop pop pop
  171.         EPSDEBUG {(runEPS: Found EPS\n) print flush} if
  172.         systemdict /NOEPS known {
  173.           cvx .runNoEPS
  174.         } {
  175.           cvx .runEPS
  176.         } ifelse
  177.       } {
  178.         EPSDEBUG {(runEPS: Normal DSC\n) print flush} if
  179.         pop
  180.           cvx .runNoEPS
  181.  
  182.       } ifelse
  183.       } {
  184.         EPSDEBUG {(runEPS: Short DSC\n) print flush} if
  185.       pop
  186.         cvx .runNoEPS
  187.       } ifelse
  188.     } {
  189.       EPSDEBUG {(runEPS: Not DSC\n) print flush} if
  190.       cvx .runNoEPS
  191.     } ifelse
  192.   } {
  193.     EPSDEBUG {(runEPS: Short non-DSC\n) print flush} if
  194.     pop
  195.     cvx .runNoEPS
  196.   } ifelse
  197. } bind odef
  198.  
  199.  
  200. % Handle DOS EPS files.
  201.  
  202. /.runnoepsf /run load def
  203. /.epsfheader <C5D0D3C6> def
  204. /run
  205.  { dup type /filetype ne { (r) file } if
  206.         % Check for MS-DOS EPSF file (see Red Book p. 729).
  207.  dup (    ) .peekstring 
  208.   { .epsfheader eq { dup (    ) readstring exch pop } { false } ifelse }
  209.   { pop false }
  210.  ifelse
  211.         % Stack: file true/false
  212.     {        % This block is executed if the file is MS-DOS EPSF.
  213.         % Build up the little-endian byte offset and length.
  214.       2
  215.     { 1 0 4
  216.        { 2 index read not { pop exit } if % if EOF, let error happen
  217.          2 index mul add exch 256 mul exch
  218.        }
  219.       repeat exch pop exch
  220.     }
  221.       repeat
  222.         % Stack: offset length file
  223.         % Use flushfile to skip quickly to the start of the
  224.         % PostScript section.
  225.       dup 4 -1 roll 12 sub () /SubFileDecode filter flushfile
  226.         % Now interpret the PostScript.
  227.       exch () /SubFileDecode filter cvx run
  228.     }
  229.     { .runnoepsf
  230.     }
  231.    ifelse
  232.  } odef
  233.  
  234. % rebind .runstdin to use redefined run
  235. userdict begin
  236. /.runstdin {
  237.   { (%stdin) run } execute0
  238. } bind def
  239. end
  240.