1
0

gs_epsf.ps 7.1 KB

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