Debian Codegraph @

Resource/Init/gs_ll3.ps

raw
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 % Initialization file for PostScript LanguageLevel 3 functions.
17 % This file must be loaded after gs_lev2.ps and gs_res.ps.
18 % These definitions go into ll3dict or various ProcSets.
19 % NOTE: the interpreter creates ll3dict.
20
21 ll3dict begin
22
23 % We need LanguageLevel 2 or higher in order to have setuserparams and
24 % defineresource.
25 languagelevel dup 2 .max .setlanguagelevel
26
27 % ------ Idiom recognition ------ %
28
29 /IdiomRecognition //false .definepsuserparam
30
31 % Modify `bind' to apply idiom recognition afterwards.
32 /.bindscratch 128 string def
33 % Note that since this definition of `bind' may get bound in,
34 % it has to function properly even at lower language levels,
35 % where IdiomRecognition may not be defined.
36 /.bind_ { % <proc> .bind <proc'>
37 //.bind currentuserparams /IdiomRecognition
38 .knownget not { //false } if
39 {
40 //false
41 /IdiomSet //.findcategory exec .Instances
42 {
43 exch pop % pop the IdiomSet identifying key. Stack /proc /false [-dict- status size]
44 0 get % get resource instance, a dictionary. Stack : /proc false -dict-
45 {
46 exch pop % pop the instance identifying key
47 % Stack: proc //false [template substitute]
48 dup 1 get exch 0 get % Stack: proc //false substitute template
49 3 index .eqproc {
50 % Stack: proc //false substitute
51 3 -1 roll pop exch not % Stack: substitute true
52 } {
53 pop % Stack: proc //false
54 } ifelse
55 } forall
56 dup {exit} if % Stack proc <bool>
57 } forall
58
59 not % Not matched in global resources
60 currentglobal not and % must not match local resources if currentglobal is true
61 {
62 //false
63 .LocalInstances
64 {
65 exch pop % pop IdiomSet identifying key. Stack /proc /false [-dict- status size]
66 0 get % get resource instance, a dictionary Stack : /proc false -dict-
67 {
68 exch pop % pop instance identifier key
69 % Stack: proc //false [template substitute]
70 dup 1 get exch 0 get % Stack: proc //false substitute template
71 3 index .eqproc {
72 % Stack: proc //false substitute
73 2 index gcheck % Stack: proc //false substitute _global?_
74 1 index gcheck not and % Stack: proc //false substitute _global?_ _global?_
75 { % must not match if candidate in global VM and substitute in local
76 pop % Stack: proc //false
77 } {
78 % Stack: proc //false substitute
79 3 -1 roll pop exch not % Stack: substitute true
80 }ifelse
81 } {
82 pop % Stack: proc //false
83 } ifelse
84 } forall
85 dup {exit} if
86 } forall
87 pop % Stack: proc/substitute
88 } if
89 end % .findcategory makes the category dictionary the current dict, so we must pop it from dict stack
90 } if
91 } def
92 /.bind /.bind_ load odef % note that .bind_ will get bound
93 currentdict /.bindscratch .undef
94
95 DELAYBIND not {
96 % We can't just do
97 % /bind /.bind load def
98 % because then /bind load == prints --.bind-- .
99 /bind {//.bind_ exec} odef
100 } if
101 currentdict /.bind_ undef
102
103 /.readbytestring { % <source> <bytestring> .readbytestring
104 % <bytestring> <filled>
105 % Note that since bytestrings don't implement getinterval,
106 % if filled is false, there is no way to tell how much
107 % was read.
108 //true exch 0 1 2 index length 1 sub {
109 % Stack: source true str index
110 3 index read not { pop exch not exch exit } if
111 3 copy put pop pop
112 } for 3 -1 roll pop exch
113 } .internalbind def
114
115 /.bigstring { % <size> .bigstring <string|bytestring>
116 count 1 lt { /.bigstring cvx /stackunderflow signalerror } if
117 dup type /integertype ne {
118 /.bigstring cvx /typecheck signalerror
119 } if
120 dup 65400 gt { .bytestring } { string } ifelse
121 } .internalbind def
122
123 % ------ HalftoneTypes 6, 10, 16 and HalftoneMode ------ %
124
125 % This code depends on an internal HalftoneType 7 with the following keys:
126 % Width, Height, Width2, Height2, TransferFunction:
127 % as for HalftoneType 16.
128 % Thresholds: a string or bytestring holding the thresholds,
129 % (Width x Height + Width2 x Height2) x BitsPerSample / 8 bytes,
130 % as for HalftoneType 16 except that the samples may be either
131 % 8 or 16 bits wide.
132 % BitsPerSample: 8 or 16.
133
134 % Note that this HalftoneType never appears in halftone dictionaries given
135 % to sethalftone, only as a component in those given to .sethalftone5,
136 % so its numeric value can be chosen ad lib as long as it differs from the
137 % other values that are legal in component dictionaries for .sethalftone5
138 % (currently only 1 and 3).
139
140 /.makehalftone7 { % <dict> <dict> <source> <Width> <Height>
141 % (<Width2> <Height2> | null) <BPS> .makehalftone7
142 % <setdict> <dict5> { .sethalftone5 }
143 8 dict begin
144 /HalftoneType 7 def
145 /BitsPerSample exch def
146 dup //null eq {
147 pop 0
148 } {
149 /Height2 1 index def /Width2 2 index def mul
150 } ifelse 3 1 roll
151 /Height 1 index def
152 /Width 2 index def
153 mul add BitsPerSample 8 idiv mul //.bigstring exec
154 % Stack: dict dict source str
155 dup type /stringtype eq { readstring } { //.readbytestring exec} ifelse
156 not { /sethalftone .systemvar /rangecheck signalerror exit } if
157 readonly /Thresholds exch def
158 /TransferFunction .knownget { /TransferFunction exch def } if
159 % If the original Thresholds was a file, replace it with
160 % a new one.
161 dup /Thresholds get type /filetype eq {
162 dup /Thresholds [ Thresholds ] cvx 0 () /SubFileDecode filter put
163 } if
164 mark /HalftoneType 5 /Default currentdict end .dicttomark
165 { .sethalftone5 }
166 } .internalbind def
167
168 /.sethalftone6 { % <dict> <dict> .sethalftone6 <setdict> <dict5>
169 % { .sethalftone5 }
170 % Keys: Width, Height, Thresholds, T'Function
171 dup /Thresholds get
172 1 index /Width get 2 index /Height get
173 //null 8 //.makehalftone7 exec
174 } .internalbind def
175
176 /.sethalftone10 { % <dict> <dict> .sethalftone10 <setdict> <dict5>
177 % { .sethalftone5 }
178 % Keys: Xsquare, Ysquare, Thresholds, T'Function
179 % Note that this is the only one of these three HalftoneTypes
180 % that allows either a file or a string for Thresholds.
181 dup /Thresholds get dup type /stringtype eq { 0 () /SubFileDecode filter } if
182 1 index /Xsquare get dup 3 index /Ysquare get dup
183 8 //.makehalftone7 exec
184 } .internalbind def
185
186 /.sethalftone16 { % <dict> <dict> .sethalftone16 <setdict> <dict5>
187 % { .sethalftone5 }
188 % Keys: Width, Height, Width2, Height2,
189 % Thresholds, T'Function
190 dup /Thresholds get
191 1 index /Width get 2 index /Height get
192 3 index /Width2 .knownget { % 2-rectangle case
193 4 index /Height2 get
194 } { % 1-rectangle case
195 //null
196 } ifelse 16 //.makehalftone7 exec
197 } .internalbind def
198
199 .halftonetypes begin
200 6 //.sethalftone6 def
201 10 //.sethalftone10 def
202 16 //.sethalftone16 def
203 end
204
205 % Redefine the halftone-setting operators to honor HalftoneMode.
206 /setcolorscreen {
207 /HalftoneMode getuserparam 0 eq {
208 //setcolorscreen
209 } {
210 12 { pop } repeat //.getdefaulthalftone exec
211 currentglobal
212 true setglobal
213 { //sethalftone }
214 { .setdefaulthalftone }
215 ifelse
216 setglobal
217 } ifelse
218 } odef
219 /setscreen {
220 /HalftoneMode getuserparam 0 eq {
221 //setscreen
222 } {
223 pop pop pop //.getdefaulthalftone exec
224 currentglobal
225 true setglobal
226 { //sethalftone }
227 { .setdefaulthalftone }
228 ifelse
229 setglobal
230 } ifelse
231 } odef
232 /sethalftone {
233 /HalftoneMode getuserparam 0 eq {
234 //sethalftone
235 } {
236 pop
237 currentglobal
238 true setglobal
239 //.getdefaulthalftone exec
240 { //sethalftone }
241 { .setdefaulthalftone }
242 ifelse
243 setglobal
244 } ifelse
245 } odef
246
247 % ------ ImageTypes 3 and 4 (masked images) ------ %
248
249 % Put the new data source in the requested dictionary.
250 % Copy the updated dictionary because it nay be used elsewhere.
251 % <dict> <data> <name> update_image3 <dict'>
252 /update_image3 {
253 3 -1 roll dup length dict copy dup % [data] /name <<image>> <<image>>
254 2 index get % [data] /name <<image>> <<sub-image>>
255 dup length dict copy dup % [data] /name <<image>> <<sub-image>> <<sub-image>>
256 /DataSource 6 -1 roll put % /name <<image>> <<sub-image>>
257 exch dup % /name <<sub-image>> <<image>> <<image>>
258 4 2 roll % <<image>> <<image>> /name <<sub-image>>
259 put % <<image>>
260 } .internalbind def
261
262 % Copy mask data source to a reusable stream
263 % <dict> <source> buffer_mask <dict> <file>
264 /buffer_mask {
265 1 index /MaskDict get
266 dup /Width get
267 1 index /BitsPerComponent get mul 7 add 8 idiv
268 exch /Height get mul () /SubFileDecode filter
269 /ReusableStreamDecode filter
270 } .internalbind def
271
272 % Data type vs. mask type decission table.
273 % All procedures have the signature:
274 % <image dict> <data DataSource> <mask DataSource> proc <image dict'>
275 /data_mask_dict mark
276 /stringtype mark % data is a string
277 /stringtype { pop pop } bind
278 /arraytype { % convert data to proc, don't buffer mask
279 pop
280 dup type dup /arraytype eq exch /packedarraytype eq or {
281 [ exch { 1 array astore cvx } forall ]
282 } {
283 1 array astore cvx
284 } ifelse
285 /DataDict //update_image3 exec
286 } bind
287 /packedarraytype 1 index
288 /filetype { % convert data to file, don't buffer mask
289 pop
290 dup type dup /arraytype eq exch /packedarraytype eq or {
291 [ exch { 1 array astore cvx 0 () /SubFileDecode filter } forall ]
292 } {
293 1 array astore cvx 0 () /SubFileDecode filter
294 } ifelse
295 /DataDict //update_image3 exec
296 } .internalbind
297 .dicttomark readonly
298 /arraytype mark % data is a proc
299 /stringtype { % convert mask to proc
300 exch pop
301 1 array astore cvx
302 /MaskDict //update_image3 exec
303 } .internalbind
304 /arraytype { % buffer mask in reusable stream and convert back to proc
305 exch pop
306 //buffer_mask exec
307 500 string
308 { readstring pop } aload pop
309 4 packedarray cvx
310 /MaskDict //update_image3 exec
311 } .internalbind
312 /packedarraytype 1 index
313 /filetype 1 index
314 .dicttomark readonly
315 /packedarraytype 1 index
316 /filetype mark % data is a file
317 /stringtype { % convert mask to file
318 exch pop
319 1 array astore cvx 0 () /SubFileDecode filter
320 /MaskDict //update_image3 exec
321 } .internalbind
322 /arraytype { % buffer the mask and convert to file
323 exch pop
324 //buffer_mask exec
325 /MaskDict //update_image3 exec
326 } .internalbind
327 /packedarraytype 1 index
328 /filetype {
329 //systemdict /userdict get /PDFTopSave known {
330 pop pop % mask is already buffered by PDF interpreter
331 } {
332 exch pop % buffer the mask
333 //buffer_mask exec
334 /MaskDict //update_image3 exec
335 } ifelse
336 } .internalbind
337 .dicttomark readonly
338 .dicttomark readonly def
339
340 .imagetypes
341 dup 3 {
342 dup /InterleaveType get 3 eq {
343 % .image3 requires data sources of the image and mask to be of the
344 % same type. Work around this restriction here.
345 dup /DataDict get /DataSource get
346 1 index /MaskDict get /DataSource get % <<>> data mask
347 dup type % <<>> data mask mask_type
348 2 index
349 4 index /DataDict get /MultipleDataSources .knownget {
350 { 0 get } if
351 } if
352 type % <<>> data mask mask_type data_type
353 //data_mask_dict exch .knownget {
354 exch .knownget {
355 exec
356 } {
357 pop pop % pass wrong type as is to .image3
358 } ifelse
359 } {
360 pop pop pop % pass wrong type as is to .image3
361 } ifelse
362 } if
363 .image3
364 } .internalbind put
365 dup 4 /.image4 load put
366 % We also detect ImageType 103 here: it isn't worth making a separate file
367 % just for this.
368 systemdict /ALLOWPSTRANSPARENCY get {/.image3x where { pop dup 103 /.image3x load put } if} if
369 pop
370
371 currentdict /data_mask_dict undef
372 currentdict /update_image3 undef
373 currentdict /buffer_mask undef
374
375 % ------ Functions ------ %
376
377 % Define the FunctionType resource category.
378 /Generic /Category findresource dup maxlength 3 add dict .copydict begin
379 /InstanceType /integertype def
380 /FunctionType currentdict end /Category defineresource pop
381
382 {0 2 3} { dup /FunctionType defineresource pop } forall
383
384 % ------ Smooth shading ------ %
385
386 % Define the ShadingType resource category.
387 /Generic /Category findresource dup maxlength 3 add dict .copydict begin
388 /InstanceType /integertype def
389 /ShadingType currentdict end /Category defineresource pop
390
391 systemdict /.shadingtypes mark % not ll3dict
392 1 /.buildshading1 load
393 2 /.buildshading2 load
394 3 /.buildshading3 load
395 4 /.buildshading4 load
396 5 /.buildshading5 load
397 6 /.buildshading6 load
398 7 /.buildshading7 load
399 .dicttomark put
400
401 systemdict /.reuseparamdict mark
402 /Intent 2
403 /AsyncRead //false
404 /CloseSource //true
405 .dicttomark readonly put
406 /.buildshading { % <shadingdict> .buildshading <shading>
407 dup rcheck not {
408 % Adobe seems to access ColorSpace first and CET 12-14c checks this.
409 /$error .systemvar /errorinfo [ /ColorSpace //null ] put
410 /shfill .systemvar /invalidaccess signalerror
411 } if
412 % Unfortunately, we always need to make the DataSource reusable,
413 % because if clipping is involved, even shfill may need to read
414 % the source data multiple times. If it weren't for this,
415 % we would only need to create a reusable stream if the ultimate
416 % source of the data is a procedure (since the library can't
417 % suspend shading to do a procedure callout).
418 dup /DataSource .knownget {
419 dup type /filetype eq {
420 //.reuseparamdict /ReusableStreamDecode filter
421 % Copy the dictionary to replace the DataSource, but make sure the
422 % copy is in the same VM as the original.
423 .currentglobal 2 index gcheck .setglobal
424 % Stack: shdict rsdfile saveglobal
425 2 index dup length dict copy exch .setglobal
426 dup /DataSource 4 -1 roll put exch pop
427 } {
428 pop
429 } ifelse
430 } if
431 % The .buildshading operators use the current color space
432 % for ColorSpace.
433 dup /ShadingType .knownget not { % error handling for CET 12-14b conformance
434 /$error .systemvar /errorinfo [ /ShadingType //null ] put
435 /shfill .systemvar /undefined signalerror
436 } if
437 dup type /integertype ne {
438 /$error .systemvar /errorinfo [ /ShadingType 4 index ] put
439 /shfill .systemvar /typecheck signalerror
440 } if
441 //.shadingtypes 1 index .knownget not {
442 /$error .systemvar /errorinfo [ /ShadingType 4 index ] put
443 /shfill .systemvar /rangecheck signalerror
444 } if
445 exch pop 1 index /ColorSpace .knownget {
446 { setcolorspace } stopped {
447 /$error .systemvar /errorinfo [ /ColorSpace 4 index ] put
448 /shfill .systemvar /$error .systemvar /errorname get signalerror
449 } if
450 exec
451 } {
452 /$error .systemvar /errorinfo [ /ColorSpace //null ] put
453 /shfill .systemvar /undefined signalerror
454 } ifelse
455 } .internalbind def
456
457 /.buildshading_and_shfill {
458 //.buildshading exec .shfill
459 } bind def
460
461 systemdict /.reuseparamdict undef
462
463 /.buildpattern2 { % <template> <matrix> .buildpattern2
464 % <template> <pattern>
465 % We want to build the pattern without doing gsave/grestore,
466 % since we want it to load the CIE caches.
467 1 index /Shading get
468 mark currentcolor currentcolorspace
469 counttomark 4 add -3 roll mark 4 1 roll
470 % Stack: -mark- ..color.. cspace -mark- template matrix shadingdict
471 { //.buildshading exec} stopped {
472 cleartomark setcolorspace setcolor pop stop
473 } if
474 .buildshadingpattern
475 3 -1 roll pop counttomark 1 add 2 roll setcolorspace setcolor pop
476 } .internalbind def
477
478 .patterntypes
479 2 //.buildpattern2 put
480
481 /shfill { % <shadingdict> shfill -
482 % Currently, .shfill requires that the color space
483 % in the pattern be the current color space.
484 % Disable overprintmode for shfill
485 { dup gsave 0 .setoverprintmode .buildshading_and_shfill } stopped
486 grestore {
487 /$error .systemvar /errorinfo 2 copy known {
488 pop pop
489 } {
490 //null put % CET 12-14c
491 } ifelse
492 stop
493 } if
494 pop
495 } odef
496
497 % Establish an initial smoothness value that matches Adobe RIPs.
498 0.02 setsmoothness
499
500 % ------ DeviceN color space ------ %
501 % gs_res.ps uses these entries in colorspacedict
502 % to populate the ColorSpaceFamily resource, so we need
503 % to add the supported spaces.
504 %
505 systemdict /colorspacedict get begin
506 /CIEBasedDEF [] def
507 /CIEBasedDEFG [] def
508 /DeviceN [] def
509 end
510
511 % ------ Miscellaneous ------ %
512
513 % Make the setoverprintmode and currentoverprintmode operators visible (3015)
514 % Because of the requirements of PDF, the .currentoverprintmode and
515 % .setoverprintmode operators have integer parameters. Thus we need to
516 % convert the Postscript bool parameters to integers.
517 %
518 /setoverprintmode % bool setoverprint -
519 {
520 { 1 } { 0 } ifelse % convert bool to integer
521 //.setoverprintmode
522 } odef
523
524 /currentoverprintmode % - currentoverprintmode bool
525 {
526 //.currentoverprintmode
527 0 ne % convert integers to bool
528 } odef
529
530 % Define additional user and system parameters.
531 /HalftoneMode 0 .definepsuserparam
532 /MaxSuperScreen 1016 .definepsuserparam
533 % read-only, so use .forceput
534 pssystemparams /MaxDisplayAndSourceList 160000 .forceput
535
536 % Define the IdiomSet resource category.
537 { /IdiomSet } {
538 /Generic /Category findresource dup maxlength 3 add dict .copydict begin
539 /InstanceType /dicttype def
540 currentdict end /Category defineresource pop
541 } forall
542
543 (*) {
544 dup type /nametype eq not {
545 cvn
546 } if
547 /IdiomSet findresource pop
548 } 256 string /IdiomSet resourceforall
549
550 /languagelevel 3 def
551 % When running in LanguageLevel 3 mode, this interpreter is supposed to be
552 % compatible with Adobe version 3010.
553 /version (3010) readonly def
554
555 .setlanguagelevel
556
557 % undefine things defined (or used) in this file and not referenced elsewhere
558 [
559 /.buildpattern2
560 /.buildshading
561 /.buildshading1 /.buildshading2 /.buildshading3 /.buildshading4
562 /.buildshading5 /.buildshading6 /.buildshading7 /.buildshadingpattern
563 /.makehalftone7 /.sethalftone6 /.sethalftone10 /.sethalftone16
564 /.readbytestring /.bigstring
565 ]
566 dup currentdict .undefinternalnames
567 systemdict .undefinternalnames
568
569 end % ll3dict