| 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 |
% pdf_main.ps |
| 17 |
% PDF file- and page-level operations. |
| 18 |
|
| 19 |
/.setlanguagelevel where { pop 2 .setlanguagelevel } if |
| 20 |
.currentglobal //true .setglobal |
| 21 |
|
| 22 |
% ======================== Main program ======================== % |
| 23 |
|
| 24 |
userdict begin |
| 25 |
|
| 26 |
% Redefine 'run' so it recognizes PDF files. |
| 27 |
systemdict begin |
| 28 |
|
| 29 |
systemdict /OLDPDF known { |
| 30 |
systemdict /OLDPDF get { |
| 31 |
( **** WARNING ****\n) print |
| 32 |
(The old, written in PostScript, PDF interpreter has been removed entirely.\n) print |
| 33 |
(You should cease using -dOLDPF as it has no effect now.\n) print |
| 34 |
(Continuing to process PDF file using the new, written in C, PDF interpreter.\n\n) print |
| 35 |
} if |
| 36 |
} if |
| 37 |
|
| 38 |
systemdict /NEWPDF known { |
| 39 |
systemdict /NEWPDF get not { |
| 40 |
(The old, written in PostScript, PDF interpreter has been removed entirely.\n) print |
| 41 |
(You should cease using -dNEWDPF as it has no effect now.\n) print |
| 42 |
(Continuing to process PDF file using the new, written in C, PDF interpreter.\n) print |
| 43 |
} if |
| 44 |
} if |
| 45 |
|
| 46 |
|
| 47 |
% PostScript interface to the ghostpdf C-based interpreter |
| 48 |
% |
| 49 |
% Promised or documented as used by customers |
| 50 |
% =========================================== |
| 51 |
% runpdf <file> runpdf - |
| 52 |
% Called from the modified run operator (which copies stdin to a temp |
| 53 |
% file if required). Calls runpdfbegin, check for PDF collections. Then |
| 54 |
% calls process_trailer_attrs, runpdfpagerange, dopdfpages and runpdfend |
| 55 |
% to process all the required pages. |
| 56 |
% |
| 57 |
% runpdfbegin <file> runpdfbegin - |
| 58 |
% creates /pdfdict in userdict set userparmas (ProcessDSCComment |
| 59 |
% setobjectformat to 0 /Page# /Page to null DSCPageCount to 0 |
| 60 |
% /PDFSave /null. |
| 61 |
% calls pdfopen and then 'begin's the returned dictionary. |
| 62 |
% Also disables page handler device. |
| 63 |
% |
| 64 |
% pdfgetpage <int> pdfgetpage <pagedict> | <null> |
| 65 |
% int is a number from 1 to N indicating the desired page number from |
| 66 |
% the PDF file. Returns a dictionary, apparently the /Page dictionary. Probably |
| 67 |
% we only need the Boxes and Rotate key ? If this fails, returns a null object. |
| 68 |
% |
| 69 |
% pdfshowpage_init <int> pdfshowpage_init <dict> |
| 70 |
% This simply adds 1 to the /DSCPageCount value in the supplied dictioanry |
| 71 |
% |
| 72 |
% pdfshowpage_setpage <pagedict> pdfshowpage_setpage <pagedict> |
| 73 |
% Takes a dictionary as returned from pdfgetpage, extracts various |
| 74 |
% parameters from it, and sets the media size for the page, taking into |
| 75 |
% account the boxes, and requested Box, Rotate value and PDFFitPage. |
| 76 |
% |
| 77 |
% pdfshowpage_finish <pagedict> pdfshowpage_finish - |
| 78 |
% Takes a dictionary as returned from pdfgetpage, renders the page content |
| 79 |
% executes showpage to transfer the rendered content to the device. |
| 80 |
% |
| 81 |
% runpdfend - |
| 82 |
% Uses currentdict as an argument to pdfclose, which closes /PDFfile. Also |
| 83 |
% executes restore and various cleanup activities. |
| 84 |
% |
| 85 |
% pdfavailable - pdfavailable <bool> |
| 86 |
% Determines if there is a PDF interpreter available |
| 87 |
% |
| 88 |
% Also Used by gsview 5 |
| 89 |
% ===================== |
| 90 |
% pdfopen <file> pdfopen <dict> |
| 91 |
% According to the comments; open a PDF file and read the header, trailer |
| 92 |
% and cross-reference. Calls pdfopenfile and 'begin's the returned dictionary |
| 93 |
% calls pdfopencache and then closes the current dictionary. |
| 94 |
% pdfopenfile appears to create a dictionary containing a load of stuff but |
| 95 |
% most importantly, sets /PDFfile to the passed-in file. Probably also contains |
| 96 |
% the Trailer dictionary. |
| 97 |
% |
| 98 |
% pdfclose <dict> pdfclose - |
| 99 |
% The supplied dictionary contains /PDFfile and it executes closefile on that |
| 100 |
% supplied file. |
| 101 |
% |
| 102 |
% Supplied by Ray to customer |
| 103 |
% ============================ |
| 104 |
% pdfshowpage <pagedict> pdfshowpage |
| 105 |
% Takes a dictionary returned from pdfgetpage and calls the pdfshowpage_init |
| 106 |
% pdfshowpage_setpage, pdfshowpage_finish trio to start the page, set up the |
| 107 |
% media and render the page. |
| 108 |
% |
| 109 |
% dopdfpages <int> <int> dopdfpages - |
| 110 |
% The integers are the first and last pages to be run from the file. Runs a loop from |
| 111 |
% the fist integer to the last. NOTE! If the current dictionary contains a PDFPageList |
| 112 |
% array then we 'get' the entry from the array corresponding to the current loop |
| 113 |
% index, and use that to determine whether we should draw the page. Otherwise we |
| 114 |
% simply draw the page. Uses pdfshowpage to actually render the page. |
| 115 |
% |
| 116 |
% Additionallly useful ? |
| 117 |
% ======================= |
| 118 |
% runpdfpagerange - runpdfpagerange <int> <int> |
| 119 |
% Processes the PostScript /FirstPage, /LastPage and /PageList parameters to build an array |
| 120 |
% of page numbers to run (if PageList is present) and a FirstPage and LastPage value. |
| 121 |
% There seems no point in rewriting this, may as well use it to control page processing. |
| 122 |
% |
| 123 |
% Normal operation |
| 124 |
% ================= |
| 125 |
% runpdf - runpdfbegin - pdfopen |
| 126 |
% - process_trailer_attrs |
| 127 |
% - runpdfpagerange |
| 128 |
% - dopdfpages - pdfgetpage |
| 129 |
% - pdfshowpage - pdfshowpage_init |
| 130 |
% - pdfshowpage_setpage |
| 131 |
% - pdfshowpage_finish |
| 132 |
% - runpdfend - pdfclose |
| 133 |
% |
| 134 |
|
| 135 |
/DisablePageHandlerDevice |
| 136 |
{ |
| 137 |
systemdict /FirstPage known |
| 138 |
systemdict /LastPage known or |
| 139 |
systemdict /PageList known or |
| 140 |
{ |
| 141 |
<</DisablePageHandler //true>> setpagedevice |
| 142 |
} if |
| 143 |
} .internalbind def |
| 144 |
|
| 145 |
/EnablePageHandlerDevice |
| 146 |
{ |
| 147 |
systemdict /FirstPage known |
| 148 |
systemdict /LastPage known or |
| 149 |
systemdict /PageList known or |
| 150 |
{ |
| 151 |
<</DisablePageHandler //false>> setpagedevice |
| 152 |
} if |
| 153 |
} .internalbind def |
| 154 |
|
| 155 |
/newpdf_pdfformaterror { % <string> pdfformaterror - |
| 156 |
(%stdout) (w) file |
| 157 |
dup 3 -1 roll writestring flushfile |
| 158 |
} .internalbind def |
| 159 |
|
| 160 |
% This routine sets up the transparency compositor requirement, and |
| 161 |
% number of spot channels required (if any) in the device. This needs |
| 162 |
% to be done before we set the media size, so that the erasepage is |
| 163 |
% correct. Called with the dictionary returned from .PDFPageInfo |
| 164 |
/newpdf_device_setup |
| 165 |
{ |
| 166 |
2 dict exch % <<page dict>> << >> |
| 167 |
dup /UsesTransparency get % << >> <<page dict>> bool |
| 168 |
/PageUsesTransparency exch % << >> <<page dict>> /PageUsesTransparency bool |
| 169 |
3 index 3 1 roll % << >> <<page dict>> <<info dict>> << >> /PageUsesTransparency bool |
| 170 |
put % <</PageUsesTransparency bool>> <<page dict>> |
| 171 |
currentpagedevice /PageSpotColors known { |
| 172 |
/NumSpots get % <</PageUsesTransparency bool>> int |
| 173 |
/PageSpotColors exch % <</PageUsesTransparency bool>> /PageSpotColors int |
| 174 |
2 index 3 1 roll % <</PageUsesTransparency bool>> <<page dict>> /PageSpotColors int |
| 175 |
put % <</PageUsesTransparency bool /PageSpotColors int >> |
| 176 |
}{ |
| 177 |
pop |
| 178 |
} ifelse |
| 179 |
setpagedevice |
| 180 |
}.internalbind def |
| 181 |
|
| 182 |
% Takes the selected Box as a parameter and scales it to fit the current media. |
| 183 |
% If the amount of scaling would be reduced by rotating the content, then |
| 184 |
% rotate the content. |
| 185 |
% Caters for the box non-zero lower left x and y (ie cropping) |
| 186 |
/NewPDF_FitPage |
| 187 |
{ |
| 188 |
dup 2 get 1 index 0 get sub %urx -llx = width |
| 189 |
1 index 3 get 2 index 1 get sub %ury - lly = height |
| 190 |
|
| 191 |
currentpagedevice /PageSize get aload pop % Stack - box boxwidth boxheight mediawidth mediaheight |
| 192 |
|
| 193 |
% Start by deciding if we should rotate the page to better fit the media. |
| 194 |
4 copy eq |
| 195 |
3 1 roll eq |
| 196 |
or % square media or square page, no point in rotating |
| 197 |
{ |
| 198 |
pop pop pop pop |
| 199 |
//false |
| 200 |
} |
| 201 |
{ |
| 202 |
gt |
| 203 |
{ % landscape media |
| 204 |
ge |
| 205 |
{ % landscape media, landscape box, no rotation |
| 206 |
//false |
| 207 |
} |
| 208 |
{ % landscape media, portrait box, rotate it to landscape |
| 209 |
//true |
| 210 |
} ifelse |
| 211 |
} |
| 212 |
{ % portrait media |
| 213 |
ge |
| 214 |
{ % portrait media, landscape box, rotate it to portrait |
| 215 |
//true |
| 216 |
} |
| 217 |
{ % portrait media, portrait box, no rotation |
| 218 |
//false |
| 219 |
} ifelse |
| 220 |
}ifelse |
| 221 |
} ifelse |
| 222 |
|
| 223 |
% Store values for calculating scale, we do this here because we want to |
| 224 |
% swap the media width and height (for the scale calculation) if we are |
| 225 |
% rotating the page |
| 226 |
1 index 2 get 2 index 0 get sub % urx -llx = width |
| 227 |
2 index 3 get 3 index 1 get sub % ury - lly = height |
| 228 |
currentpagedevice /PageSize get aload pop % Stack - box rotate boxwidth boxheight mediawidth mediaheight |
| 229 |
5 -1 roll % Stack - box boxwidth boxheight mediawidth mediaheight rotate |
| 230 |
|
| 231 |
% If we need to rotate, recalculate the box |
| 232 |
{ |
| 233 |
% We take any existing /Rotate key in the dictionary into account as well, because |
| 234 |
% that way we will preserve the orientation which would be used when displaying |
| 235 |
% the original, un-modified page. Note that PostScript's rotate operator works |
| 236 |
% counter-clockwise, while PDF works clockwise.... |
| 237 |
% |
| 238 |
7 index dup /Rotate known {/Rotate get}{pop 90}ifelse |
| 239 |
270 eq { |
| 240 |
90 rotate |
| 241 |
0 2 index neg translate % move 0,0 to bottom right of media to account for rotation |
| 242 |
exch % swap media width/height for scaling calculation |
| 243 |
} |
| 244 |
{ |
| 245 |
270 rotate |
| 246 |
dup neg 0 translate % move 0,0 to top right of media to account for rotation |
| 247 |
exch % swap media width/height for scaling calculation |
| 248 |
} ifelse |
| 249 |
}{ |
| 250 |
% If we are not rotating the content to better fit the media, then the only existing Rotate |
| 251 |
% value we need to care about is a 180 rotation, because anything else will cause the content |
| 252 |
% not to fit the media. |
| 253 |
7 index dup /Rotate known {/Rotate get}{pop 0}ifelse |
| 254 |
180 eq { |
| 255 |
180 rotate |
| 256 |
2 copy neg exch neg exch translate % move 0,0 to top right of media to account for rotation |
| 257 |
} if |
| 258 |
}ifelse |
| 259 |
|
| 260 |
% Now use the box and media values to calculate the required x/y scale factors |
| 261 |
4 copy % Stack - box boxwidth boxheight mediawidth mediaheight boxwidth boxheight mediawidth mediaheight |
| 262 |
3 -1 roll div % box boxwidth boxheight mediawidth mediaheight boxwidth mediawidth (mediaheight / boxheight) |
| 263 |
3 1 roll % box boxwidth boxheight mediawidth mediaheight (mediaheight / boxheight) boxwidth mediawidth |
| 264 |
exch % box boxwidth boxheight mediawidth mediaheight (mediaheight / boxheight) mediawidth boxwidth |
| 265 |
div % box boxwidth boxheight mediawidth mediaheight (mediaheight / boxheight) (mediawidth / boxwidth) |
| 266 |
|
| 267 |
% Stack - box boxwidth boxheight mediawidth mediaheight Yscale Xscale |
| 268 |
|
| 269 |
% Centre the output on the media |
| 270 |
2 copy % box boxwidth boxheight mediawidth mediaheight Yscale Xscale Yscale Xscale |
| 271 |
gt { |
| 272 |
exch pop % box boxwidth boxheight mediawidth mediaheight Xscale |
| 273 |
dup 4 index mul % box boxwidth boxheight mediawidth mediaheight Xscale (boxheight * Xscale) |
| 274 |
3 -1 roll sub dup |
| 275 |
0 lt {-1 mul} if % box boxwidth boxheight mediawidth Xscale ((boxheight * Xscale) - mediaheight) |
| 276 |
dup 0 ne {2 div} if % box boxwidth boxheight mediawidth Xscale (heightdiff / 2) |
| 277 |
0 exch translate % box boxwidth boxheight mediawidth Xscale |
| 278 |
} { |
| 279 |
pop % box boxwidth boxheight mediawidth mediaheight Yscale |
| 280 |
dup 5 index mul % box boxwidth boxheight mediawidth mediaheight Yscale (boxwidth * Yscale) |
| 281 |
4 -1 roll sub dup |
| 282 |
0 lt {-1 mul} if % box boxwidth boxheight mediaheight Yscale ((boxwidth * Yscale) - mediawidth) |
| 283 |
dup 0 ne {2 div} if % box boxwidth boxheight mediawidth Yscale (widthdiff / 2) |
| 284 |
0 translate % box boxwidth boxheight mediawidth Yscale |
| 285 |
} ifelse |
| 286 |
|
| 287 |
% Apply any 'offset' in the Box. Tht is, if the llx and lly are not 0, then we need to |
| 288 |
% shift the origin of the content so that they become 0,0. We need to take into |
| 289 |
% account the scaling from above. |
| 290 |
4 index aload pop pop pop |
| 291 |
neg 2 index mul exch neg 2 index mul exch |
| 292 |
translate |
| 293 |
|
| 294 |
dup scale % scale both axes the same |
| 295 |
pop pop pop % remove the leftover boxwidth, boxheight and mediawidth/height |
| 296 |
} .internalbind def |
| 297 |
|
| 298 |
/newpdf_get_media_box { % <pagedict> get_media_box <box> <bool> |
| 299 |
dup /MediaBox known { |
| 300 |
/MediaBox get |
| 301 |
dup length 4 eq { |
| 302 |
//true |
| 303 |
} |
| 304 |
{ |
| 305 |
pop |
| 306 |
( **** Error: Page has an invalid /MediaBox attribute. Using the current page size.\n) |
| 307 |
newpdf_pdfformaterror |
| 308 |
( Output may be incorrect.\n) pdfformaterror |
| 309 |
[ 0 0 currentpagedevice /PageSize get aload pop ] |
| 310 |
//false |
| 311 |
}ifelse |
| 312 |
} |
| 313 |
{ |
| 314 |
pop |
| 315 |
( **** Error: Page has no /MediaBox attribute. Using the current page size.\n) |
| 316 |
newpdf_pdfformaterror |
| 317 |
( Output may be incorrect.\n) newpdf_pdfformaterror |
| 318 |
[ 0 0 currentpagedevice /PageSize get aload pop ] //false |
| 319 |
} ifelse |
| 320 |
} .internalbind def |
| 321 |
|
| 322 |
% [llx lly urx ury] newpdf_check_empty_box [llx lly urx ury] true | false |
| 323 |
% rturns true and the original array if its valid, otherwise returns false |
| 324 |
/newpdf_check_empty_box |
| 325 |
{ |
| 326 |
dup type /arraytype eq |
| 327 |
{ |
| 328 |
dup aload pop |
| 329 |
dup 3 index eq |
| 330 |
{ |
| 331 |
//true |
| 332 |
} |
| 333 |
{ |
| 334 |
1 index 4 index eq |
| 335 |
} ifelse |
| 336 |
{ |
| 337 |
pop pop pop pop |
| 338 |
( **** Warning: File has an empty Box parameter.\n) print |
| 339 |
( Using the MediaBox instead.\n) print |
| 340 |
( Output may be incorrect.\n) print flush |
| 341 |
pop //false |
| 342 |
} |
| 343 |
{ |
| 344 |
pop pop pop pop |
| 345 |
//true |
| 346 |
} ifelse |
| 347 |
} |
| 348 |
{ |
| 349 |
( **** Warning: File has an invalid Box parameter.\n) print |
| 350 |
( Using the MediaBox instead.\n) print |
| 351 |
( Output may be incorrect.\n) print flush |
| 352 |
pop //false |
| 353 |
} ifelse |
| 354 |
} .internalbind def |
| 355 |
|
| 356 |
/newpdf_get_any_box { % <pagedict> get_any_box <box name> <box> |
| 357 |
//systemdict /UseBleedBox .knownget dup { and } if { |
| 358 |
dup /BleedBox .knownget { |
| 359 |
newpdf_check_empty_box |
| 360 |
{ |
| 361 |
/BleedBox exch |
| 362 |
} if |
| 363 |
} if |
| 364 |
} if |
| 365 |
dup type /arraytype ne { |
| 366 |
//systemdict /UseTrimBox .knownget dup { and } if { |
| 367 |
dup /TrimBox .knownget { |
| 368 |
newpdf_check_empty_box |
| 369 |
{ |
| 370 |
/TrimBox exch |
| 371 |
} if |
| 372 |
} if |
| 373 |
} if |
| 374 |
} if |
| 375 |
dup type /arraytype ne { |
| 376 |
//systemdict /UseArtBox .knownget dup { and } if { |
| 377 |
dup /ArtBox .knownget { |
| 378 |
newpdf_check_empty_box |
| 379 |
{ |
| 380 |
/ArtBox exch |
| 381 |
} if |
| 382 |
} if |
| 383 |
} if |
| 384 |
} if |
| 385 |
dup type /arraytype ne { |
| 386 |
//systemdict /UseCropBox .knownget dup { and } if { |
| 387 |
dup /CropBox .knownget { |
| 388 |
newpdf_check_empty_box |
| 389 |
{ |
| 390 |
/CropBox exch |
| 391 |
} if |
| 392 |
} if |
| 393 |
} if |
| 394 |
} if |
| 395 |
dup type /arraytype ne { |
| 396 |
/MediaBox exch newpdf_get_media_box pop |
| 397 |
} { |
| 398 |
%% Complicated stuff. We need to use the 'Box' we identified (if any), but we |
| 399 |
%% need to clamp the boundaries of the 'Box' to the MediaBox. This appears to |
| 400 |
%% be what Acrobat does. The complication arises because the Box values don't |
| 401 |
%% have to be sensibly positive, its permissible to have the MediaBox set up |
| 402 |
%% so that it extends down and left instead of up and right. We take care of the |
| 403 |
%% content when we st up the CTM, but we do need to make sure that we clamp |
| 404 |
%% the BoundingBox, and that means we need to take direcitonality into account... |
| 405 |
|
| 406 |
aload pop |
| 407 |
6 -1 roll newpdf_get_media_box { % /SomeBox x0 y0 x1 y1 [MediaBox] |
| 408 |
aload pop % /SomeBox x0 y0 x1 y1 X0 Y0 X1 Y1 |
| 409 |
|
| 410 |
%% Start with the width, get the X0 and X1 values of the MediaBox |
| 411 |
3 index % /SomeBox x0 y0 x1 y1 X0 Y0 X1 Y1 X0 |
| 412 |
2 index % /SomeBox x0 y0 x1 y1 X0 Y0 X1 Y1 X0 X1 |
| 413 |
gt { |
| 414 |
%% Media extends to left |
| 415 |
4 -1 roll % /SomeBox x0 y0 x1 y1 Y0 X1 Y1 X0 |
| 416 |
8 -1 roll % /SomeBox y0 x1 y1 Y0 X1 Y1 X0 x0 |
| 417 |
.min % /SomeBox y0 x1 y1 Y0 X1 Y1 mX0 |
| 418 |
7 1 roll % /SomeBox mX0 y0 x1 y1 Y0 X1 Y1 |
| 419 |
exch % /SomeBox mX0 y0 x1 y1 Y0 Y1 X1 |
| 420 |
5 -1 roll % /SomeBox mX0 y0 y1 Y0 Y1 X1 x1 |
| 421 |
.max % /SomeBox mX0 y0 y1 Y0 Y1 mX1 |
| 422 |
5 1 roll % /SomeBox mX0 mX1 y0 y1 Y0 Y1 |
| 423 |
}{ |
| 424 |
%% Media extends to right |
| 425 |
4 -1 roll % /SomeBox x0 y0 x1 y1 Y0 X1 Y1 X0 |
| 426 |
8 -1 roll % /SomeBox y0 x1 y1 Y0 X1 Y1 X0 x0 |
| 427 |
.max % /SomeBox y0 x1 y1 Y0 X1 Y1 mX0 |
| 428 |
7 1 roll % /SomeBox mX0 y0 x1 y1 Y0 X1 Y1 |
| 429 |
exch % /SomeBox mX0 y0 x1 y1 Y0 Y1 X1 |
| 430 |
5 -1 roll % /SomeBox mX0 y0 y1 Y0 Y1 X1 x1 |
| 431 |
.min % /SomeBox mX0 y0 y1 Y0 Y1 mX1 |
| 432 |
5 1 roll % /SomeBox mX0 mX1 y0 y1 Y0 Y1 |
| 433 |
} ifelse |
| 434 |
|
| 435 |
%% Now deal with the height |
| 436 |
2 copy % /SomeBox mX0 mX1 y0 y1 Y0 Y1 Y0 Y1 |
| 437 |
gt { |
| 438 |
%% Media extends down |
| 439 |
exch % /SomeBox mX0 mX1 y0 y1 Y1 Y0 |
| 440 |
4 -1 roll % /SomeBox mX0 mX1 y1 Y1 Y0 y0 |
| 441 |
.min % /SomeBox mX0 mX1 y1 Y1 mY0 |
| 442 |
3 1 roll % /SomeBox mX0 mX1 mY0 y1 Y1 |
| 443 |
.max % /SomeBox mX0 mX1 mY0 mY1 |
| 444 |
}{ |
| 445 |
%% Media extends up |
| 446 |
exch % /SomeBox mX0 mX1 y0 y1 Y1 Y0 |
| 447 |
4 -1 roll % /SomeBox mX0 mX1 y1 Y1 Y0 y0 |
| 448 |
.max % /SomeBox mX0 mX1 y1 Y1 mY0 |
| 449 |
3 1 roll % /SomeBox mX0 mX1 mY0 y1 Y1 |
| 450 |
.min % /SomeBox mX0 mX1 mY0 mY1 |
| 451 |
} ifelse |
| 452 |
exch % /SomeBox mX0 mX1 mY1 mY0 |
| 453 |
3 1 roll % /SomeBox mX0 mY0 mX1 mY1 |
| 454 |
} { |
| 455 |
pop |
| 456 |
} ifelse |
| 457 |
4 array astore % /SomeBox [mX0 mY0 mX1 mY1] |
| 458 |
} ifelse |
| 459 |
} .internalbind def |
| 460 |
|
| 461 |
% This routine is used to set the PostScript /PageSize from the requested Box of the |
| 462 |
% PDF file. Much of this is copied from the old pdfshowpage_setpage routine which |
| 463 |
% is used for the PDF interpreter written in PostScript. |
| 464 |
% |
| 465 |
% Called with a dictionary containing the PDF page information. |
| 466 |
% |
| 467 |
/newpdf_set_pagesize |
| 468 |
{ |
| 469 |
% Stack: pdfpagedict |
| 470 |
|
| 471 |
% Don't understand this at all, removed for now replace if we ever figure out |
| 472 |
% what it's for. |
| 473 |
% Only lock in Orientation if we need to for pdfmarks |
| 474 |
% .writepdfmarks { /Orientation 0 def } if |
| 475 |
|
| 476 |
dup |
| 477 |
dup newpdf_get_any_box % Stack: pdfpagedict pdfpagedict /BoxName [box] |
| 478 |
|
| 479 |
% Check that the box is 'normal' and make it so if not |
| 480 |
% Also check the array is 4 elements and if it isn't then use the |
| 481 |
% current PageSize. The normalise arithmetic assumes the array is a 4 |
| 482 |
% element array. |
| 483 |
dup length 4 ne { |
| 484 |
% Invalid size of mediabox |
| 485 |
pop currentpagedevice /PageSize get |
| 486 |
} { |
| 487 |
aload 5 1 roll % array x1 y1 x2 y2 |
| 488 |
exch % array x1 y1 y2 x2 |
| 489 |
4 -1 roll % array y1 y2 x2 x1 |
| 490 |
2 copy gt { |
| 491 |
exch % array y1 y2 xmin xmax |
| 492 |
} if |
| 493 |
4 -2 roll % array xmin xmax y1 y2 |
| 494 |
2 copy gt { |
| 495 |
exch % array xmin xmax ymin ymax |
| 496 |
} if |
| 497 |
4 1 roll |
| 498 |
exch 4 -1 roll |
| 499 |
5 -1 roll |
| 500 |
astore |
| 501 |
} ifelse |
| 502 |
|
| 503 |
//systemdict /PDFFitPage known { |
| 504 |
NewPDF_FitPage |
| 505 |
} { |
| 506 |
6 dict begin % for setpagedevice |
| 507 |
|
| 508 |
% Set the page size. |
| 509 |
2 index /UserUnit known |
| 510 |
{ |
| 511 |
systemdict /NoUserUnit .knownget not {//false} if |
| 512 |
{ |
| 513 |
2 index /UserUnit undef |
| 514 |
% No scaling required, calculate PageSize as the width and height of the box |
| 515 |
aload 5 1 roll % box llx lly urx ury |
| 516 |
2 index sub % box llx lly urx (ury - lly) |
| 517 |
exch 3 index sub % box llx lly (ury - lly) (urx - llx) |
| 518 |
exch |
| 519 |
} |
| 520 |
{ |
| 521 |
2 index /UserUnit get |
| 522 |
/PassUserUnit /GetDeviceParam .special_op {exch pop} {//false} ifelse { |
| 523 |
[ /UserUnit 3 -1 roll .pdfputparams pop pop |
| 524 |
% No media scaling required, calculate PageSize as the width and height of the box |
| 525 |
aload 5 1 roll % box llx lly urx ury |
| 526 |
2 index sub % box llx lly urx (ury - lly) |
| 527 |
exch 3 index sub % box llx lly (ury - lly) (urx - llx) |
| 528 |
exch |
| 529 |
}{ |
| 530 |
% The PageSize needs to be multiplied too |
| 531 |
exch aload 5 1 roll % UserUnit box llx lly urx ury |
| 532 |
2 index sub exch % UserUnit box llx lly (ury - lly) urx |
| 533 |
3 index sub % UserUnit box llx lly boxheight boxwidth |
| 534 |
5 index mul % UserUnit box llx lly boxheight (Boxwidth*UserUnit) |
| 535 |
exch |
| 536 |
5 index mul % UserUnit box llx lly (Boxwidth*UserUnit) (boxheight*UserUnit) |
| 537 |
4 -2 roll |
| 538 |
5 index mul exch % UserUnit box (Boxwidth*UserUnit) (boxheight*UserUnit) (lly*UserUnit) llx |
| 539 |
5 index mul exch % UserUnit box (Boxwidth*UserUnit) (boxheight*UserUnit) (llx*UserUnit) (lly*UserUnit) |
| 540 |
4 2 roll % UserUnit box (llx*UserUnit) (lly*UserUnit) (Boxwidth*UserUnit) (boxheight*UserUnit) |
| 541 |
6 -1 roll pop % box (llx*UserUnit) (lly*UserUnit) (Boxwidth*UserUnit) (boxheight*UserUnit) |
| 542 |
} ifelse |
| 543 |
} ifelse |
| 544 |
} |
| 545 |
{ |
| 546 |
/PassUserUnit /GetDeviceParam .special_op { |
| 547 |
exch pop { |
| 548 |
[ /UserUnit 1 .pdfputparams pop pop |
| 549 |
} if |
| 550 |
}if |
| 551 |
% No scaling required, calculate PageSize as the width and height of the box |
| 552 |
aload 5 1 roll % box llx lly urx ury |
| 553 |
2 index sub % box llx lly urx (ury - lly) |
| 554 |
exch 3 index sub % box llx lly (ury - lly) (urx - llx) |
| 555 |
exch |
| 556 |
} ifelse |
| 557 |
|
| 558 |
% handle page rotation here |
| 559 |
6 index /Rotate known { |
| 560 |
% PDF page rotation runs clockwise and must be a multiple of 90, unlike PostScript..... |
| 561 |
% convert Rotate into a quadrant number |
| 562 |
6 index /Rotate get 90 div cvi |
| 563 |
% Make sure quadrant is 0-3 |
| 564 |
dup 0 lt { |
| 565 |
% Negative rotation... Turn it into a positive rotation |
| 566 |
% Again, limit the quadrant to -0 to -3 |
| 567 |
dup -4 lt {4 mod} if |
| 568 |
% Adding 360 degrees results in the same positive rotation |
| 569 |
4 add |
| 570 |
} if |
| 571 |
dup 3 gt {4 mod} if |
| 572 |
dup 0 eq |
| 573 |
{ |
| 574 |
pop |
| 575 |
2 array astore /PageSize exch def |
| 576 |
currentdict end setpagedevice |
| 577 |
neg exch neg exch |
| 578 |
translate |
| 579 |
} |
| 580 |
{ |
| 581 |
dup 1 eq |
| 582 |
{ |
| 583 |
pop |
| 584 |
exch 4 2 roll exch 4 2 roll |
| 585 |
2 array astore dup /PageSize exch def |
| 586 |
currentdict end setpagedevice |
| 587 |
270 rotate |
| 588 |
1 get add neg exch neg |
| 589 |
translate |
| 590 |
} |
| 591 |
{ |
| 592 |
2 eq |
| 593 |
{ |
| 594 |
2 array astore dup /PageSize exch def |
| 595 |
currentdict end setpagedevice |
| 596 |
180 rotate |
| 597 |
aload pop 3 -1 roll add neg 3 1 roll add neg exch |
| 598 |
translate |
| 599 |
} |
| 600 |
{ |
| 601 |
exch 4 2 roll exch 4 2 roll |
| 602 |
2 array astore dup /PageSize exch def |
| 603 |
currentdict end setpagedevice |
| 604 |
90 rotate |
| 605 |
0 get 3 -1 roll add |
| 606 |
neg exch neg exch |
| 607 |
translate |
| 608 |
} ifelse |
| 609 |
} ifelse |
| 610 |
} ifelse |
| 611 |
} |
| 612 |
{ |
| 613 |
2 array astore /PageSize exch def |
| 614 |
currentdict end setpagedevice |
| 615 |
neg exch neg exch |
| 616 |
translate |
| 617 |
}ifelse |
| 618 |
|
| 619 |
% scale the co-ordinate system by the UserUnit |
| 620 |
% We have to do this after setting the PageSize, because |
| 621 |
% setpagedevice does an implicit initgraphics, resetting the CTM. |
| 622 |
% |
| 623 |
//systemdict /NoUserUnit .knownget not { //false } if not |
| 624 |
{ |
| 625 |
2 index /UserUnit known { |
| 626 |
/PassUserUnit /GetDeviceParam .special_op { |
| 627 |
exch pop not |
| 628 |
} { |
| 629 |
true |
| 630 |
}ifelse |
| 631 |
|
| 632 |
% If PassUserUnit isn't handled by the device, or it returns 'false' |
| 633 |
% then scale the content by the UserUnit. Otherwise do not scale. |
| 634 |
{ |
| 635 |
2 index /UserUnit get |
| 636 |
dup scale |
| 637 |
} if |
| 638 |
} if |
| 639 |
} if |
| 640 |
} ifelse |
| 641 |
|
| 642 |
3 1 roll % <page dict> /BoxName [box dimensions] -> [box dimensions] <page dict> /BoxName |
| 643 |
|
| 644 |
% If we are using the MediaBox (and only the MediaBox) then we |
| 645 |
% want to clip to the CropBox, if there is one present. For every |
| 646 |
% other case, clip to the given Box. |
| 647 |
|
| 648 |
/MediaBox eq { |
| 649 |
dup /CropBox known { |
| 650 |
/CropBox get |
| 651 |
aload pop 2 index sub exch 3 index sub exch rectclip |
| 652 |
pop % [box dimensions] array |
| 653 |
} { |
| 654 |
pop % <page dict> |
| 655 |
|
| 656 |
aload % Load the co-ordinates of the box onto the stack |
| 657 |
pop % The array with the box co-ordinates |
| 658 |
% llx lly urx ury |
| 659 |
2 index sub % ll lly urx (ury - lly) |
| 660 |
exch % llx lly height urx |
| 661 |
3 index sub % llx lly height (urx - llx) |
| 662 |
exch % llx lly width height |
| 663 |
rectclip |
| 664 |
} ifelse |
| 665 |
} { |
| 666 |
pop % <page dict> |
| 667 |
|
| 668 |
aload % Load the co-ordinates of the box onto the stack |
| 669 |
pop % The array with the box co-ordinates |
| 670 |
% llx lly urx ury |
| 671 |
2 index sub % ll lly urx (ury - lly) |
| 672 |
exch % llx lly height urx |
| 673 |
3 index sub % llx lly height (urx - llx) |
| 674 |
exch % llx lly width height |
| 675 |
rectclip |
| 676 |
}ifelse |
| 677 |
|
| 678 |
pop |
| 679 |
} .internalbind def |
| 680 |
|
| 681 |
% This routine checks a list of known/implemented command line switches to see if they |
| 682 |
% have been defined in the PostScript environment. If they have we construct a dictionary |
| 683 |
% containing the names and their values, and return it. That dictionary can then be |
| 684 |
% passed to a custom PostScript operator and used to configure the PDF interpreter. |
| 685 |
% |
| 686 |
% NB device parameters will already have been sent to the device and used to configure it |
| 687 |
% so here we should only handle parameters which control the behaviour of the interpreter. |
| 688 |
% |
| 689 |
/PDFSwitches [ /QUIET /PDFCACHE /PDFPassword /PDFDEBUG /PDFSTOPONERROR /PDFSTOPONWARNING /NOTRANSPARENCY /FirstPage /LastPage |
| 690 |
/PDFA /PDFACompatibilityPolicy /PDFNOCIDFALLBACK /NO_PDFMARK_OUTLINES /NO_PDFMARK_DESTS /PDFFitPage /Printed /UsePDFX3Profile |
| 691 |
/UseBleedBox /UseCropBox /UseArtBox /UseTrimBox /ShowAcroForm /ShowAnnots /PreserveAnnots |
| 692 |
/NoUserUnit /RENDERTTNOTDEF /DOPDFMARKS /PDFINFO /ShowAnnotTypes /PreserveAnnotTypes |
| 693 |
/CIDFSubstPath /CIDFSubstFont /SUBSTFONT /IgnoreToUnicode /NONATIVEFONTMAP /PreserveMarkedContent /OutputFile |
| 694 |
/PreserveDocView /PreserveEmbeddedFiles ] def |
| 695 |
|
| 696 |
/newpdf_gather_parameters |
| 697 |
{ |
| 698 |
10 dict begin |
| 699 |
|
| 700 |
//PDFSwitches { |
| 701 |
dup where |
| 702 |
{ exch dup 3 1 roll get def } |
| 703 |
{ pop } ifelse |
| 704 |
} forall |
| 705 |
|
| 706 |
% This isn't a command line parameter, we track it internally, but we need to |
| 707 |
% send it to the interpreter. It is used to 'offset' the page Dest for Link |
| 708 |
% annotations and Outlines by the numebr of pages processed so far. |
| 709 |
/PageCount /CumulativePageCount where { /CumulativePageCount get }{currentpagedevice /PageCount get} ifelse def |
| 710 |
|
| 711 |
currentdict end |
| 712 |
} .internalbind def |
| 713 |
|
| 714 |
currentdict /PDFSwitches undef |
| 715 |
|
| 716 |
/pdfpagecount |
| 717 |
{ |
| 718 |
currentdict /PDFInfo known |
| 719 |
{ |
| 720 |
PDFInfo |
| 721 |
} |
| 722 |
{ |
| 723 |
PDFFile //null eq not |
| 724 |
{ |
| 725 |
PDFSTOPONERROR |
| 726 |
{ |
| 727 |
PDFFile .PDFInfo //false |
| 728 |
} |
| 729 |
{ |
| 730 |
PDFFile {.PDFInfo} stopped |
| 731 |
} ifelse |
| 732 |
} |
| 733 |
{ |
| 734 |
//true |
| 735 |
}ifelse |
| 736 |
|
| 737 |
{ |
| 738 |
<</NumPages 0>> |
| 739 |
} if |
| 740 |
} ifelse |
| 741 |
|
| 742 |
dup /NumPages known |
| 743 |
{ |
| 744 |
/NumPages get |
| 745 |
} |
| 746 |
{ |
| 747 |
pop 0 |
| 748 |
} ifelse |
| 749 |
}.internalbind def |
| 750 |
|
| 751 |
/runpdfpagerange |
| 752 |
{ |
| 753 |
/PageList where { |
| 754 |
pop |
| 755 |
PageList pdfpagecount .PDFparsePageList |
| 756 |
dup 0 eq { % No ranges, error |
| 757 |
(\n **** Error: Invalid PageList: ) print |
| 758 |
PageList print |
| 759 |
(\n No pages will be processed.) = flush |
| 760 |
/runpdfpagerange cvx /syntaxerror signalerror |
| 761 |
} if |
| 762 |
array astore % move integer triples from the stack to the array |
| 763 |
% newpdf PDFPageList is an array of 3 elements per range: even/odd flag, start, end |
| 764 |
/PDFPageList exch def |
| 765 |
QUIET not { |
| 766 |
(Processing pages ) print PageList =only (.) = flush |
| 767 |
} if |
| 768 |
1 pdfpagecount % dummy parameters for newpdf_dopages |
| 769 |
}{ |
| 770 |
/FirstPage where { |
| 771 |
pop FirstPage dup pdfpagecount gt { |
| 772 |
(\nRequested FirstPage is greater than the number of pages in the file: ) print |
| 773 |
pdfpagecount = flush |
| 774 |
} if |
| 775 |
} { |
| 776 |
1 |
| 777 |
} ifelse |
| 778 |
/LastPage where {pop LastPage pdfpagecount .min } { pdfpagecount } ifelse |
| 779 |
1 index 1 index gt { |
| 780 |
( No pages will be processed \(FirstPage > LastPage\).) = flush |
| 781 |
} { |
| 782 |
QUIET not { |
| 783 |
(Processing pages ) print 1 index =only ( through ) print dup =only |
| 784 |
(.) = flush |
| 785 |
} if |
| 786 |
} ifelse |
| 787 |
} ifelse |
| 788 |
} .internalbind def |
| 789 |
|
| 790 |
|
| 791 |
/runpdf_collection_entry |
| 792 |
{ |
| 793 |
<</PreserveDocView false>> runpdfbegin_with_params % <file> runpdfbegin - |
| 794 |
PDFInfo type /dicttype eq |
| 795 |
{ |
| 796 |
PDFInfo /Collection known |
| 797 |
{ |
| 798 |
PDFInfo /Collection get |
| 799 |
pdfclose |
| 800 |
dup length 1 sub 0 2 3 -1 roll |
| 801 |
{ |
| 802 |
1 index exch get (r) file runpdf_collection_entry |
| 803 |
} for |
| 804 |
pop |
| 805 |
} |
| 806 |
{ |
| 807 |
process_trailer_attrs % - process_trailer_attrs - |
| 808 |
runpdfpagerange % - runpdfpagerange <int> <int> |
| 809 |
dopdfpages % <int> <int> dopdfpages - |
| 810 |
runpdfend % - runpdfend - |
| 811 |
} ifelse |
| 812 |
} { |
| 813 |
pop pop |
| 814 |
}ifelse |
| 815 |
} .internalbind def |
| 816 |
|
| 817 |
% <file> runpdf - |
| 818 |
/newpdf_runpdf |
| 819 |
{ |
| 820 |
runpdfbegin % <file> runpdfbegin - |
| 821 |
PDFInfo type /dicttype eq |
| 822 |
{ |
| 823 |
PDFInfo /Collection known |
| 824 |
{ |
| 825 |
PDFInfo /Collection get |
| 826 |
pdfclose |
| 827 |
dup length 1 sub 0 2 3 -1 roll |
| 828 |
{ |
| 829 |
1 index exch get (r) file runpdf_collection_entry |
| 830 |
} for |
| 831 |
pop |
| 832 |
% We need to do this, even though we called pdfclose above, in order to clean |
| 833 |
% up our dictionary and restore the state. We can't use runpdfend above because |
| 834 |
% if we do then we will delete all the temporary files containing the embedded |
| 835 |
% PDF files. |
| 836 |
runpdfend |
| 837 |
} |
| 838 |
{ |
| 839 |
process_trailer_attrs % - process_trailer_attrs - |
| 840 |
runpdfpagerange % - runpdfpagerange <int> <int> |
| 841 |
dopdfpages % <int> <int> dopdfpages - |
| 842 |
runpdfend % - runpdfend - |
| 843 |
} ifelse |
| 844 |
} { |
| 845 |
pop pop |
| 846 |
}ifelse |
| 847 |
} .internalbind def |
| 848 |
|
| 849 |
% start a PDF file, and specify interpreter parameters to be used |
| 850 |
% to override any command line ones. |
| 851 |
% Mostly for the benefit of PDF Collections where we want to disable |
| 852 |
%preserving DocView information for pdfwrite. |
| 853 |
% |
| 854 |
% file <dict> runpdfbegin_with_params - |
| 855 |
/runpdfbegin_with_params |
| 856 |
{ |
| 857 |
/pdfdict 10 dict def |
| 858 |
pdfdict begin |
| 859 |
|
| 860 |
currentpagedevice /PageCount get |
| 861 |
/CumulativePageCount exch def |
| 862 |
% This is for the benefit of pdf2dsc which assumes it will be present |
| 863 |
/Trailer << >> def |
| 864 |
/PDFSave save def |
| 865 |
% Define these in the current dictionary, if anyone uses |
| 866 |
% systemdict then this will be defeated so we'll add extra |
| 867 |
% code to the systemdict definitions as well. |
| 868 |
% /pdfpagecount /newpdf_pagecount load def |
| 869 |
% /pdfgetpage /newpdf_pdfgetpage load def |
| 870 |
% /pdfshowpage /newpdf_pdfshowpage load def |
| 871 |
% /pdfshowpage_init /newpdf_pdfshowpage_init load def |
| 872 |
% /pdfshowpage_setpage /newpdf_pdfshowpage_setpage load def |
| 873 |
% /pdfshowpage_finish /newpdf_pdfshowpage_finish load def |
| 874 |
% /pdfopen /newpdf_pdfopen load def |
| 875 |
% /pdfclose /newpdf_pdfclose load def |
| 876 |
% /dopdfpages /newpdf_dopdfpages load def |
| 877 |
% /runpdfend /newpdf_runpdfend load def |
| 878 |
% /runpdfpagerange /newpdf_runpdfpagerange load def |
| 879 |
% /process_trailer_attrs /newpdf_process_trailer_attrs load def |
| 880 |
|
| 881 |
% These are also for the benefit of pdf2dsc, which assumes they |
| 882 |
% are available. |
| 883 |
/knownoget {2 copy known {get //true}{pop pop //false}ifelse} bind def |
| 884 |
/pget {2 copy known {get //true}{pop pop //false}ifelse}bind def |
| 885 |
|
| 886 |
newpdf_gather_parameters % <parameter overrides dict> <parameter dict> |
| 887 |
exch {2 index 3 1 roll put} forall |
| 888 |
PDFSTOPONERROR |
| 889 |
{ |
| 890 |
.PDFInit |
| 891 |
/PDFFile exch def |
| 892 |
pdfopen |
| 893 |
/PDFInfo exch def |
| 894 |
pop |
| 895 |
} |
| 896 |
{ |
| 897 |
{.PDFInit} stopped |
| 898 |
{ |
| 899 |
( **** Error: Failed to initialise PDF interpreter.\n) newpdf_pdfformaterror |
| 900 |
/PDFFile //null def |
| 901 |
/PDFInfo //null def |
| 902 |
} |
| 903 |
{ |
| 904 |
/PDFFile exch def |
| 905 |
pdfopen |
| 906 |
/PDFInfo exch def |
| 907 |
pop |
| 908 |
}ifelse |
| 909 |
}ifelse |
| 910 |
} .internalbind def |
| 911 |
|
| 912 |
% -file- runpdfbegin - |
| 913 |
/runpdfbegin |
| 914 |
{ |
| 915 |
<<>> runpdfbegin_with_params |
| 916 |
} .internalbind def |
| 917 |
|
| 918 |
/pdfgetpage |
| 919 |
{ |
| 920 |
dup 1 sub |
| 921 |
PDFSTOPONERROR |
| 922 |
{ |
| 923 |
PDFFile exch .PDFPageInfo |
| 924 |
dup 3 -1 roll |
| 925 |
/Page# exch put |
| 926 |
} |
| 927 |
{ |
| 928 |
PDFFile exch {.PDFPageInfo} stopped |
| 929 |
{ |
| 930 |
pop pop |
| 931 |
( **** Error: Couldn't get page info.\n) newpdf_pdfformaterror |
| 932 |
( Output may be incorrect.\n) newpdf_pdfformaterror |
| 933 |
//null |
| 934 |
}{ |
| 935 |
dup 3 -1 roll |
| 936 |
/Page# exch put |
| 937 |
} ifelse |
| 938 |
}ifelse |
| 939 |
} .internalbind def |
| 940 |
|
| 941 |
/process_trailer_attrs |
| 942 |
{ |
| 943 |
}.internalbind def |
| 944 |
|
| 945 |
/pdfshowpage_init |
| 946 |
{ |
| 947 |
} .internalbind def |
| 948 |
|
| 949 |
/pdfshowpage_setpage |
| 950 |
{ |
| 951 |
dup newpdf_device_setup |
| 952 |
dup newpdf_set_pagesize |
| 953 |
} .internalbind def |
| 954 |
|
| 955 |
/pdfshowpage_finish |
| 956 |
{ |
| 957 |
PDFSTOPONERROR |
| 958 |
{ |
| 959 |
/Page# get PDFFile exch 1 sub .PDFDrawPage |
| 960 |
showpage |
| 961 |
} |
| 962 |
{ |
| 963 |
/Page# get PDFFile exch 1 sub {.PDFDrawPage} stopped |
| 964 |
{ |
| 965 |
pop pop |
| 966 |
( **** Error: Page drawing error occurred.\n) newpdf_pdfformaterror |
| 967 |
( Output may be incorrect.\n) newpdf_pdfformaterror |
| 968 |
} if |
| 969 |
{showpage} stopped |
| 970 |
{ |
| 971 |
( **** Error: Page drawing error occurred.\n) newpdf_pdfformaterror |
| 972 |
( Could not draw this page at all, page will be missing in the output.\n) newpdf_pdfformaterror |
| 973 |
} if |
| 974 |
}ifelse |
| 975 |
} .internalbind def |
| 976 |
|
| 977 |
/pdfshowpage |
| 978 |
{ |
| 979 |
/PDFINFO where {/PDFINFO get}{//false}ifelse |
| 980 |
{ |
| 981 |
/Page# get PDFFile exch 1 sub {.PDFDrawPage} stopped pop |
| 982 |
} |
| 983 |
{ |
| 984 |
pdfshowpage_init |
| 985 |
pdfshowpage_setpage |
| 986 |
pdfshowpage_finish |
| 987 |
}ifelse |
| 988 |
} .internalbind def |
| 989 |
|
| 990 |
/runpdfend |
| 991 |
{ |
| 992 |
% Get the accumulated count of pages processed so far |
| 993 |
% and the number of pages in this file. Do this before |
| 994 |
% we close the file and restore the state. Save the values |
| 995 |
% on the stack. |
| 996 |
pdfpagecount |
| 997 |
CumulativePageCount |
| 998 |
|
| 999 |
pdfclose |
| 1000 |
PDFSave restore |
| 1001 |
|
| 1002 |
end % pdfdict |
| 1003 |
|
| 1004 |
% add the number of pages in this file to the accumulated count, |
| 1005 |
% and store that in the device for later reuse. This allows us to |
| 1006 |
% add the number of pages already in the output to the 'Dest' of |
| 1007 |
% Outlines and Link annotations. |
| 1008 |
add <</PageCount 3 -1 roll >> setpagedevice |
| 1009 |
} .internalbind def |
| 1010 |
|
| 1011 |
/pdfopen |
| 1012 |
{ |
| 1013 |
dup PDFFile //null ne |
| 1014 |
{ |
| 1015 |
PDFSTOPONERROR |
| 1016 |
{ |
| 1017 |
PDFFile .PDFStream |
| 1018 |
PDFFile .PDFInfo |
| 1019 |
} |
| 1020 |
{ |
| 1021 |
PDFFile {.PDFStream} stopped |
| 1022 |
{ |
| 1023 |
pop pop |
| 1024 |
( **** Error: Couldn't initialise file.\n) newpdf_pdfformaterror |
| 1025 |
( Output may be incorrect.\n) newpdf_pdfformaterror |
| 1026 |
<</NumPages 0>> |
| 1027 |
} |
| 1028 |
{ |
| 1029 |
PDFFile {.PDFInfo} stopped |
| 1030 |
{ |
| 1031 |
pop |
| 1032 |
( **** Error: Couldn't get page information.\n) newpdf_pdfformaterror |
| 1033 |
( Output may be incorrect.\n) newpdf_pdfformaterror |
| 1034 |
<</NumPages 0>> |
| 1035 |
} if |
| 1036 |
} ifelse |
| 1037 |
}ifelse |
| 1038 |
} |
| 1039 |
{ |
| 1040 |
pop |
| 1041 |
<</NumPages 0>> |
| 1042 |
} ifelse |
| 1043 |
} .internalbind def |
| 1044 |
|
| 1045 |
/pdfclose |
| 1046 |
{ |
| 1047 |
PDFSTOPONERROR |
| 1048 |
{ |
| 1049 |
PDFFile .PDFClose |
| 1050 |
} |
| 1051 |
{ |
| 1052 |
PDFFile {.PDFClose} stopped |
| 1053 |
{ |
| 1054 |
pop |
| 1055 |
} if |
| 1056 |
}ifelse |
| 1057 |
} .internalbind def |
| 1058 |
|
| 1059 |
/pdfavailable |
| 1060 |
{ |
| 1061 |
.PDFAvailable |
| 1062 |
} .internalbind def |
| 1063 |
|
| 1064 |
% <int> <int> dopdfpages - |
| 1065 |
% First Page and then LastPage |
| 1066 |
% If PDFPageList array exists, the parameters are 1 pdfpagecount and are ignored. |
| 1067 |
/dopdfpages |
| 1068 |
{ |
| 1069 |
//DisablePageHandlerDevice exec |
| 1070 |
%% If we have a array of page ranges to render, use it. |
| 1071 |
/PDFPageList where { |
| 1072 |
pop |
| 1073 |
pop pop % don't use dummy parameters |
| 1074 |
PDFPageList |
| 1075 |
% process the ranges (3 elements per range) |
| 1076 |
0 3 2 index length 1 sub { |
| 1077 |
1 index 1 index get % even = 2, odd = 1 any = 0 |
| 1078 |
2 index 2 index 1 add get % start of range |
| 1079 |
exch |
| 1080 |
3 index 3 index 2 add get % end of range |
| 1081 |
exch |
| 1082 |
% stack: start end even/odd |
| 1083 |
0 eq { 1 } { 2 } ifelse |
| 1084 |
2 index 2 index gt { neg } if % negate increment for reverse range |
| 1085 |
exch |
| 1086 |
{ |
| 1087 |
pdfgetpage |
| 1088 |
dup //null ne { |
| 1089 |
pdfshowpage |
| 1090 |
} { |
| 1091 |
PDFSTOPONERROR { |
| 1092 |
/dopdfpages cvx /syntaxerror signalerror |
| 1093 |
} { |
| 1094 |
pop pop |
| 1095 |
( **** Error: page) newpdf_pdfformaterror |
| 1096 |
( not found.\n) newpdf_pdfformaterror |
| 1097 |
} ifelse |
| 1098 |
} ifelse |
| 1099 |
} for |
| 1100 |
pop % for loop index |
| 1101 |
} for |
| 1102 |
pop % done with array |
| 1103 |
} { |
| 1104 |
% else, Process the pages given by the FirstPage, LastPage |
| 1105 |
1 exch |
| 1106 |
{ |
| 1107 |
pdfgetpage |
| 1108 |
dup //null ne { |
| 1109 |
pdfshowpage |
| 1110 |
} { |
| 1111 |
PDFSTOPONERROR { |
| 1112 |
/dopdfpages cvx /syntaxerror signalerror |
| 1113 |
} { |
| 1114 |
pop pop |
| 1115 |
( **** Error: page) newpdf_pdfformaterror |
| 1116 |
( not found.\n) newpdf_pdfformaterror |
| 1117 |
} ifelse |
| 1118 |
} ifelse |
| 1119 |
} for |
| 1120 |
} ifelse |
| 1121 |
//EnablePageHandlerDevice exec |
| 1122 |
} .internalbind def |
| 1123 |
|
| 1124 |
/.runps /run load def |
| 1125 |
/run { |
| 1126 |
dup type /filetype ne { (r) file } if |
| 1127 |
% skip leading whitespace characters (actually anything less than or equal to <sp>) |
| 1128 |
{ dup ( ) .peekstring not { //false exit } if |
| 1129 |
dup 0 get 32 le { pop dup read pop pop } { //true exit } ifelse |
| 1130 |
} loop |
| 1131 |
exch pop |
| 1132 |
{ |
| 1133 |
% Appletalk PAP sends short strings with %! header expecting a response. |
| 1134 |
% 'gv' swallows the %!PS line, then sends DSC comments beginning with %% |
| 1135 |
% and also waits for a response. The following avoids those hangs. |
| 1136 |
% |
| 1137 |
% This section commented out. It causes problems with (poorly created) PDF files which have |
| 1138 |
% extraneous data prior to the '%PDF-' string which are incorrectly recognised as PostScript |
| 1139 |
% programs. I really don't think we need care about AppleTalk any more and testing with 'gv' |
| 1140 |
% shows no problems with removing this early test, and simply falling into the 'not a PDF file' |
| 1141 |
% case below. |
| 1142 |
% |
| 1143 |
% I've preserved the code in comments for the next release (10.05.0), in case it does prove to be essential. |
| 1144 |
% |
| 1145 |
% dup 2 string .peekstring pop dup (%!) eq exch (%%) eq or { |
| 1146 |
% cvx .runps |
| 1147 |
% } { |
| 1148 |
|
| 1149 |
dup 1023 string .peekstring pop |
| 1150 |
% "1024 string" exceeds current %stdin buffer |
| 1151 |
% Valid PDF file cannot be smaller than 400 bytes. |
| 1152 |
(%PDF-) search { |
| 1153 |
3 1 roll pop pop |
| 1154 |
dup (%!PS) search not { |
| 1155 |
length 0 ne { |
| 1156 |
1 index exch readstring pop pop |
| 1157 |
(%stderr) (w) file dup |
| 1158 |
( **** Warning: File has some garbage before %PDF- .\n) |
| 1159 |
writestring flushfile |
| 1160 |
PDFSTOPONWARNING { |
| 1161 |
/run cvx /unregistered signalerror |
| 1162 |
} if |
| 1163 |
} { |
| 1164 |
pop |
| 1165 |
} ifelse |
| 1166 |
dup (%stdin) (r) file eq { |
| 1167 |
% Copy PDF from stdin to temporary file then run it. |
| 1168 |
//null (w+) .tempfile exch 3 1 roll |
| 1169 |
% stack: tempname stdin tempfile |
| 1170 |
64000 string |
| 1171 |
{ |
| 1172 |
% stack: tempname stdin tempfile string |
| 1173 |
2 index 1 index readstring |
| 1174 |
exch 3 index exch writestring |
| 1175 |
not { exit } if |
| 1176 |
} |
| 1177 |
loop |
| 1178 |
pop exch closefile |
| 1179 |
% stack: tempname tempfile |
| 1180 |
dup 0 setfileposition |
| 1181 |
dup |
| 1182 |
pdfavailable { |
| 1183 |
runpdf |
| 1184 |
}{ |
| 1185 |
closefile |
| 1186 |
(%stderr) (w) file ( **** ERROR: No PDF interpreter available, unable to process PDF files as input.\n)writestring |
| 1187 |
} ifelse |
| 1188 |
closefile deletefile |
| 1189 |
} { |
| 1190 |
pdfavailable { |
| 1191 |
runpdf |
| 1192 |
}{ |
| 1193 |
closefile |
| 1194 |
(%stderr) (w) file ( **** ERROR: No PDF interpreter available, unable to process PDF files as input.\n)writestring |
| 1195 |
} ifelse |
| 1196 |
} ifelse |
| 1197 |
} { |
| 1198 |
pop pop pop pop cvx .runps % (%!PS) found first |
| 1199 |
} ifelse |
| 1200 |
} { |
| 1201 |
pop cvx .runps % (%PDF-) not found |
| 1202 |
} ifelse |
| 1203 |
% } ifelse |
| 1204 |
} { |
| 1205 |
closefile % file was empty |
| 1206 |
} ifelse |
| 1207 |
} .internalbind odef |
| 1208 |
currentdict /runpdfstring .undef |
| 1209 |
|
| 1210 |
currentdict /DisablePageHandlerDevice undef |
| 1211 |
currentdict /EnablePageHandlerDevice undef |
| 1212 |
|
| 1213 |
% Copy stream to an external temporary file and |
| 1214 |
% return the file name as PS name. |
| 1215 |
/copy_embedded_file { |
| 1216 |
//true resolvestream % strm |
| 1217 |
dup 1023 string .peekstring pop % "1024 string" exceeds current %stdin buffer |
| 1218 |
dup length 400 ge { % Valid PDF file cannot be smaller than 400 bytes. |
| 1219 |
(%PDF-) search { |
| 1220 |
pop pop pop //true |
| 1221 |
} { |
| 1222 |
pop //false |
| 1223 |
} ifelse |
| 1224 |
} { |
| 1225 |
pop //false |
| 1226 |
} ifelse { |
| 1227 |
//null (w) .tempfile % strm (name) null (w) .tempfile |
| 1228 |
% strm (name) file |
| 1229 |
3 -1 roll % (name) file strm |
| 1230 |
32768 string % (name) file strm (buf) |
| 1231 |
{ 3 copy readstring % (name) file strm (buf) file (data) bool |
| 1232 |
3 1 roll % (name) file strm (buf) bool file (data) |
| 1233 |
writestring % (name) file strm (buf) bool |
| 1234 |
not { exit } if |
| 1235 |
} loop |
| 1236 |
pop closefile % (name) file |
| 1237 |
closefile % (name) |
| 1238 |
cvn % /name |
| 1239 |
} { |
| 1240 |
closefile |
| 1241 |
} ifelse |
| 1242 |
} .internalbind def |
| 1243 |
|
| 1244 |
% Recursively enumerate /Names entries |
| 1245 |
% <node> pdf_collection_names /temp_file_name ... |
| 1246 |
/pdf_collection_names { |
| 1247 |
dup /Names knownoget { |
| 1248 |
exch pop |
| 1249 |
{ oforce |
| 1250 |
dup type /dicttype eq { |
| 1251 |
/EF knownoget { |
| 1252 |
/F knownoget { |
| 1253 |
copy_embedded_file |
| 1254 |
} if |
| 1255 |
} if |
| 1256 |
} { |
| 1257 |
pop |
| 1258 |
} ifelse |
| 1259 |
} forall |
| 1260 |
} { |
| 1261 |
/Kids knownoget { |
| 1262 |
{ oforce |
| 1263 |
dup //null ne { |
| 1264 |
pdf_collection_names |
| 1265 |
} { |
| 1266 |
pop |
| 1267 |
} ifelse |
| 1268 |
} forall |
| 1269 |
} if |
| 1270 |
} ifelse |
| 1271 |
} .internalbind def |
| 1272 |
|
| 1273 |
/runpdf { % <file> runpdf - |
| 1274 |
dup type /filetype eq |
| 1275 |
{ |
| 1276 |
dup |
| 1277 |
PDFSTOPONERROR |
| 1278 |
{ |
| 1279 |
newpdf_runpdf |
| 1280 |
} |
| 1281 |
{ |
| 1282 |
{newpdf_runpdf} stopped |
| 1283 |
{ |
| 1284 |
( **** Error: PDF interpreter encountered an error processing the file.\n) pdfformaterror |
| 1285 |
} if |
| 1286 |
}ifelse |
| 1287 |
closefile |
| 1288 |
} |
| 1289 |
{ |
| 1290 |
( **** Error: Attempt to process something other than a file object in runpdf.\n) pdfformaterror |
| 1291 |
} ifelse |
| 1292 |
} .internalbind odef |
| 1293 |
|
| 1294 |
end % systemdict |
| 1295 |
|
| 1296 |
% Redefine the procedure that the C code uses for running piped input. |
| 1297 |
% It is OK to use { (%stdin) run } here, because a startjob cannot occur. |
| 1298 |
/.runstdin { |
| 1299 |
{ (%stdin) run } execute0 |
| 1300 |
} .internalbind def |
| 1301 |
|
| 1302 |
end % userdict |
| 1303 |
|
| 1304 |
% ------ Transparency support ------ % |
| 1305 |
|
| 1306 |
systemdict /ALLOWPSTRANSPARENCY get |
| 1307 |
{ |
| 1308 |
/.setopacityalpha |
| 1309 |
{ |
| 1310 |
/.setfillconstantalpha where |
| 1311 |
{ |
| 1312 |
pop |
| 1313 |
( **** WARNING: .setopacityalpha is deprecated (as of 9.53.0) and will be removed in a future release\n) print |
| 1314 |
( **** See .setfillconstantalpha/.setalphaisshape for the improved solution\n) print flush |
| 1315 |
false .setalphaisshape |
| 1316 |
dup .setfillconstantalpha |
| 1317 |
.setstrokeconstantalpha |
| 1318 |
} |
| 1319 |
{ |
| 1320 |
/.setopacityalpha /undefined cvx signalerror |
| 1321 |
} ifelse |
| 1322 |
} .internalbind def |
| 1323 |
|
| 1324 |
/.setshapealpha |
| 1325 |
{ |
| 1326 |
/.setfillconstantalpha where |
| 1327 |
{ |
| 1328 |
pop |
| 1329 |
( **** WARNING: .setshapealpha is deprecated (as of 9.53.0) and will be removed in a future release.\n) print |
| 1330 |
( **** See .setfillconstantalpha/.setalphaisshape for the improved solution\n) print flush |
| 1331 |
true .setalphaisshape |
| 1332 |
dup .setfillconstantalpha |
| 1333 |
.setstrokeconstantalpha |
| 1334 |
} |
| 1335 |
{ |
| 1336 |
/.setshapealpha /undefined cvx signalerror |
| 1337 |
} ifelse |
| 1338 |
} .internalbind def |
| 1339 |
} if |
| 1340 |
|
| 1341 |
.setglobal |
| 1342 |
|
| 1343 |
%% This list of operators are used internally by various parts of the Ghostscript PDF interpreter. |
| 1344 |
%% Since each operator is a potential security vulnerability, and any operator listed here |
| 1345 |
%% is not required once the initislisation is complete and functions are bound, we undefine |
| 1346 |
%% the ones that aren't needed at runtime. |
| 1347 |
[ |
| 1348 |
/.setdistillerparams |
| 1349 |
] systemdict .undefinternalnames |
| 1350 |
|
| 1351 |
% The following are split out allowing control via ALLOWPSTRANSPARENCY command line param |
| 1352 |
% The examples/transparency_example.ps uses some of these (on the first line). |
| 1353 |
[ |
| 1354 |
/.pushpdf14devicefilter /.poppdf14devicefilter /.setstrokeconstantalpha /.setfillconstantalpha /.endtransparencygroup |
| 1355 |
/.currentblendmode /.currenttextknockout /.begintransparencytextgroup |
| 1356 |
/.endtransparencytextgroup /.begintransparencymaskgroup /.begintransparencymaskimage /.begintransparencypagegroup |
| 1357 |
/.endtransparencymask /.image3x /.abortpdf14devicefilter /.setstrokeconstantalpha /.setfillconstantalpha /.setalphaisshape /.currentalphaisshape |
| 1358 |
% undefining these causes errors/incorrect output |
| 1359 |
%/.setblendmode /.begintransparencygroup /.settextknockout /.setstrokeoverprint /.setfilloverprint |
| 1360 |
%/.currentstrokeoverprint /.currentfilloverprint /.currentfillconstantalpha /.currentstrokeconstantalpha |
| 1361 |
%/.setSMask /.currentSMask |
| 1362 |
|
| 1363 |
] systemdict dup /ALLOWPSTRANSPARENCY get {pop pop}{.undefinternalnames}ifelse |