% Copyright (C) 2000 artofcode LLC. 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_resmp.ps,v 1.11 2004/10/25 15:11:37 igor Exp $ % A procset to redefine a resource category with a resource map. % Public entries : % Redefine - a procedure for redefining a resource category with a map. % Methods for interpreting the resource map to be provided by client % in the argument dictionary. % % Note that the procedure Redefine is idempotential : % consequtive calls to it will not replace the category methods, % but will merge resource maps. If an interleaving redefinition % needs to cancel the idempotentity, it must remove the entry % /.IsRedefinedWithMap from the category dictionary. % MakeResourceEnumerator - this procedure is useful for % redefining any category. It provides a proper order of instances % and proper stacks during resourceforall. % BindWithCurrentdict - a procedure for generating temporary procedures % from templates, binding them with a local dictionary. % execstack_lookup - a procedure for communicating through the execution stack. % It allows for a callee to get an information from an indirect caller. % The procedures are designed for exeution witout putting % the procset instance onto the dictionary stack. languagelevel 2 .setlanguagelevel currentglobal true setglobal /MappedCategoryRedefiner 10 dict begin % The procset. currentpacking false setpacking /InstanceEnumeratorPattern % - InstanceEnumeratorPattern ... { % This is a pattern for enumeration procedure to be built dynamically, % applying BindWithCurrentdict with a temporary dictionary. % The following names will be replaced with specific objects % during BindWithCurrentdict : % en_local_dict - a dictionary for storing the local integer variable 'status'. % scr - the scratch string argument of resourceforall; % proc - the procedure argument of resourceforall; % InstancesStatus - a dictionary that maps resource instance names to their status value; % Category - the category to be enumerated. % When this procedure is called from ResourceForAll, the category is the current dictionary. % We remove it from the dictionary stack before performing the enumeration % to provide the to write to the underlying dictionary, % and put it back after the enumeration is completed. end { 0 1 2 { en_local_dict exch /status exch put InstancesStatus { en_local_dict /status get eq { scr cvs % ... (Font) proc exec % } { pop } ifelse % ... } forall } for % ... } stopped Category begin { stop } if } bind def % An auxiliary proc for BindWithCurrentdict : /.BindAux % BindAux { 0 exec } bind def setpacking /BindWithCurrentdict % BindWithCurrentdict { % Make a copy of the given procedure, binding in the values of all names % defined in currentdict. % Caution1 : this code cannot handle procedures that were already % bound recursively. % Caution2 : this code don't bind packedarrays. This was done % intentionally for a termination of the procedure tree. dup length array copy dup length 1 sub -1 0 { 2 copy get % {precopy} i {elem} dup dup type /arraytype eq exch xcheck and { % {precopy} i {elem} //.BindAux exec % {precopy} i {elem_copy} 2 index 3 1 roll put % {precopy} } { dup dup type /nametype eq exch xcheck and { % {precopy} i {elem} currentdict exch .knownget { 2 index 3 1 roll put % {precopy} } { pop } ifelse } { pop pop } ifelse } ifelse % {precopy} } for % {copy} cvx } bind def //.BindAux 0 //BindWithCurrentdict put % bind the recursive call in 'Bind'. /MakeResourceEnumerator % MakeResourceEnumerator { % Build the enumeration procedure : % Since the resourceforall procedure may leave values on the operand stack, % we cannot simply store the enumerator's local data on the stack. % We also cannot use a static dictionary to store local variables, % because of possible recursion in the resourceforall procedure. % To work around this, we create a copy of the enumeration procedure and % bind it dynamically with a temporary dictionary, which contains % local variables for the currently executing instance of resourceforall. currentdict % Category 6 dict begin % the temporary dictionary /Category exch def % /InstancesStatus exch def /scr exch def /proc exch def /en_local_dict currentdict def //InstanceEnumeratorPattern //BindWithCurrentdict exec % Enumerator /status 0 def % variable for the current status to enumerate - do not bind with it ! end } bind def /execstack_lookup % execstack_lookup % execstack_lookup null { % Checks whether execution stack contains a procedure starting with , % and retrives the 2nd element of the procedure, % or null if the procedure was not found. % % Since 'execstack' actually renders subarrays of procedures, % the pattern for recognition must be like this : % % { % CallSomething % } loop % % The solution with 'loop' depends on how GS implements cycles, % so it must not appear in documents, which are required to be interpreter independent. % Any other type of cycles are also acceptable. % If no repitition is really needed, just insert 'exit' into its body. % If are not needed for the caller, insert "pop pop" after them. % If is really unuseful, the pattern may be simplified : % % { pop % CallSomething % exit % } loop % % It will retrieve 'pop' or 'null'. % % Note that 2 topmost execstack elements are the execstack_lookup procedure and its caller. % We don't check them. currentglobal false setglobal % bGlobal countexecstack array execstack % bGlobal [execstack] dup null exch % bGlobal [execstack] null [execstack] length 3 sub -1 0 { % bGlobal [execstack] null i 2 index exch get % bGlobal [execstack] null proc dup type dup /packedarraytype eq exch /arraytype eq or { dup length 1 gt { % bGlobal [execstack] null proc dup 0 get % bGlobal [execstack] null proc elem0 5 index eq { % bGlobal [execstack] null proc 1 get % bGlobal [execstack] null object1 exch pop exit % bGlobal [execstack] object1 } { pop } ifelse } { pop % bGlobal [execstack] false } ifelse } { pop % bGlobal [execstack] false } ifelse } for % bGlobal [execstack] bResult exch pop exch setglobal exch pop % bResult } bind def currentpacking false setpacking /MethodsToRedefine 5 dict begin % Procedures in this dictionary really are patterns for new category methods. % The following names will be replaced with specific objects during BindWithCurrentdict : % .map - the map dictionary; % DefineResource, ResourceStatus, ResourceFileName, FindResource, ResourceForAll % - procedures from the original resource category. /FindResource % FindResource { RESMPDEBUG { (resmp FindResource beg ) print dup = } if dup ResourceStatus exec { pop 2 lt } { false } ifelse % bInVirtualMemory { FindResource exec } { dup dup .map exch .knownget { % /Name /Name <> dup dup /RecordVirtualMethods get /IsActive get exec { 1 index .getvminstance { % /Name /Name <> holder 1 get 1 eq } { true } ifelse % /Name /Name <> bStatusIs1 4 1 roll % bStatusIs1 /Name /Name <> dup /RecordVirtualMethods get /MakeInstance get exec % bStatusIs1 /Name /Name Instance size 5 1 roll % size bStatusIs1 /Name /Name Instance DefineResource exec % size bStatusIs1 /Name Instance % Make ResourceStatus to return correct values for this instance : % Hack: we replace status values in the instance holder : exch .getvminstance pop % size bStatusIs1 Instance holder dup 5 -1 roll 2 exch put % bStatusIs1 Instance holder 3 2 roll { % Instance holder 1 1 put % Instance } { pop } ifelse % Instance } { % /Name /Name <> pop pop FindResource exec } ifelse } { % /Name /Name pop FindResource exec } ifelse } ifelse RESMPDEBUG { (resmp FindResource end) = } if } bind def /ResourceStatus % ResourceStatus true % ResourceStatus false { RESMPDEBUG { (resmp ResourceStatus beg ) print dup == } if dup ResourceStatus exec { % /Name status size 1 index 2 lt { % In VM - return with it. 3 2 roll pop true } { % Not in VM. exch pop exch % size /Name dup .map exch .knownget { % size /Name <> dup dup /RecordVirtualMethods get /IsActive get exec { 3 2 roll pop % /Name <> dup /RecordVirtualMethods get /GetSize get exec 2 exch true } { % size /Name <> pop pop 2 exch true } ifelse } { % size /Name pop 2 exch true } ifelse } ifelse } { % /Name dup .map exch .knownget { % /Name <> dup dup /RecordVirtualMethods get /IsActive get exec { dup /RecordVirtualMethods get /GetSize get exec 2 exch true } { % /Name <> pop pop false } ifelse } { % /Name pop false } ifelse } ifelse RESMPDEBUG { (resmp ResourceStatus end) = } if } bind def /ResourceFileName % ResourceFileName { RESMPDEBUG { (resmp ResourceFileName beg ) print 1 index = } if exch % (scratch) /Name .map 1 index .knownget { % (scratch) /Name <> RESMPDEBUG { (resmp ResourceFileName : have a map record.) = } if dup dup /RecordVirtualMethods get /IsActive get exec { RESMPDEBUG { (resmp ResourceFileName : record is active.) = } if dup /RecordVirtualMethods get /GetFilePath get exec % (string) RESMPDEBUG { (resmp ResourceFileName : retrieving ) print dup = } if } { % (scratch) /Name <> RESMPDEBUG { (resmp ResourceFileName : record is NOT active.) = } if pop exch ResourceFileName exec RESMPDEBUG { (resmp ResourceFileName : retrieving ) print dup = } if } ifelse } { RESMPDEBUG { (resmp ResourceFileName : have NO map record.) = } if exch ResourceFileName exec RESMPDEBUG { (resmp ResourceFileName : retrieving ) print dup = } if } ifelse RESMPDEBUG { (resmp ResourceFileName end) = } if } bind def /ResourceForAll %