Debian Codegraph @

Resource/Init/gs_setpd.ps

raw
1 % Copyright (C) 2001-2024 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 % The current implementation of setpagedevice has the following limitations:
17 % - It doesn't attempt to "interact with the user" for Policy = 2.
18
19 languagelevel 1 .setlanguagelevel
20 level2dict begin
21 % ---------------- Redefinitions ---------------- %
22
23 % Define interpreter callouts for handling gstate-saving operators,
24 % to make sure that they create a page device dictionary for use by
25 % the corresponding gstate-restoring operator.
26 % We'd really like to avoid the cost of doing this, but we don't see how.
27 % The names %gsavepagedevice, %savepagedevice, %gstatepagedevice,
28 % %copygstatepagedevice, and %currentgstatepagedevice are known to the
29 % interpreter.
30
31 (%gsavepagedevice) cvn
32 { currentpagedevice pop gsave
33 } .internalbind def
34
35 (%savepagedevice) cvn
36 { currentpagedevice pop save
37 } .internalbind def
38
39 (%gstatepagedevice) cvn
40 { currentpagedevice pop gstate
41 } .internalbind def
42
43 (%copygstatepagedevice) cvn
44 { currentpagedevice pop copy
45 } .internalbind def
46
47 (%currentgstatepagedevice) cvn
48 { currentpagedevice pop currentgstate
49 } .internalbind def
50
51 % Define interpreter callouts for handling gstate-restoring operators
52 % when the current page device needs to be changed.
53 % The names %grestorepagedevice, %grestoreallpagedevice,
54 % %restorepagedevice, %restore1pagedevice, and %setgstatepagedevice
55 % are known to the interpreter.
56
57 % ---------------- Keys and Attributes ---------------- %
58
59 % We have to deal specially with entries that the driver may change
60 % on its own. We also have to deal specially with parameters which the device may
61 % change on its own but which we *also* want to transmit to the device. Previously
62 % any parameter which was 'dynamic' would not be sent to the device, making it
63 % impossible to set a parameter, and later have the device change it. Currently
64 % only OutputICCProfile fits this category.
65 % This whole area is broken its completely the wrong way round from the way the spec says it should work.
66
67 % This dictionary contains the keys we never want to set.
68 /.readonlypdkeys mark
69 /.MediaSize dup % because it changes when PageSize is set
70 /PageCount dup
71 /Colors dup
72 /BitsPerPixel dup
73 /ColorValues dup
74 .dicttomark readonly def
75
76 % Bonkers, but needed by our ridiculous setpagedevice implementation. There are
77 % some keys (at the moment, RedValues, GreenValues and BlueValues are known) which
78 % only exist in the page device dictionary under some conditions (ProcessColorModel == DeviceRGB)
79 % If we change the conditions, so that these keys are no longer present in the params
80 % returned by the device, sending these keys to the device can trigger a fault.
81 % This is a problem because of our stored dictionary:
82 %
83 % 1) Set up the inital dictioanry by retrieving the params from the device
84 % 2) Change the conditions (ProcessColorModel == DeviceGray)
85 % 3) merge any volatile keys from the device. Note that RedValues etc no longer defined.
86 % 4) Call .installpagdevice, use the stored dicitonary to set the params
87 % 5) The stored RedValues etc, cause an error.
88 %
89 % The stored dictioanry is readonly (we use forceput to wedge new keys into it) so
90 % we can't 'undef' keys from it. (the dictionary is made readonly by the action of zsetpagedevice
91 % '.setpagedevice' in PostScrfipt)
92 %
93 % So the only solution is to have 'write only' keys. These can be written to the device
94 % but are not stored in the saved page device dictionary. This means PostScript programs
95 % can't interrogate and take action on these, but there's no solution to that except to
96 % rewrite this stuff completely so that it actually works properly.
97
98 /.writeonlykeys mark
99 /RedValues dup % Set by the device when ProcessColorModel changes
100 /GreenValues dup % Set by the device when ProcessColorModel changes
101 /BlueValues dup % Set by the device when ProcessColorModel changes
102 /GrayValues dup % Set by the device when ProcessColorModel changes
103 .dicttomark readonly def
104
105 % This dictionary contains the keys we always want to read back from the device.
106 /.volatilepdkeys mark
107 /.MediaSize dup % because it changes when PageSize is set
108 /RedValues dup % Set by the device when ProcessColorModel changes
109 /GreenValues dup % Set by the device when ProcessColorModel changes
110 /BlueValues dup % Set by the device when ProcessColorModel changes
111 /GrayValues dup % Set by the device when ProcessColorModel changes
112 /PageCount dup
113 /Colors dup
114 /BitsPerPixel dup
115 /ColorValues dup
116 /OutputICCProfile dup % ColorConversionStrategy can change this
117 .dicttomark readonly def
118
119 % The implementation of setpagedevice is quite complex. Currently,
120 % everything but the media matching algorithm is implemented here.
121
122 % By default, we only present the requested changes to the device,
123 % but there are some parameters that require special merging action.
124 % Define those parameters here, with the procedures that do the merging.
125 % The procedures are called as follows:
126 % <merged> <key> <new_value> -proc- <merged> <key> <new_value'>
127 /.mergespecial mark
128 /InputAttributes
129 { dup //null eq
130 { pop //null
131 }
132 { 3 copy pop .knownget
133 { dup //null eq
134 { pop dup length dict }
135 { dup length 2 index length add dict .copydict }
136 ifelse
137 }
138 { dup length dict
139 }
140 ifelse .copydict readonly
141 }
142 ifelse
143 } .internalbind
144 /OutputAttributes 1 index
145 /Policies
146 { 3 copy pop .knownget
147 { dup length 2 index length add dict .copydict }
148 { dup length dict }
149 ifelse copy readonly
150 } .internalbind
151 .dicttomark readonly def
152
153 % M. Sweet, Easy Software Products:
154 %
155 % Define NOMEDIAATTRS to turn off the default (but unimplementable) media
156 % selection policies for setpagedevice. This is used by CUPS to support
157 % the standard Adobe media attributes.
158 NOMEDIAATTRS {
159 % Define only PageSize for input attribute matching.
160 /.inputattrkeys [
161 /PageSize
162 ] readonly def
163 % Define no other keys used in media selection.
164 /.inputselectionkeys [
165 /noInputSelectionsKeys
166 ] readonly def
167
168 % Define no keys used in output attribute matching.
169 /.outputattrkeys [
170 /noOutputAttrKeys
171 ] readonly def
172 } {
173 % Define the keys used in input attribute matching.
174 /.inputattrkeys [
175 /PageSize /MediaColor /MediaWeight /MediaType /InsertSheet /ManualFeed
176 % The following are documented in Adobe's supplement for v2017.
177 /LeadingEdge /MediaClass
178 ] readonly def
179 % Define other keys used in media selection.
180 /.inputselectionkeys [
181 /MediaPosition /Orientation
182 ] readonly def
183
184 % Define the keys used in output attribute matching.
185 /.outputattrkeys [
186 /OutputType
187 ] readonly def
188 } ifelse
189
190 % Define all the parameters that should always be copied to the merged
191 % dictionary.
192 /.copiedkeys [
193 /SeparationColorNames
194 /OutputDevice
195 //.mergespecial { pop } forall
196 .inputattrkeys aload pop
197 .inputselectionkeys aload pop
198 .outputattrkeys aload pop
199 ] readonly def
200
201 % Define the parameters that should not be presented to the device.
202 % The procedures are called as follows:
203 % <merged> <key> <value> -proc-
204 % The procedure leaves all its operands on the stack and returns
205 % true iff the key/value pair should be presented to .putdeviceparams.
206 /.presentspecial mark
207 .readonlypdkeys
208 { pop //false }
209 forall
210 % We must ignore an explicit request for .MediaSize,
211 % because media matching always handles this.
212 /.MediaSize //false
213 /Name //false
214 /OutputDevice //false
215 /PageDeviceName //false
216 /PageOffset //false
217 /PageSize //false % obsolete alias for .MediaSize
218 /InputAttributes //false
219 .inputattrkeys
220 { dup dup /PageSize eq exch /LeadingEdge eq or
221 { pop }
222 { { 2 index /InputAttributes .knownget { //null eq } { //true } ifelse } }
223 ifelse
224 }
225 forall
226 .inputselectionkeys { //false } forall
227 /OutputAttributes //false
228 .outputattrkeys
229 { { 2 index /OutputAttributes .knownget { //null eq } { //true } ifelse } }
230 forall
231 /Install //false
232 /BeginPage //false
233 /EndPage //false
234 /Policies //false
235 % Our extensions:
236 /HWColorMap
237 { % HACK: don't transmit the color map, because
238 % window systems can change the color map on their own
239 % incrementally. Someday we'll have a better
240 % solution for this....
241 //false
242 }
243 /ViewerPreProcess //false
244 /ImagingBBox //false % This prevents the ImagingBBox value in the setpagedevice
245 % from affecting the device's ImagingBBox parameter, but
246 % does retain a 'shadow' copy at the PostScript level.
247 % This is done for Adobe compatibility since Adobe does
248 % render marks outside the ImagingBBox (and QuarkXpress
249 % relies on it).
250 .dicttomark readonly def
251
252 % ---------------- End Keys and Attributes ---------------- %
253
254 % Prepare to present parameters to the device, by spreading them onto the
255 % operand stack and removing any that shouldn't be presented.
256 /.prepareparams % <params> .prepareparams -mark- <key1> <value1> ...
257 { mark exch dup
258 { % Stack: -mark- key1 value1 ... merged key value
259 //.presentspecial 2 index .knownget
260 { exec { 3 -1 roll } { pop pop } ifelse }
261 { 3 -1 roll }
262 ifelse
263 }
264 forall pop
265 } .internalbind def
266
267 currentdict /.presentspecial .undef
268
269 % Compute the media size and initial matrix from a merged request (after
270 % media selection).
271 /.computemediasize % <request> .computemediasize
272 % <request> <matrix> <[width height]>
273 { dup /PageSize get % requested page size
274 1 index /InputAttributes get
275 2 index (%MediaSource) get get /PageSize get % media size
276 % (may be a range)
277 2 index /Policies get
278 dup /PageSize .knownget
279 { exch pop } { /PolicyNotFound get } ifelse % PageSize policy,
280 % affects scaling
281 3 index /Orientation .knownget not { //null } if
282 4 index /RollFedMedia .knownget not { //false } if
283 matrix .matchpagesize not {
284 % This is a "can't happen" condition!
285 /setpagedevice .systemvar /rangecheck signalerror
286 } if
287 2 array astore
288 } .internalbind def
289
290 % Try setting the device parameters from the merged request.
291 /.trysetparams % <merged> <(ignored)> <device> <Policies>
292 % .trysetparams
293 { //true 4 index //.prepareparams exec
294 % Add the computed .MediaSize.
295 % Stack: merged (ignored) device Policies -true-
296 % -mark- key1 value1 ...
297 counttomark 5 add index //.computemediasize exec
298 exch pop exch pop /.MediaSize exch
299 SETPDDEBUG { (Putting.) = pstack flush } if
300 .putdeviceparamsonly
301 SETPDDEBUG { (Result of putting.) = pstack flush } if
302 } .internalbind odef
303
304 /.installpagedevice
305 { % Since setpagedevice doesn't create new device objects,
306 % we must (carefully) reinstall the old parameters in
307 % the same device.
308 .currentpagedevice pop //null currentdevice //null
309 { //.trysetparams } //.internalstopped exec
310 {
311 //null
312 } if
313 dup type /booleantype eq
314 { pop pop }
315 {
316 SETPDDEBUG { (Error in .trysetparams!) = pstack flush } if
317 {cleartomark pop pop pop} //.internalstopped exec pop
318 % if resetting the entire device state failed, at least put back the
319 % security related key
320 currentdevice //null //false mark /.LockSafetyParams
321 currentpagedevice /.LockSafetyParams .knownget not
322 {systemdict /SAFER .knownget not {//false} } if
323 .putdeviceparamsonly
324 /.installpagedevice cvx /rangecheck signalerror
325 }
326 ifelse pop pop
327 % A careful reading of the Red Book reveals that an erasepage
328 % should occur, but *not* an initgraphics.
329 erasepage .beginpage
330 } .internalbind def
331
332 /.uninstallpagedevice
333 {
334 {2 .endpage { .currentnumcopies //false .outputpage } if} //.internalstopped exec pop
335 nulldevice
336 } .internalbind def
337
338 (%grestorepagedevice) cvn
339 {
340 .uninstallpagedevice
341 grestore
342 //.installpagedevice exec
343 } .internalbind def
344
345 (%grestoreallpagedevice) cvn
346 { .uninstallpagedevice grestore //.installpagedevice exec grestoreall
347 } .internalbind def
348
349 (%restore1pagedevice) cvn
350 { .uninstallpagedevice grestore //.installpagedevice exec restore
351 } .internalbind def
352
353 (%restorepagedevice) cvn
354 { .uninstallpagedevice restore //.installpagedevice exec
355 } .internalbind def
356
357 (%setgstatepagedevice) cvn
358 { .uninstallpagedevice setgstate //.installpagedevice exec
359 } .internalbind def
360
361 % Redefine .currentpagedevice and .setpagedevice so they convert between
362 % null and a fixed empty directionary.
363 /.nullpagedevice 0 dict readonly def
364 /.currentpagedevice {
365 //.currentpagedevice exch dup //null eq { pop //.nullpagedevice } if exch
366 } .internalbind odef
367 /.setpagedevice {
368 dup //.nullpagedevice eq { pop //null } if //.setpagedevice
369 } .internalbind odef
370
371 % ---------------- Auxiliary definitions ---------------- %
372
373 % Define the required attributes of all page devices, and their default values.
374 % We don't include attributes such as .MediaSize, which all devices
375 % are guaranteed to supply on their own.
376 /.defaultpolicies mark
377 % M. Sweet, Easy Software Products
378 %
379 % Due to the fact that it is not possible to properly implement
380 % the selection policies from a Ghostscript driver, we have changed
381 % the default policy to "7" (impose) to avoid numerous problems with
382 % printing within CUPS...
383 %
384 % If NOMEDIAATTRS is false, the set the default depending on whether
385 % PSFitPage is true. Policy 13 does best fit with page scaling up or down
386 % so it is only useful if FIXEDMEDIA is also specified, or if the set of
387 % media in the InputAttributes dictionary is the actual available choices
388 % and does not include any "range" page sizes.
389 /PageSize NOMEDIAATTRS { 7 } { //systemdict /PSFitPage known { 13 } { 0 } ifelse } ifelse
390 /PolicyNotFound 1
391 /PolicyReport {
392 dup /.LockSafetyParams known {
393 % Only possible error is invalidaccess
394 /setpagedevice .systemvar /invalidaccess signalerror
395 }
396 if
397 pop
398 } .internalbind
399 .dicttomark readonly def
400 % Note that the values of .requiredattrs are executed, not just fetched.
401 /.requiredattrs mark
402 /PageDeviceName //null
403 /PageOffset [0 0] readonly
404 % We populate InputAttributes with all of the known page sizes
405 % followed by a dummy media type that handles pages of any size.
406 % This will create some duplicates, but that only slightly slows
407 % down the media selection (loop is in zmedia2.c).
408 %
409 % Some PostScript creators assume that slot 0 is the default media
410 % size and some can't handle a non-standard 4-element array which
411 % is a 'range' type page size (always put last).
412 %
413 % Real Devices that can only handle specific page sizes will override this.
414 /InputAttributes {
415 mark
416 % First put the device's default page size in slot 0
417 % This satifies those that have devices built with a4 as the default
418 0 mark /PageSize /GetDeviceParam .special_op
419 not {/setpagedevice .systemvar /configurationerror signalerror} if .dicttomark
420 % Only populate the other entries if we aren't FIXEDMEDIA
421 FIXEDMEDIA not {
422 statusdict /.pagetypenames get {
423 statusdict /.pagetypeprocs get
424 exch get 0 2 getinterval cvlit
425 counttomark 1 sub 2 idiv exch mark exch /PageSize exch
426 % stack: mark --dict-- --dict-- ... key mark /PageSize [x y]
427 % see note above about pagetype executable array contents.
428 .dicttomark
429 } forall
430 % If NORANGEPAGESIZE is defined, (-dNORANGEPAGESIZE), then don't add
431 % the 'match any' PageSize entry
432 systemdict /NORANGEPAGESIZE known not {
433 % Add one last entry which is the 4 element range array (non-standard)
434 counttomark 2 idiv
435 % PageSize with either dimension 0 will be detected in
436 % match_page_size, so we can allow it here
437 mark /PageSize [0 dup 16#7ffff dup] .dicttomark
438 } if
439 } if % FIXEDMEDIA false
440 .dicttomark
441 }
442 (%MediaSource) 0
443 /OutputAttributes {
444 mark 0 mark .dicttomark readonly .dicttomark
445 }
446 (%MediaDestination) 0
447 /Install {{.callinstall}} .internalbind
448 /BeginPage {{.callbeginpage}} .internalbind
449 /EndPage {{.callendpage}} .internalbind
450 /Policies .defaultpolicies
451 /ImagingBBox //null % default value
452 /UseCIEColor /.getuseciecolor load
453 .dicttomark readonly def
454
455 % Define currentpagedevice so it creates the dictionary on demand if needed,
456 % adding all the required entries defined just above.
457
458 /.makecurrentpagedevice { % - .makecurrentpagedevice <dict>
459 currentdevice //null .getdeviceparams
460 % Make the dictionary large enough to add defaulted entries.
461 counttomark 2 idiv //.requiredattrs length add dict
462 counttomark 2 idiv { dup 4 2 roll put } repeat exch pop
463 % Add any missing required attributes.
464 % Make a writable and (if possible) local copy of any default
465 % dictionaries, to work around a bug in the output of WordPerfect,
466 % which assumes that these dictionaries are writable and local.
467 .currentglobal exch dup gcheck .setglobal
468 //.requiredattrs {
469 2 index 2 index known {
470 1 index /Policies eq {
471 % Merge policies from the device driver with defaults
472 2 index % <<>> /key value <<>>
473 3 2 roll get % <<>> value <<policies>>
474 exch {
475 2 index 2 index known {
476 pop pop
477 } {
478 2 index 3 1 roll put
479 } ifelse
480 } forall
481 pop
482 } {
483 pop pop
484 } ifelse
485 } {
486 exec 2 index 3 1 roll put
487 } ifelse
488 } forall exch .setglobal
489
490 % Remove any keys we don't want to be stored, before .setpagedevice
491 % makes the dictionary read only
492 .writeonlykeys {2 index exch undef pop} forall
493 dup .setpagedevice
494 } .internalbind def
495
496 % Copy a dictionary recursively.
497 /.copytree { % <dict> .copytree <dict'>
498 dup length dict exch {
499 dup type /dicttype eq { .copytree } if 2 index 3 1 roll put
500 } forall
501 } def
502
503 /currentpagedevice {
504 .currentpagedevice {
505 dup length 0 eq {
506 pop //.makecurrentpagedevice exec
507 } {
508 % If any of the dynamic keys have changed,
509 % we must update the page device dictionary.
510 currentdevice //.volatilepdkeys .getdeviceparams .dicttomark {
511 % Stack: current key value
512 2 index 2 index .knownget { 1 index ne } { //true } ifelse
513 { 2 index wcheck not
514 { % This is the first entry being updated.
515 % Copy the dictionary to make it writable.
516 3 -1 roll
517 currentglobal 1 index dup gcheck currentglobal and setglobal
518 length dict
519 exch setglobal
520 .copydict
521 3 1 roll
522 }
523 if
524 2 index 3 1 roll put
525 }
526 { pop pop
527 }
528 ifelse
529 } forall
530 % If the device is the distiller device, update distillerparams that
531 % may have been changed by setdistillerparams
532 /IsDistiller /GetDeviceParam .special_op {
533 exch pop
534 }{
535 //false
536 }ifelse
537 {
538 currentdistillerparams {
539 % Stack: current key value
540 2 index 2 index .knownget { 1 index ne } { //true } ifelse
541 { 2 index 3 1 roll put } { pop pop } ifelse
542 } forall
543 } if
544 % If the dictionary was global and is now local, copy
545 % any global subsidiary dictionaries to local VM. This
546 % too is to work around the Word Perfect bug (see above).
547 dup gcheck not {
548 dup {
549 dup type /dicttype eq { dup gcheck } { //false } ifelse {
550 % Copy-on-write, see above.
551 2 index wcheck not {
552 3 -1 roll dup length dict .copydict
553 3 1 roll
554 } if
555 //.copytree exec 2 index 3 1 roll put
556 } {
557 pop pop
558 } ifelse
559 } forall
560 } if
561 % We would like to do a .setpagedevice so we don't keep
562 % re-creating the dictionary. Unfortunately, the effect
563 % of this is that if any dynamic key changes (PageCount
564 % in particular), we will do the equivalent of a
565 % setpagedevice at the next restore or grestore.
566 % Therefore, we make the dictionary read-only, but
567 % we don't store it away. I.e., NOT:
568 % dup wcheck { .setpagedevice .currentpagedevice pop } if
569 readonly
570 } ifelse
571 } if
572 } .internalbind odef
573
574 % Define access to device defaults.
575 /.defaultdeviceparams
576 { finddevice //null .getdeviceparams
577 } .internalbind def
578
579 % Select media (input or output). The hard work is done in an operator:
580 % <pagedict> <attrdict> <policydict> <keys> .matchmedia <key> true
581 % <pagedict> <attrdict> <policydict> <keys> .matchmedia false
582 % <pagedict> null <policydict> <keys> .matchmedia null true
583 /.selectmedia % <orig> <request> <merged> <failed> <-- retained
584 % <attrdict> <policydict> <attrkeys> <mediakey>
585 % .selectmedia
586 { 5 index 5 -2 roll 4 index .matchmedia
587 % Stack: orig request merged failed attrkeys mediakey
588 % (key true | false)
589 { 4 index 3 1 roll put pop
590 }
591 { % Adobe's implementations have a "big hairy heuristic"
592 % to choose the set of keys to report as having failed the match.
593 % For the moment, we report any keys that are in the request
594 % and don't have the same value as in the original dictionary.
595 5 index 1 index .knownget
596 { 4 index 3 1 roll put }
597 { 3 index exch .undef }
598 ifelse
599 { % Stack: <orig> <request> <merged> <failed> <attrkey>
600 3 index 1 index .knownget
601 { 5 index 2 index .knownget { ne } { pop //true } ifelse }
602 { //false }
603 ifelse % Stack: ... <failed> <attrkey> <report>
604 { 2 copy /rangecheck put }
605 if pop
606 }
607 forall
608 }
609 ifelse
610 } .internalbind def
611
612 % Apply Policies to any unprocessed failed requests.
613 % As we process each request entry, we replace the error name
614 % in the <failed> dictionary with the policy value,
615 % and we replace the key in the <merged> dictionary with its prior value
616 % (or remove it if it had no prior value).
617
618 % These procedures are called with the following on the stack:
619 % <orig> <merged> <failed> <Policies> <key> <policy>
620 % They are expected to consume the top 2 operands.
621 % NOTE: we currently treat all values other than 0, 1, or 7 (for PageSize)
622 % the same as 0, i.e., we signal an error.
623 /0Policy { % Set errorinfo and signal a configurationerror.
624 NOMEDIAATTRS {
625 % NOMEDIAATTRS means that the default policy is 7...
626 pop 2 index exch 7 put
627 } {
628 pop dup 4 index exch get 2 array astore
629 $error /errorinfo 3 -1 roll put
630 cleartomark
631 /setpagedevice .systemvar /configurationerror signalerror
632 } ifelse
633 } .internalbind odef
634
635 % Making this an operator means we can properly hide
636 % the contents - specifically .forceput
637 /1Policy
638 {
639 % Roll back the failed request to its previous status.
640 SETPDDEBUG { (Rolling back.) = pstack flush } if
641 3 index 2 index 3 -1 roll .forceput
642 4 index 1 index .knownget
643 { 4 index 3 1 roll .forceput } executeonly
644 { 3 index exch .undef }
645 ifelse
646 } .internalbind odef
647
648 /7Policy { % For PageSize only, just impose the request.
649 1 index /PageSize eq
650 { pop pop 1 index /PageSize 7 put }
651 { .policyprocs 0 get exec }
652 ifelse
653 } .internalbind odef
654
655 /.applypolicies % <orig> <merged> <failed> .applypolicies
656 % <orig> <merged'> <failed'>
657 {
658 1 index /Policies get 1 index
659 { type /integertype eq
660 {
661 pop % already processed
662 }{
663 2 copy .knownget not { 1 index /PolicyNotFound get } if
664 % Stack: <orig> <merged> <failed> <Policies> <key>
665 % <policy>
666 dup 1 eq {
667 1Policy
668 }{
669 dup 7 eq {
670 7Policy
671 }{
672 0Policy
673 } ifelse
674 } ifelse
675 } ifelse
676 }
677 forall pop
678 } .forcebind odef
679
680 currentdict /0Policy undef
681 currentdict /1Policy undef
682 currentdict /7Policy undef
683
684 % Put device parameters without resetting currentpagedevice.
685 % (.putdeviceparams clears the current page device.)
686 /.putdeviceparamsonly % <device> <Policies|null> <require_all> -mark-
687 % <key1> <value1> ... .putdeviceparamsonly
688 % On success: <device> <eraseflag>
689 % On failure: <device> <Policies|null> <req_all> -mark-
690 % <key1> <error1> ...
691 { .currentpagedevice
692 { counttomark 4 add 1 roll .putdeviceparams
693 dup type /booleantype eq { 3 } { counttomark 5 add } ifelse -1 roll
694 .setpagedevice
695 }
696 { pop .putdeviceparams
697 }
698 ifelse
699 } .internalbind def
700
701
702 /.postinstall { % mark ... <failed> <merged> .postinstall -
703 matrix currentmatrix .setdefaultmatrix
704 % Erase and initialize the page.
705 initgraphics
706 currentoverprint //false setoverprint 1 setcolor
707 .fillpage
708 0 setcolor setoverprint
709 .beginpage
710
711 % Clean up, calling PolicyReport if needed.
712 % Stack: mark ... <failed> <merged>
713 SETPDDEBUG { (Finishing.) = pstack flush } if
714
715 exch dup length 0 ne
716 { 1 index /Policies get /PolicyReport get
717 counttomark 1 add 2 roll cleartomark
718 exec
719 }
720 { cleartomark
721 }
722 ifelse pop
723
724 } def
725
726 % ---------------- setpagedevice itself ---------------- %
727
728 /setpagedevice
729 {
730 % To avoid VM mismatches caused by copying subsidiary
731 % dictionaries to local VM (see WorPerfect bug in
732 % .makecurrentpagedevice) we want to make the dict
733 % returned by currentpagedevice local. However, if we
734 % run with -dSAFER we get a call to setpagedevice from
735 % .setsafe in gs_init.ps during startup. The dict returned
736 % by currentpagdevice is stored to the graphics state by
737 % .setpagedevice below, and returned by currentpagdevice.
738 % The Display PostScript code insists that the savedinitialgstate
739 % not have any pointers to local VM objects, so if we simply
740 % make the dict local then we fail in gs_dps.ps. The only
741 % solution is to make sure the VM mode is global during
742 % startup (to satisfy gs_dps.ps) and local thereafter
743 % (to satisfy the WordPerfect bug).
744 dup /..StartupGlobal known
745 {
746 currentglobal exch //true setglobal
747 dup /..StartupGlobal undef
748 } {
749 % ensure that we are always in local VM mode to avoid
750 % mismatches. This is because we always create child
751 % dictionaries in local VM, regardless of the current VM state,
752 % (see .makecurrentpagdevice) and we can't store local objects
753 % in a global object, so we must ensure teh dictionary returned
754 % from currentpagedevice is in local VM.
755 currentglobal exch //false setglobal
756 } ifelse
757
758 %% We used to execute endpage after .tsrysetparams, but that actually alters
759 %% the page device dictionary (in particular /PageSize) this is not correct.
760 %% Testing with Adobe Acrobat Distiller shows that EndPage is ececuted if the
761 %% page device dictionary is empty, and indeed even if setpagedevice returns
762 %% an error (caught by stopped), so it seems pretty clear that we should
763 %% run any required EndPage very early in the setpagedevice process.
764 %% Bug 690667.
765 2 .endpage
766 { 1 //true .outputpage
767 (>>setpagedevice, press <return> to continue<<\n) //.confirm exec
768 }
769 if
770 % We mustn't pop the argument until the very end,
771 % so that the pseudo-operator machinery can restore the stack
772 % if an error occurs.
773 mark 1 index currentpagedevice
774
775 % Check whether we are changing OutputDevice;
776 % also handle the case where the current device
777 % is not a page device.
778 % Stack: mark <request> <current>
779 SETPDDEBUG { (Checking.) = pstack flush } if
780
781 dup /OutputDevice .knownget
782 { % Current device is a page device.
783 2 index /OutputDevice .knownget
784 { % A specific OutputDevice was requested.
785 2 copy eq
786 { pop pop //null }
787 { exch pop }
788 ifelse
789 }
790 { pop //null
791 }
792 ifelse
793 }
794 { % Current device is not a page device.
795 % Use the default device.
796 1 index /OutputDevice .knownget not { .defaultdevicename } if
797 }
798 ifelse
799 dup //null eq
800 { pop
801 }
802 { exch pop //.defaultdeviceparams exec
803 % In case of duplicate keys, .dicttomark takes the entry
804 % lower on the stack, so we can just append the defaults here.
805 //.requiredattrs { exec } forall .dicttomark
806 }
807 ifelse
808
809 % Check whether a viewer wants to intervene.
810 % We must check both the request (which takes precedence)
811 % and the current dictionary.
812 % Stack: mark <request> <orig>
813 exch dup /ViewerPreProcess .knownget
814 { exec }
815 { 1 index /ViewerPreProcess .knownget { exec } if }
816 ifelse exch
817
818 % Construct a merged request from the actual request plus
819 % any keys that should always be propagated.
820 % Stack: mark <request> <orig>
821 SETPDDEBUG { (Merging.) = pstack flush } if
822
823 exch 1 index length 1 index length add dict
824 .copiedkeys
825 { % Stack: <orig> <request> <merged> <key>
826 3 index 1 index .knownget { 3 copy put pop } if pop
827 }
828 forall
829 % Stack: <orig> <request> <merged>
830 dup 2 index
831 { % stack: <orig> <request> <merged> <merged> <rkey> <rvalue>
832 //.mergespecial 2 index .knownget { exec } if
833 put dup
834 }
835 forall pop
836 % Hack: if FIXEDRESOLUTION is true, discard any attempt to
837 % change HWResolution.
838 FIXEDRESOLUTION { dup /HWResolution .undef } if
839 % Hack: if FIXEDMEDIA is true, discard any attempt to change
840 % PageSize or HWSize unless the PageSize Policy 13 (for FitPage).
841 dup /Policies get /PageSize get 13 ne FIXEDMEDIA and
842 { dup /PageSize 4 index /PageSize get put
843 dup /HWSize 4 index /HWSize get put
844 } if
845 % Hack: to work around some files that take a PageSize
846 % from InputAttributes and impose it, discard any attempt
847 % to set PageSize to a 4-element value.
848 % Stack: mark <orig> <request> <merged>
849 dup /PageSize .knownget {
850 length 2 ne {
851 dup /PageSize 4 index /PageSize get put
852 } if
853 } if
854
855 % Select input and output media.
856 % Stack: mark <orig> <request> <merged>
857 SETPDDEBUG { (Selecting.) = pstack flush } if
858
859 0 dict % <failed>
860 1 index /InputAttributes .knownget
861 { 2 index /Policies get
862 .inputattrkeys (%MediaSource) cvn //.selectmedia exec
863 } if
864 1 index /OutputAttributes .knownget
865 { 2 index /Policies get
866 .outputattrkeys (%MediaDestination) cvn //.selectmedia exec
867 } if
868 3 -1 roll 4 1 roll % temporarily swap orig & request
869 .applypolicies
870 3 -1 roll 4 1 roll % swap back
871
872 % Construct the new device, and attempt to set its attributes.
873 % Stack: mark <orig> <request> <merged> <failed>
874 SETPDDEBUG { (Constructing.) = pstack flush } if
875
876 % Non-obvious: we need to check the name of the output device, to tell
877 % whether we're going to have to replace the entire device chain (which
878 % may be only one device, or may be multiple devices.
879 % If we're not replacing the entire change, we have to use the device in
880 % the graphics state, so the configuration of the entire device chain is
881 % correctly set.
882 .currentoutputdevice .devicename 2 index /OutputDevice get eq
883 { currentdevice }
884 { 1 index /OutputDevice get finddevice }
885 ifelse
886 %**************** We should copy the device here,
887 %**************** but since we can't close the old device,
888 %**************** we don't. This is WRONG.
889 %****************copydevice
890 2 index /Policies get
891 //.trysetparams
892 dup type /booleantype ne
893 { % The request failed.
894 % Stack: ... <orig> <request> <merged> <failed> <device>
895 % <Policies> true mark <name> <errorname> ...
896 SETPDDEBUG { (Recovering.) = pstack flush } if
897 counttomark 4 add index
898 counttomark 2 idiv { dup 4 -2 roll put } repeat
899 pop pop pop
900 % Stack: mark ... <orig> <request> <merged> <failed> <device>
901 % <Policies>
902 6 2 roll 3 -1 roll 4 1 roll
903 .applypolicies
904 3 -1 roll 4 1 roll 6 -2 roll
905 //.trysetparams % shouldn't fail!
906 dup type /booleantype ne
907 { 2 { counttomark 1 add 1 roll cleartomark } repeat
908 /setpagedevice .systemvar exch signalerror
909 }
910 if
911 }
912 if
913
914 % The attempt succeeded. Install the new device.
915 % Stack: mark ... <merged> <failed> <device> <eraseflag>
916 SETPDDEBUG { (Installing.) = pstack flush } if
917
918 pop
919 % .setdevice clears the current page device!
920 .currentpagedevice pop exch
921 { .setdevice } stopped {
922 cleartomark exch pop
923 /setpagedevice cvx $error /errorname get
924 signalerror
925 } if
926 pop
927 .setpagedevice
928
929 % Implement UseCIEColor directly if this is a LL3 system.
930 % The color substitution feature is now implemented in
931 % the interpreter, and this is used as an optimization.
932 %
933 % NB: This shoud be the only use of the .setuseciecolor
934 % operator anywhere.
935 %
936 % Set some color space other than /DeviceGray, to insure
937 % that initgraphics will actually perform a setcolorspace
938 % operation (there is an optimization in setcolorspace
939 % that does nothing if the operand and current color
940 % spaces are the same)
941
942 /.setuseciecolor where
943 {
944 pop 1 index /UseCIEColor .knownget
945 {
946 .setuseciecolor /DeviceRGB setcolorspace
947 }
948 if
949 }
950 if
951
952 % Merge the request into the current page device,
953 % unless we're changing the OutputDevice.
954 % Stack: mark ... <merged> <failed>
955 exch currentpagedevice dup length 2 index length add dict
956 % Stack: mark ... <failed> <merged> <current> <newdict>
957 2 index /OutputDevice .knownget {
958 2 index /OutputDevice .knownget not { //null } if eq
959 } {
960 //true
961 } ifelse {
962 % Same OutputDevice, merge the dictionaries.
963 .copydict
964 } {
965 % Different OutputDevice, discard the old dictionary.
966 exch pop
967 } ifelse .copydict
968 % Initialize the default matrix, taking media matching
969 % into account.
970 //.computemediasize exec pop initmatrix concat
971 dup /PageOffset .knownget
972 { % Translate by the given number of 1/72" units in device X/Y.
973 dup 0 get exch 1 get
974 2 index /HWResolution get dup 1 get exch 0 get
975 4 -1 roll mul 72 div 3 1 roll mul 72 div
976 idtransform translate
977 }
978 if
979 % We must install the new page device dictionary
980 % before calling the Install procedure.
981 dup .setpagedevice
982 /HighLevelDevice /GetDeviceParam .special_op {
983 exch pop not
984 }{
985 //true
986 }ifelse
987 {
988 .setdefaulthalftone % Set the default screen before calling Install.
989 } if
990 dup /Install .knownget {
991 { .execinstall } stopped {
992 pop % Install procedure failed. One element will have been left on the stack.
993 % stack: mark <orig> <request> <failed> <merged>
994 1 index /Install $error /errorname get put % Put it in the "failed" dict
995 % .applypolicies needs stack: <orig> <merged> <failed>
996 exch 4 2 roll exch 4 2 roll .applypolicies exch 4 2 roll exch 4 2 roll
997 % Now execute the old Install -- failures after this are not handled
998 dup /Install .knownget { { .execinstall } stopped { pop } if } if
999 //.postinstall exec stop
1000 } {
1001 //.postinstall exec
1002 } ifelse
1003 } {
1004 //.postinstall exec
1005 } ifelse
1006 setglobal % return to original VM allocation mode
1007 } .forcebind odef
1008
1009 % We break out the code after calling the Install procedure into a
1010 % separate procedure, since it is executed even if Install causes an error.
1011 % By making .execinstall a separate operator procedure, we get the stacks
1012 % mostly restored if it fails, except for one element (the operand).
1013 % Thus if it fails, there will be one element left on the op stack.
1014
1015 /.execinstall { % <proc> .execinstall -
1016 dup % element left on the stack if the exec fails.
1017
1018 % Because the interpreter optimizes tail calls, we can't just let
1019 % the body of this procedure be 'exec', because that would lose
1020 % the stack protection that is the whole reason for having the
1021 % procedure in the first place. The 'pop' for the dummy element
1022 % on the op stack suffices.
1023 exec
1024 pop % See above.
1025 } odef
1026
1027 [
1028 /.computemediasize
1029 /.prepareparams
1030 /.selectmedia
1031 /.trysetparams
1032 /.installpagedevice
1033 /.postinstall
1034 /.defaultdeviceparams
1035 /.makecurrentpagedevice
1036 /.mergespecial
1037 /.requiredattrs
1038 /.applypolicies
1039 % /.copytree
1040 % /.uninstallpagedevice % (called from C code)
1041 ] dup currentdict .undefinternalnames
1042 systemdict .undefinternalnames
1043
1044 end % level2dict
1045 .setlanguagelevel