% Copyright (C) 1997, 2000 Aladdin Enterprises. All rights reserved. % % This software is provided AS-IS with no warranty, either express or % implied. % % This software is distributed under license and may not be copied, % modified or distributed except as expressly authorized under the terms % of the license contained in the file LICENSE in this distribution. % % For more information about licensing, please refer to % http://www.ghostscript.com/licensing/. For information on % commercial licensing, go to http://www.artifex.com/licensing/ or % contact Artifex Software, Inc., 101 Lucas Valley Road #110, % San Rafael, CA 94903, U.S.A., +1(415)492-9861. % $Id: gs_ll3.ps 10300 2009-11-09 18:42:04Z alexcher $ % Initialization file for PostScript LanguageLevel 3 functions. % This file must be loaded after gs_lev2.ps and gs_res.ps. % These definitions go into ll3dict or various ProcSets. % NOTE: the interpreter creates ll3dict. ll3dict begin % We need LanguageLevel 2 or higher in order to have setuserparams and % defineresource. languagelevel dup 2 .max .setlanguagelevel % ------ Idiom recognition ------ % /IdiomRecognition false .definepsuserparam % Modify `bind' to apply idiom recognition afterwards. /.bindscratch 128 string def % Note that since this definition of `bind' may get bound in, % it has to function properly even at lower language levels, % where IdiomRecognition may not be defined. /.bind_ { % .bind //.bind currentuserparams /IdiomRecognition .knownget not { false } if { (*) { /IdiomSet findresource false exch { % Stack: proc false dummykey [template substitute] exch pop dup 1 get exch 0 get % Stack: proc false substitute template 3 index .eqproc { 2 index gcheck 1 index gcheck not and { pop } { 3 -1 roll pop exch not exit } ifelse } { pop } ifelse } forall { exit } if } //.bindscratch /IdiomSet resourceforall } if } def /.bind /.bind_ load odef % note that .bind_ will get bound currentdict /.bindscratch .undef DELAYBIND NOBIND or not { % We can't just do % /bind /.bind load def % because then /bind load == prints --.bind-- . /bind {//.bind_ exec} odef } if currentdict /.bind_ undef % ------ HalftoneTypes 6, 10, 16 and HalftoneMode ------ % % This code depends on an internal HalftoneType 7 with the following keys: % Width, Height, Width2, Height2, TransferFunction: % as for HalftoneType 16. % Thresholds: a string or bytestring holding the thresholds, % (Width x Height + Width2 x Height2) x BitsPerSample / 8 bytes, % as for HalftoneType 16 except that the samples may be either % 8 or 16 bits wide. % BitsPerSample: 8 or 16. % Note that this HalftoneType never appears in halftone dictionaries given % to sethalftone, only as a component in those given to .sethalftone5, % so its numeric value can be chosen ad lib as long as it differs from the % other values that are legal in component dictionaries for .sethalftone5 % (currently only 1 and 3). /.makehalftone7 { % % ( | null) .makehalftone7 % { .sethalftone5 } 8 dict begin /HalftoneType 7 def /BitsPerSample exch def dup null eq { pop 0 } { /Height2 1 index def /Width2 2 index def mul } ifelse 3 1 roll /Height 1 index def /Width 2 index def mul add BitsPerSample 8 idiv mul .bigstring % Stack: dict dict source str dup type /stringtype eq { readstring } { .readbytestring } ifelse not { /sethalftone .systemvar /rangecheck signalerror exit } if readonly /Thresholds exch def /TransferFunction .knownget { /TransferFunction exch def } if % If the original Thresholds was a file, replace it with % a new one. dup /Thresholds get type /filetype eq { dup /Thresholds [ Thresholds ] cvx 0 () .subfiledecode put } if mark /HalftoneType 5 /Default currentdict end .dicttomark { .sethalftone5 } } bind def /.bigstring { % .bigstring dup 65400 gt { .bytestring } { string } ifelse } bind def /.readbytestring { % .readbytestring % % Note that since bytestrings don't implement getinterval, % if filled is false, there is no way to tell how much % was read. true exch 0 1 2 index length 1 sub { % Stack: source true str index 3 index read not { pop exch not exch exit } if 3 copy put pop pop } for 3 -1 roll pop exch } bind def /.sethalftone6 { % .sethalftone6 % { .sethalftone5 } % Keys: Width, Height, Thresholds, T'Function dup /Thresholds get 1 index /Width get 2 index /Height get null 8 .makehalftone7 } bind def /.sethalftone10 { % .sethalftone10 % { .sethalftone5 } % Keys: Xsquare, Ysquare, Thresholds, T'Function % Note that this is the only one of these three HalftoneTypes % that allows either a file or a string for Thresholds. dup /Thresholds get dup type /stringtype eq { 0 () .subfiledecode } if 1 index /Xsquare get dup 3 index /Ysquare get dup 8 .makehalftone7 } bind def /.sethalftone16 { % .sethalftone16 % { .sethalftone5 } % Keys: Width, Height, Width2, Height2, % Thresholds, T'Function dup /Thresholds get 1 index /Width get 2 index /Height get 3 index /Width2 .knownget { % 2-rectangle case 4 index /Height2 get } { % 1-rectangle case null } ifelse 16 .makehalftone7 } bind def .halftonetypes begin 6 /.sethalftone6 load def 10 /.sethalftone10 load def 16 /.sethalftone16 load def end % Redefine the halftone-setting operators to honor HalftoneMode. /setcolorscreen { /HalftoneMode getuserparam 0 eq { //setcolorscreen } { 12 { pop } repeat .getdefaulthalftone { //sethalftone } { .setdefaulthalftone } ifelse } ifelse } odef /setscreen { /HalftoneMode getuserparam 0 eq { //setscreen } { pop pop pop .getdefaulthalftone { //sethalftone } { .setdefaulthalftone } ifelse } ifelse } odef /sethalftone { /HalftoneMode getuserparam 0 eq { //sethalftone } { gsave //sethalftone grestore .getdefaulthalftone { //sethalftone } { .setdefaulthalftone } ifelse } ifelse } odef % ------ ImageTypes 3 and 4 (masked images) ------ % % Put the new data source in the requested dictionary. % Copy the updated dictionary because it nay be used elsewhere. % update_image3 /update_image3 { 3 -1 roll dup length dict copy dup % [data] /name <> <> 2 index get % [data] /name <> <> dup length dict copy dup % [data] /name <> <> <> /DataSource 6 -1 roll put % /name <> <> exch dup % /name <> <> <> 4 2 roll % <> <> /name <> put % <> } bind def % Copy mask data source to a reusable stream % buffer_mask /buffer_mask { 1 index /MaskDict get dup /Width get 1 index /BitsPerComponent get mul 7 add 8 idiv exch /Height get mul () /SubFileDecode filter /ReusableStreamDecode filter } bind def % Data type vs. mask type decission table. % All procedures have the signature: % proc /data_mask_dict mark /stringtype mark % data is a string /stringtype { pop pop } bind /arraytype { % convert data to proc, don't buffer mask pop dup type dup /arraytype eq exch /packedarraytype eq or { [ exch { 1 array astore cvx } forall ] } { 1 array astore cvx } ifelse /DataDict //update_image3 exec } bind /packedarraytype 1 index /filetype { % convert data to file, don't buffer mask pop dup type dup /arraytype eq exch /packedarraytype eq or { [ exch { 1 array astore cvx 0 () /SubFileDecode filter } forall ] } { 1 array astore cvx 0 () /SubFileDecode filter } ifelse /DataDict //update_image3 exec } bind .dicttomark readonly /arraytype mark % data is a proc /stringtype { % convert mask to proc exch pop 1 array astore cvx /MaskDict //update_image3 exec } bind /arraytype { % buffer mask in reusable stream and convert back to proc exch pop //buffer_mask exec 500 string { readstring pop } aload pop 4 packedarray cvx /MaskDict //update_image3 exec } bind /packedarraytype 1 index /filetype 1 index .dicttomark readonly /packedarraytype 1 index /filetype mark % data is a file /stringtype { % convert mask to file exch pop 1 array astore cvx 0 () /SubFileDecode filter /MaskDict //update_image3 exec } bind /arraytype { % buffer the mask and convert to file exch pop //buffer_mask exec /MaskDict //update_image3 exec } bind /packedarraytype 1 index /filetype { //systemdict /userdict get /PDFTopSave known { pop pop % mask is already buffered by PDF interpreter } { exch pop % buffer the mask //buffer_mask exec /MaskDict //update_image3 exec } ifelse } bind .dicttomark readonly .dicttomark readonly def .imagetypes dup 3 { dup /InterleaveType get 3 eq { % .image3 requires data sources of the image and mask to be of the % same type. Work around this restriction here. dup /DataDict get /DataSource get 1 index /MaskDict get /DataSource get % <<>> data mask dup type % <<>> data mask mask_type 2 index 4 index /DataDict get /MultipleDataSources .knownget { { 0 get } if } if type % <<>> data mask mask_type data_type //data_mask_dict exch .knownget { exch .knownget { exec } { pop pop % pass wrong type as is to .image3 } ifelse } { pop pop pop % pass wrong type as is to .image3 } ifelse } if .image3 } bind put dup 4 /.image4 load put % We also detect ImageType 103 here: it isn't worth making a separate file % just for this. /.image3x where { pop dup 103 /.image3x load put } if pop currentdict /data_mask_dict undef currentdict /update_image3 undef currentdict /buffer_mask undef % ------ Functions ------ % % Define the FunctionType resource category. /Generic /Category findresource dup maxlength 3 add dict .copydict begin /InstanceType /integertype def /FunctionType currentdict end /Category defineresource pop {0 2 3} { dup /FunctionType defineresource pop } forall % ------ Smooth shading ------ % % Define the ShadingType resource category. /Generic /Category findresource dup maxlength 3 add dict .copydict begin /InstanceType /integertype def /ShadingType currentdict end /Category defineresource pop systemdict /.shadingtypes mark % not ll3dict 1 /.buildshading1 load 2 /.buildshading2 load 3 /.buildshading3 load 4 /.buildshading4 load 5 /.buildshading5 load 6 /.buildshading6 load 7 /.buildshading7 load .dicttomark put systemdict /.reuseparamdict mark /Intent 2 /AsyncRead false /CloseSource true .dicttomark readonly put /.buildshading { % .buildshading dup rcheck not { % Adobe seems to access ColorSpace first and CET 12-14c checks this. /$error .systemvar /errorinfo [ /ColorSpace null ] put /shfill .systemvar /invalidaccess signalerror } if % Unfortunately, we always need to make the DataSource reusable, % because if clipping is involved, even shfill may need to read % the source data multiple times. If it weren't for this, % we would only need to create a reusable stream if the ultimate % source of the data is a procedure (since the library can't % suspend shading to do a procedure callout). dup /DataSource .knownget { dup type /filetype eq { //.reuseparamdict /ReusableStreamDecode filter % Copy the dictionary to replace the DataSource, but make sure the % copy is in the same VM as the original. .currentglobal 2 index gcheck .setglobal % Stack: shdict rsdfile saveglobal 2 index dup length dict copy exch .setglobal dup /DataSource 4 -1 roll put exch pop } { pop } ifelse } if % The .buildshading operators use the current color space % for ColorSpace. dup /ShadingType .knownget not { % error handling for CET 12-14b conformance /$error .systemvar /errorinfo [ /ShadingType //null ] put /shfill .systemvar /undefined signalerror } if dup type /integertype ne { /$error .systemvar /errorinfo [ /ShadingType 4 index ] put /shfill .systemvar /typecheck signalerror } if //.shadingtypes 1 index .knownget not { /$error .systemvar /errorinfo [ /ShadingType 4 index ] put /shfill .systemvar /rangecheck signalerror } if exch pop 1 index /ColorSpace .knownget { { setcolorspace } stopped { /$error .systemvar /errorinfo [ /ColorSpace 4 index ] put /shfill .systemvar /$error .systemvar /errorname get signalerror } if exec } { /$error .systemvar /errorinfo [ /ColorSpace //null ] put /shfill .systemvar /undefined signalerror } ifelse } bind def systemdict /.reuseparamdict undef /.buildpattern2 { %