| 1 |
% Copyright (C) 2001-2026 Artifex Software, Inc. |
| 2 |
% All Rights Reserved. |
| 3 |
% |
| 4 |
% This software is provided AS-IS with no warranty, either express or |
| 5 |
% implied. |
| 6 |
% |
| 7 |
% This software is distributed under license and may not be copied, |
| 8 |
% modified or distributed except as expressly authorized under the terms |
| 9 |
% of the license contained in the file LICENSE in this distribution. |
| 10 |
% |
| 11 |
% Refer to licensing information at http://www.artifex.com or contact |
| 12 |
% Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
| 13 |
% CA 94129, USA, for further information. |
| 14 |
% |
| 15 |
|
| 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 |
|
| 19 |
% Encapsulate EPS files and optionally resize page or rescale image. |
| 20 |
% To display an EPS file cropped to the bounding box: |
| 21 |
% gs -dEPSCrop file.eps |
| 22 |
% To display an EPS file scaled to fit the page: |
| 23 |
% gs -dEPSFitPage file.eps |
| 24 |
% To display a file without EPS encapsulation: |
| 25 |
% gs -dNOEPS file.ps |
| 26 |
|
| 27 |
% When starting to process an EPS file, state is 0. |
| 28 |
% After %%BoundingBox processed, state is 1 if OK or 2 if cropped. |
| 29 |
% After %%HiResBoundingBox processed, state is 3 if OK or 4 if cropped. |
| 30 |
% After %%EndComments processed, state is 5. |
| 31 |
/EPSBoundingBoxState 5 def |
| 32 |
/EPSBoundingBoxString () def % set if either BoundingBox is seen (even if invalid) |
| 33 |
/EPSBoundingBoxSetState { |
| 34 |
//systemdict /EPSBoundingBoxState 3 -1 roll .forceput |
| 35 |
} .forcebind odef % .forceput must be bound and hidden |
| 36 |
|
| 37 |
% Parse 4 numbers for a bounding box |
| 38 |
/EPSBoundingBoxParse { % (llx lly urx ury) -- llx lly urx ury true OR false |
| 39 |
mark exch |
| 40 |
token {exch token {exch token {exch token {exch pop} if} if} if} if |
| 41 |
counttomark |
| 42 |
4 eq { |
| 43 |
5 -1 roll pop % remove mark |
| 44 |
//true |
| 45 |
} { |
| 46 |
cleartomark //false |
| 47 |
} ifelse |
| 48 |
} .internalbind odef |
| 49 |
|
| 50 |
% Crop the page to the BoundingBox |
| 51 |
/EPSBoundingBoxCrop { % llx lly urx ury -- |
| 52 |
EPSDEBUG { |
| 53 |
(gs_epsf.ps: Setting pagesize from EPS bounding box\n) print flush |
| 54 |
} if |
| 55 |
exch 3 index sub exch 2 index sub % stack: llx lly urx-llx ury-lly |
| 56 |
<< /PageSize [ 5 -2 roll ] >> setpagedevice |
| 57 |
neg exch neg exch translate |
| 58 |
} .internalbind odef |
| 59 |
|
| 60 |
% Rescale, translate and rotate to fit the BoundingBox on the page |
| 61 |
/EPSBoundingBoxFitPage { % llx lly urx ury -- |
| 62 |
EPSDEBUG { (gs_epsf.ps: Rescaling EPS to fit page\n) print flush } if |
| 63 |
clippath pathbbox newpath % ellx elly eurx eury pllx plly purx pury |
| 64 |
EPSDEBUG { |
| 65 |
(Page Coordinates: LLX: ) print 3 index =print (, LLY: ) print |
| 66 |
2 index =print (, URX: ) print 1 index =print (, URY: ) print dup = flush |
| 67 |
} if |
| 68 |
% Convert box corners to coordinates of the center and box sizes |
| 69 |
2 { % loop doing the page coordinates, the the EPS bbox coordinates |
| 70 |
3 -1 roll exch % ... llx urx lly ury |
| 71 |
2 { % loop doing Y then X coordnates |
| 72 |
2 copy exch sub % ... llx urx lly ury ury-lly |
| 73 |
3 1 roll % ... llx urx ury-lly lly ury |
| 74 |
add 2 div % ... llx urx ury-lly (lly+ury)/2 |
| 75 |
4 2 roll % ... ury-lly (lly+ury)/2 llx urx |
| 76 |
} repeat |
| 77 |
8 4 roll |
| 78 |
} repeat |
| 79 |
% edx, edy = EPS dimension X and Y, ecx, ecy = EPS Center X and Y. |
| 80 |
% pdx and pcx, etc, are for the Page values. |
| 81 |
% edx ecx edy ecy pdx pcx pdy pcy |
| 82 |
|
| 83 |
% Move the origin to the center of the printable area. |
| 84 |
3 -1 roll exch % edx ecx edy ecy pdx pdy pcx pcy |
| 85 |
translate % edx ecx edy ecy pdx pdy |
| 86 |
|
| 87 |
% Find orientation of the best fit. Square pages or files don't rotate. |
| 88 |
2 copy sub % edx ecx edy ecy pdx pdy pdx-pdy |
| 89 |
EPSDEBUG { |
| 90 |
(pdx: ) print 2 index =print (, pdy: ) print 1 index =print |
| 91 |
(, pdx-pdy: ) print dup = flush |
| 92 |
} if |
| 93 |
6 index 5 index sub |
| 94 |
EPSDEBUG { |
| 95 |
(edx: ) print 7 index =print (, edy: ) print 5 index =print |
| 96 |
(, edx-edy: ) print dup = flush |
| 97 |
} if |
| 98 |
mul % edx ecx edy ecy pdx pdy (pdx-pdy)*(edx-edy) |
| 99 |
EPSDEBUG { |
| 100 |
(product: ) print dup = flush |
| 101 |
} if |
| 102 |
0 lt { |
| 103 |
90 rotate |
| 104 |
exch |
| 105 |
} if |
| 106 |
|
| 107 |
% Scale to fit in the most restricting direction. |
| 108 |
4 -1 roll div % edx ecx ecy pdx pdy/edy |
| 109 |
exch 5 -1 roll div % ecx ecy pdy/edy pdx/edx |
| 110 |
//.min exec |
| 111 |
dup scale % ecx ecy |
| 112 |
|
| 113 |
% Center the document |
| 114 |
neg exch neg exch translate |
| 115 |
|
| 116 |
} .internalbind odef |
| 117 |
|
| 118 |
/EPSBoundingBoxProcess { % (llx lly urx ury) state -- |
| 119 |
% The following 'lt' check prioritzies HiResBoundingBox over BoundingBox |
| 120 |
% even if HiResBoundingBox occurs first in the EPS file. |
| 121 |
//systemdict /EPSBoundingBoxState get 1 index lt { |
| 122 |
% save the BBoxString for possible FitPage when EndComments is seen |
| 123 |
exch dup currentglobal //true setglobal exch |
| 124 |
dup length string copy //systemdict /EPSBoundingBoxString 3 -1 roll .forceput |
| 125 |
setglobal |
| 126 |
EPSBoundingBoxParse |
| 127 |
{ |
| 128 |
//systemdict /EPSCrop known { |
| 129 |
EPSBoundingBoxCrop |
| 130 |
} { |
| 131 |
//systemdict /EPSFitPage known not { |
| 132 |
% Warn if some of the EPS file will be clipped |
| 133 |
clippath pathbbox newpath |
| 134 |
{ % context for exit |
| 135 |
5 -1 roll lt { 6 { pop } repeat //true exit } if |
| 136 |
4 -1 roll lt { 4 { pop } repeat //true exit } if |
| 137 |
3 -1 roll gt { 2 { pop } repeat //true exit } if |
| 138 |
exch gt { //true exit } if |
| 139 |
//false exit |
| 140 |
} loop |
| 141 |
QUIET not and /EPSBoundingBoxState .systemvar 1 and 1 eq and { |
| 142 |
(\n **** Warning: Some of the BoundingBox for the EPS file will be clipped.) = |
| 143 |
( Use -dEPSCrop or -dEPSFitPage to avoid clipping.\n) = |
| 144 |
flush |
| 145 |
1 add |
| 146 |
} if |
| 147 |
} { |
| 148 |
pop pop pop pop |
| 149 |
} ifelse |
| 150 |
} ifelse |
| 151 |
EPSBoundingBoxSetState |
| 152 |
} { |
| 153 |
% improperly formed BoundingBox string. |
| 154 |
QUIET not { |
| 155 |
(\n **** Warning: BoundingBox values are invalid and will be ignored: ') print |
| 156 |
//systemdict /EPSBoundingBoxString get |
| 157 |
print (') = flush |
| 158 |
} if |
| 159 |
pop % state |
| 160 |
} ifelse |
| 161 |
} { |
| 162 |
pop pop |
| 163 |
} ifelse |
| 164 |
} .internalbind odef |
| 165 |
|
| 166 |
% Perform anchorsearch on the strings in the array until a match is found. |
| 167 |
/anchorsearchforany { % haystack [needle1 ...] --> post needle true |
| 168 |
% --> haystack false |
| 169 |
false 3 1 roll % false haystack [...] |
| 170 |
{ % false haystack needle |
| 171 |
dup 3 1 roll % false needle haystack needle |
| 172 |
anchorsearch { |
| 173 |
% false needle post needle |
| 174 |
pop % false needle post |
| 175 |
3 1 roll % post false needle |
| 176 |
exch not exch % post true needle |
| 177 |
exit |
| 178 |
} { |
| 179 |
% false needle haystack |
| 180 |
exch pop % false haystack |
| 181 |
} ifelse |
| 182 |
} forall |
| 183 |
exch % haystack false | post needle true |
| 184 |
} .internalbind def |
| 185 |
|
| 186 |
/ProcessEPSComment { % file comment -- file comment |
| 187 |
/EPSBoundingBoxState .systemvar 3 lt { |
| 188 |
dup |
| 189 |
(%%BoundingBox:) anchorsearch { |
| 190 |
pop |
| 191 |
EPSDEBUG { (gs_epsf.ps: found %%BoundingBox\n) print flush } if |
| 192 |
1 EPSBoundingBoxProcess |
| 193 |
} { |
| 194 |
(%%HiResBoundingBox:) anchorsearch { |
| 195 |
pop |
| 196 |
EPSDEBUG { (gs_epsf.ps: found %%HiResBoundingBox\n) print flush } if |
| 197 |
3 EPSBoundingBoxProcess |
| 198 |
} { |
| 199 |
pop % Not interested in this DSC comment |
| 200 |
} ifelse |
| 201 |
} ifelse |
| 202 |
} if |
| 203 |
/EPSBoundingBoxState .systemvar 5 lt { |
| 204 |
dup (%%DocumentCustomColors:) anchorsearch { |
| 205 |
pop |
| 206 |
[ exch |
| 207 |
{ { |
| 208 |
token not { exit } if |
| 209 |
dup type /stringtype ne { stop } if |
| 210 |
dup (atend) eq { stop } if |
| 211 |
exch |
| 212 |
} loop |
| 213 |
{ |
| 214 |
counttomark 2 add index |
| 215 |
dup (123) .peekstring not { stop } if |
| 216 |
(%%+) eq { |
| 217 |
dup (123) readstring pop pop |
| 218 |
256 string readline pop |
| 219 |
cvx exec |
| 220 |
} { |
| 221 |
pop exit |
| 222 |
} ifelse |
| 223 |
} loop |
| 224 |
} stopped { |
| 225 |
cleartomark |
| 226 |
} { |
| 227 |
counttomark 0 gt { |
| 228 |
currentpagedevice /MaxSeparations get 4 gt { |
| 229 |
] << /SeparationColorNames 3 -1 roll >> setpagedevice |
| 230 |
} { |
| 231 |
cleartomark |
| 232 |
} ifelse |
| 233 |
} { |
| 234 |
cleartomark |
| 235 |
} ifelse |
| 236 |
} ifelse |
| 237 |
} { |
| 238 |
{(%%EndComments) (%%BeginProlog) (%%BeginSetup)} anchorsearchforany { |
| 239 |
EPSDEBUG { (EPSComment processing finished, encountered: ) print dup = } if |
| 240 |
pop pop % discard the strings from the anchorsearch |
| 241 |
% We may have seen BoundingBox or HiResBounfingBox. If so and if EPSFitPage |
| 242 |
% is set, then we do the transformation here to scale and center the page, |
| 243 |
% rotating if needed (and AllowFitPageRotation is true -- the default.) |
| 244 |
//systemdict /EPSFitPage known |
| 245 |
//systemdict /EPSBoundingBoxState get 0 gt |
| 246 |
and { |
| 247 |
EPSBoundingBoxString EPSBoundingBoxParse { |
| 248 |
EPSBoundingBoxFitPage |
| 249 |
} if |
| 250 |
} if |
| 251 |
% Ignore any following comments |
| 252 |
5 EPSBoundingBoxSetState |
| 253 |
} { |
| 254 |
pop % Not %%EndComments -- ignore it |
| 255 |
} ifelse |
| 256 |
} ifelse |
| 257 |
} if |
| 258 |
} .internalbind def |
| 259 |
|
| 260 |
% Install EPS handler for DSC comments, which we do later |
| 261 |
/EPSBoundingBoxInit { |
| 262 |
systemdict /NOEPS known not { |
| 263 |
% Merge ProcessEPSComment with existing handler |
| 264 |
//ProcessEPSComment /exec load |
| 265 |
currentuserparams /ProcessDSCComment get |
| 266 |
dup //null eq {pop {pop pop}} if /exec load |
| 267 |
4 array astore cvx readonly |
| 268 |
<< /ProcessDSCComment 3 -1 roll >> setuserparams |
| 269 |
} if |
| 270 |
} .internalbind odef |
| 271 |
|
| 272 |
|
| 273 |
/.runNoEPS /run load def |
| 274 |
|
| 275 |
/.runEPS { % file OR string -- |
| 276 |
.updatematrices |
| 277 |
/runEPS_save save def |
| 278 |
/runEPS_dict_count countdictstack def |
| 279 |
/runEPS_op_count count 2 sub def |
| 280 |
/runEPS_page_count .currentshowpagecount not {0} if def |
| 281 |
0 EPSBoundingBoxSetState |
| 282 |
//.runNoEPS |
| 283 |
.currentshowpagecount not {0} if runEPS_page_count sub 0 eq |
| 284 |
{ /showpage load exec } if |
| 285 |
count runEPS_op_count sub {pop} repeat |
| 286 |
countdictstack runEPS_dict_count sub {end} repeat |
| 287 |
runEPS_save restore |
| 288 |
} .internalbind odef |
| 289 |
|
| 290 |
/run { % file OR string -- |
| 291 |
dup type /filetype ne { (r) file } if |
| 292 |
dup (%!PS-Adobe-) .peekstring { |
| 293 |
(%!PS-Adobe-) eq { |
| 294 |
dup (%!PS-Adobe-X.X EPSF-X.X) .peekstring { |
| 295 |
(EPSF) search { |
| 296 |
pop pop pop |
| 297 |
EPSDEBUG {(runEPS: Found EPS\n) print flush} if |
| 298 |
systemdict /NOEPS known { |
| 299 |
cvx //.runNoEPS |
| 300 |
} { |
| 301 |
cvx .runEPS |
| 302 |
} ifelse |
| 303 |
} { |
| 304 |
EPSDEBUG {(runEPS: Normal DSC\n) print flush} if |
| 305 |
pop |
| 306 |
cvx //.runNoEPS |
| 307 |
|
| 308 |
} ifelse |
| 309 |
} { |
| 310 |
EPSDEBUG {(runEPS: Short DSC\n) print flush} if |
| 311 |
pop |
| 312 |
cvx //.runNoEPS |
| 313 |
} ifelse |
| 314 |
} { |
| 315 |
EPSDEBUG {(runEPS: Not DSC\n) print flush} if |
| 316 |
cvx //.runNoEPS |
| 317 |
} ifelse |
| 318 |
} { |
| 319 |
EPSDEBUG {(runEPS: Short non-DSC\n) print flush} if |
| 320 |
pop |
| 321 |
cvx //.runNoEPS |
| 322 |
} ifelse |
| 323 |
} .internalbind odef |
| 324 |
|
| 325 |
% Handle DOS EPS files. |
| 326 |
|
| 327 |
/.runnoepsf /run load def |
| 328 |
/.epsfheader <C5D0D3C6> def |
| 329 |
/run |
| 330 |
{ dup type /filetype ne { (r) file } if |
| 331 |
% Check for MS-DOS EPSF file (see Red Book p. 729). |
| 332 |
dup ( ) .peekstring |
| 333 |
{ .epsfheader eq { dup ( ) readstring exch pop } { //false } ifelse } |
| 334 |
{ pop //false } |
| 335 |
ifelse |
| 336 |
% Stack: file true/false |
| 337 |
{ % This block is executed if the file is MS-DOS EPSF. |
| 338 |
% Build up the little-endian byte offset and length. |
| 339 |
2 |
| 340 |
{ 1 0 4 |
| 341 |
{ 2 index read not { pop exit } if % if EOF, let error happen |
| 342 |
2 index mul add exch 256 mul exch |
| 343 |
} |
| 344 |
repeat exch pop exch |
| 345 |
} |
| 346 |
repeat |
| 347 |
% Stack: offset length file |
| 348 |
% Use flushfile to skip quickly to the start of the |
| 349 |
% PostScript section. |
| 350 |
dup 4 -1 roll 12 sub () /SubFileDecode filter flushfile |
| 351 |
% Now interpret the PostScript. |
| 352 |
exch () /SubFileDecode filter cvx run |
| 353 |
} |
| 354 |
{ //.runnoepsf |
| 355 |
} |
| 356 |
ifelse |
| 357 |
} odef |
| 358 |
|
| 359 |
% rebind .runstdin to use redefined run |
| 360 |
userdict begin |
| 361 |
/.runstdin { |
| 362 |
{ (%stdin) run } execute0 |
| 363 |
} .internalbind def |
| 364 |
end |
| 365 |
|
| 366 |
% undefine things defined in this file and not referenced elsewhere |
| 367 |
[ |
| 368 |
/.runNoEPS |
| 369 |
/.runnoepsf |
| 370 |
/.runEPS |
| 371 |
/EPSBoundingBoxSetState |
| 372 |
/EPSBoundingBoxString |
| 373 |
/EPSBoundingBoxCrop |
| 374 |
/EPSBoundingBoxFitPage |
| 375 |
/EPSBoundingBoxParse |
| 376 |
/EPSBoundingBoxProcess |
| 377 |
/ProcessEPSComment |
| 378 |
] currentdict .undefinternalnames |