home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / GhostScript / !GhostScr / 6_01 / lib / gs_ll3.ps < prev    next >
Text File  |  2000-03-29  |  15KB  |  468 lines

  1. %    Copyright (C) 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % $Id: gs_ll3.ps,v 1.1 2000/03/09 08:40:40 lpd Exp $
  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. % Do the right thing if NOBIND or DELAYBIND is in effect.
  34. % Note also that since this definition of `bind' may get bound in,
  35. % it has to function properly even at lower language levels,
  36. % where IdiomRecognition may not be defined.
  37. /bind load /.bind load ne
  38. /bind {        % <proc> bind <proc'>
  39.   //.bind currentuserparams /IdiomRecognition
  40.   .knownget not { false } if {
  41.     (*) {
  42.       /IdiomSet findresource
  43.       false exch {
  44.         % Stack: proc false dummykey [template substitute]
  45.     exch pop dup 1 get exch 0 get
  46.         % Stack: proc false substitute template
  47.     3 index .eqproc {
  48.       2 index gcheck 1 index gcheck not and {
  49.         pop
  50.       } {
  51.         3 -1 roll pop exch not exit
  52.       } ifelse
  53.     } {
  54.       pop
  55.     } ifelse
  56.       } forall { exit } if
  57.     } //.bindscratch /IdiomSet resourceforall
  58.   } if
  59. } odef
  60. { /.bind /bind load def
  61.   /bind { } def
  62. } if
  63. currentdict /.bindscratch .undef
  64.  
  65. % ------ HalftoneTypes 6, 10, 16 and HalftoneMode ------ %
  66.  
  67. % This code depends on an internal HalftoneType 7 with the following keys:
  68. %    Width, Height, Width2, Height2, TransferFunction:
  69. %      as for HalftoneType 16.
  70. %    Thresholds: a string or bytestring holding the thresholds,
  71. %      (Width x Height + Width2 x Height2) x BitsPerSample / 8 bytes,
  72. %      as for HalftoneType 16 except that the samples may be either
  73. %      8 or 16 bits wide.
  74. %    BitsPerSample: 8 or 16.
  75.  
  76. % Note that this HalftoneType never appears in halftone dictionaries given
  77. % to sethalftone, only as a component in those given to .sethalftone5,
  78. % so its numeric value can be chosen ad lib as long as it differs from the
  79. % other values that are legal in component dictionaries for .sethalftone5
  80. % (currently only 1 and 3).
  81.  
  82. /.makehalftone7 {    % <dict> <dict> <source> <Width> <Height>
  83.             %   (<Width2> <Height2> | null) <BPS> .makehalftone7
  84.             %   <setdict> <dict5> { .sethalftone5 }
  85.   8 dict begin
  86.   /HalftoneType 7 def
  87.   /BitsPerSample exch def
  88.   dup null eq {
  89.     pop 0
  90.   } {
  91.     /Height2 1 index def /Width2 2 index def mul
  92.   } ifelse 3 1 roll
  93.   /Height 1 index def
  94.   /Width 2 index def
  95.   mul add BitsPerSample 8 idiv mul .bigstring
  96.         % Stack: dict dict source str
  97.   dup type /stringtype eq { readstring } { .readbytestring } ifelse
  98.   not { /sethalftone load /rangecheck signalerror exit } if
  99.   readonly /Thresholds exch def
  100.   /TransferFunction .knownget { /TransferFunction exch def } if
  101.         % If the original Thresholds was a file, replace it with
  102.         % a new one.
  103.   dup /Thresholds get type /filetype eq {
  104.     dup /Thresholds [ Thresholds ] cvx 0 () .subfiledecode put
  105.   } if
  106.   mark /HalftoneType 5 /Default currentdict end .dicttomark
  107.   { .sethalftone5 }
  108. } bind def
  109.  
  110. /.bigstring {    % <size> .bigstring <string|bytestring>
  111.   dup 65400 gt { .bytestring } { string } ifelse
  112. } bind def
  113.  
  114. /.readbytestring {    % <source> <bytestring> .readbytestring
  115.             %   <bytestring> <filled>
  116.         % Note that since bytestrings don't implement getinterval,
  117.         % if filled is false, there is no way to tell how much
  118.         % was read.
  119.   true exch 0 1 2 index length 1 sub {
  120.         % Stack: source true str index
  121.     3 index read not { pop exch not exch exit } if
  122.     3 copy put pop pop
  123.   } for 3 -1 roll pop exch
  124. } bind def
  125.  
  126. /.sethalftone6 {    % <dict> <dict> .sethalftone6 <setdict> <dict5>
  127.             %   { .sethalftone5 }
  128.             % Keys: Width, Height, Thresholds, T'Function
  129.   dup /Thresholds get
  130.   1 index /Width get 2 index /Height get
  131.   null 8 .makehalftone7
  132. } bind def
  133.  
  134. /.sethalftone10 {    % <dict> <dict> .sethalftone10 <setdict> <dict5>
  135.             %   { .sethalftone5 }
  136.             % Keys: Xsquare, Ysquare, Thresholds, T'Function
  137.         % Note that this is the only one of these three HalftoneTypes
  138.         % that allows either a file or a string for Thresholds.
  139.   dup /Thresholds get dup type /stringtype eq { 0 () .subfiledecode } if
  140.   1 index /Xsquare get dup 3 index /Ysquare get dup
  141.   8 .makehalftone7
  142. } bind def
  143.  
  144. /.sethalftone16 {    % <dict> <dict> .sethalftone16 <setdict> <dict5>
  145.             %   { .sethalftone5 }
  146.             % Keys: Width, Height, Width2, Height2,
  147.             %   Thresholds, T'Function
  148.   dup /Thresholds get
  149.   1 index /Width get 2 index /Height get
  150.   3 index /Width2 .knownget {  % 2-rectangle case
  151.     4 index /Height2 get
  152.   } {            % 1-rectangle case
  153.     null
  154.   } ifelse 16 .makehalftone7
  155. } bind def
  156.  
  157. .halftonetypes begin
  158.   6 /.sethalftone6 load def
  159.   10 /.sethalftone10 load def
  160.   16 /.sethalftone16 load def
  161. end
  162.  
  163. % Redefine the halftone-setting operators to honor HalftoneMode.
  164. /setcolorscreen {
  165.   /HalftoneMode getuserparam 0 eq {
  166.     //setcolorscreen
  167.   } {
  168.     12 { pop } repeat .getdefaulthalftone
  169.     { //sethalftone }
  170.     { .setdefaulthalftone }
  171.     ifelse
  172.   } ifelse
  173. } odef
  174. /setscreen {
  175.   /HalftoneMode getuserparam 0 eq {
  176.     //setscreen
  177.   } {
  178.     pop pop pop .getdefaulthalftone
  179.     { //sethalftone }
  180.     { .setdefaulthalftone }
  181.     ifelse
  182.   } ifelse
  183. } odef
  184. /sethalftone {
  185.   /HalftoneMode getuserparam 0 eq {
  186.     //sethalftone
  187.   } {
  188.     pop .getdefaulthalftone
  189.     { //sethalftone }
  190.     { .setdefaulthalftone }
  191.     ifelse
  192.   } ifelse
  193. } odef
  194.  
  195. % ------ ImageTypes 3 and 4 (masked images) ------ %
  196.  
  197. .imagetypes
  198.   dup 3 /.image3 load put
  199.   4 /.image4 load put
  200.  
  201. % ------ Functions ------ %
  202.  
  203. % Define the FunctionType resource category.
  204. /Generic /Category findresource dup maxlength 3 add dict .copydict begin
  205.   /InstanceType /integertype def
  206. /FunctionType currentdict end /Category defineresource pop
  207.  
  208. {0 2 3} { dup /FunctionType defineresource pop } forall
  209.  
  210. % ------ Smooth shading ------ %
  211.  
  212. % Define the ShadingType resource category.
  213. /Generic /Category findresource dup maxlength 3 add dict .copydict begin
  214.   /InstanceType /integertype def
  215. /ShadingType currentdict end /Category defineresource pop
  216.  
  217. systemdict /.shadingtypes mark        % not ll3dict
  218.   1 /.buildshading1 load
  219.   2 /.buildshading2 load
  220.   3 /.buildshading3 load
  221.   4 /.buildshading4 load
  222.   5 /.buildshading5 load
  223.   6 /.buildshading6 load
  224.   7 /.buildshading7 load
  225. .dicttomark put
  226.  
  227. systemdict /.reuseparamdict mark
  228.   /Intent 2
  229.   /AsyncRead false
  230.   /CloseSource true
  231. .dicttomark readonly put
  232. /.buildshading {    % <shadingdict> .buildshading <shading>
  233.     % The current implementation of shadings can't deal with
  234.     % procedure-based DataSource.
  235.   dup /DataSource .knownget {
  236.     dup type /filetype eq {
  237.       dup .isprocfilter {
  238.     //.reuseparamdict /ReusableStreamDecode filter
  239.     .currentglobal 1 index gcheck .setglobal
  240.         % Stack: shdict rsdfile saveglobal
  241.     2 index dup length dict copy exch .setglobal
  242.     dup /DataSource 4 -1 roll put exch pop
  243.       } {
  244.     pop
  245.       } ifelse
  246.     } {
  247.       pop
  248.     } ifelse
  249.   } if
  250.     % The .buildshading operators use the current color space
  251.     % for ColorSpace.
  252.   dup /ShadingType get //.shadingtypes exch get
  253.   1 index /ColorSpace get setcolorspace exec
  254. } bind def
  255. systemdict /.reuseparamdict undef
  256.  
  257. /.buildpattern2 {    % <template> <matrix> .buildpattern2
  258.             %   <template> <pattern>
  259.     % We want to build the pattern without doing gsave/grestore,
  260.     % since we want it to load the CIE caches.
  261.   1 index /Shading get
  262.   mark currentcolor currentcolorspace
  263.   counttomark 4 add -3 roll mark 4 1 roll
  264.     % Stack: -mark- ..color.. cspace -mark- template matrix shadingdict
  265.   { .buildshading } stopped {
  266.     cleartomark setcolorspace setcolor pop stop
  267.   } if
  268.   .buildshadingpattern
  269.   3 -1 roll pop counttomark 1 add 2 roll setcolorspace setcolor pop
  270. } bind def
  271.  
  272. .patterntypes
  273.   2 /.buildpattern2 load put
  274.  
  275. /shfill {        % <shadingdict> shfill -
  276.     % Currently, .shfill requires that the color space
  277.     % in the pattern be the current color space.
  278.   dup gsave { .buildshading .shfill } stopped grestore { stop } if
  279.   pop
  280. } odef
  281.  
  282. % Establish an initial smoothness value that matches Adobe RIPs.
  283. 0.02 setsmoothness
  284.  
  285. % ------ ReusableStreamDecode filter ------ %
  286.  
  287. /.reusablestreamdecode {    % <source> <dict> .reusablestreamdecode <file>
  288.                 % <source> .reusablestreamdecode <file>
  289.         % Collect the filter parameters.
  290.   dup type /dicttype eq { 2 copy } { dup 0 dict } ifelse
  291.   dup .rsdparams
  292.         % Construct the filter pipeline.
  293.         % The very first filter should use the value of CloseSource
  294.         % from the RSD dictionary; all the others should have
  295.         % CloseSource = true.
  296.         % Stack: source dict filters parms
  297.   2 index /CloseSource .knownget not { false } if 5 -1 roll
  298.         % Stack: dict filters parms CloseSource source
  299.   0 1 5 index length 1 sub {
  300.     4 index 1 index get
  301.         % Stack: dict filters parms CloseSource source index filtname
  302.     4 index null eq {
  303.       0 dict
  304.     } {
  305.       4 index 2 index get dup null eq { pop } if
  306.     } ifelse
  307.     3 -1 roll pop exch filter
  308.     exch pop true exch        % set CloseSource for further filters
  309.   } for
  310.         % See if we can create the filter directly.
  311.         % Stack: dict filters parms CloseSource file
  312.   null 2 index { .reusablestream } .internalstopped {
  313.     pop pop
  314.         % No luck.  Read the entire contents of the stream now.
  315.     dup type /filetype ne {
  316.         % Make a stream from a procedure or string data source.
  317.       0 () .subfiledecode
  318.     } if
  319.     10 dict exch {
  320.         % Stack: dict filters parms CloseSource contdict file
  321.       dup 1000 string readstring
  322.       3 index dup length 4 -1 roll put not { exit } if
  323.     } loop pop
  324.         % Concatenate the contents into one big string.
  325.         % Stack: dict filters parms CloseSource contdict
  326.     0 1 index { length exch pop add } forall
  327.     .bigstring exch {
  328.         % Stack: dict filters parms CloseSource string index substring
  329.       exch 1000 mul exch 2 index 3 1 roll putinterval
  330.     } forall
  331.         % Now create the stream on the string.
  332.     null 2 index .reusablestream
  333.   } if
  334.         % We created the stream successfully: clean up.
  335.   4 { exch pop } repeat
  336.   1 index type /dicttype eq { exch pop } if exch pop
  337. } odef
  338.  
  339. filterdict /ReusableStreamDecode /.reusablestreamdecode load put
  340.  
  341. % ------ UseCIEColor ------ %
  342.  
  343. % The library maintains and detects the UseCIEColor device parameter,
  344. % but it doesn't have access to the resource dictionaries.  We also
  345. % want color space substitution to work in systems without a PostScript
  346. % interpreter.  Therefore, we eagerly inform the library of changes in
  347. % the (effective) ColorSpace category that might affect the operation of
  348. % UseCIEColor.  We must notice the following events:
  349. %    1) defineresource and undefineresource of the Default ColorSpaces.
  350. %    2) restore.
  351. %    3) Changes in current VM, which cause a different set of resources
  352. %    to become visible.
  353. % #1 is rare.  #2 is handled in C code.  If checking the ColorSpace
  354. % category on #3 turns out to be expensive, we can cache more information
  355. % about whether these operations actually affect UseCIEColor.
  356.  
  357. % This operator implements color space substitution in the library:
  358. %    <index> <bool> .setsubstitutecolorspace -
  359. % <bool> = true means substitute the current color space for the one given
  360. % by <index>; <bool> = false means stop substituting.  Substitution is not
  361. % affected by grestore/setgstate, but it is affected by restore.
  362.  
  363. % ColorSpace defineresource and undefineresource for the Default keys
  364. % call .definedefaultcs and .undefinedefaultcs.  See gs_res.ps.
  365.  
  366. /.useciecolorkeydict mark
  367.   /UseCIEColor null
  368. .dicttomark readonly def
  369. /.definedefaultcs {    % <index> <value> .definedefaultcs -
  370.   currentcolorspace
  371.     % Temporarily disable color substitution, in case the substitute
  372.     % color space is or mentions a color space that is currently
  373.     %  being substituted.
  374.   currentdevice //.useciecolorkeydict .getdeviceparams exch pop exch pop
  375.   mark 5 -2 roll
  376.     % Stack: cspace UseCIEColor mark index value
  377.    { setcolorspace true .setsubstitutecolorspace }
  378.   stopped counttomark 1 add 1 roll cleartomark
  379.     % Stack: cspace UseCIEColor stopped?
  380.   3 1 roll
  381.   currentdevice null true mark /UseCIEColor 6 -1 roll .putdeviceparams pop pop
  382.   setcolorspace { stop } if
  383. } bind def
  384. currentdict /.useciecolorkeydict undef
  385.  
  386. /.undefinedefaultcs {    % <index> .undefinedefaultcs -
  387.   false .setsubstitutecolorspace
  388. } bind def
  389.  
  390. /.setdefaultcs {    % <index> <value|null> .setdefaultcs -
  391.   dup null eq { pop .undefinedefaultcs } { .definedefaultcs } ifelse
  392. } bind def
  393.  
  394. /.getdefaultcs {    % <key> .getdefaultcs <value|null>
  395.   .GetInstance { 0 get } { null } ifelse
  396. } bind def
  397.  
  398. /.setglobal {        % <bool> .setglobal -
  399.   dup .currentglobal ne {
  400.     % We only want to change substitutions for color spaces
  401.     % whose definitions are actually changing.
  402.     /ColorSpace /Category findresource begin
  403.     % If there are no local definitions of the Default keys,
  404.     % changing VM can't affect the definition of any resource.
  405.     .LocalDefaults {
  406.       /DefaultGray .getdefaultcs
  407.       /DefaultRGB .getdefaultcs
  408.       /DefaultCMYK .getdefaultcs
  409.       end
  410.       3 index .setglobal
  411.       /ColorSpace /Category findresource begin
  412.       /DefaultGray .getdefaultcs
  413.       /DefaultRGB .getdefaultcs
  414.       /DefaultCMYK .getdefaultcs
  415.       end
  416.             % Stack: bool oldgray oldrgb oldcmyk
  417.             %   newgray newrgb newcmyk
  418.       dup 4 index ne { 2 exch .setdefaultcs } { pop } ifelse
  419.       dup 4 index ne { 1 exch .setdefaultcs } { pop } ifelse
  420.       dup 4 index ne { 0 exch .setdefaultcs } { pop } ifelse
  421.       pop pop pop pop
  422.     } {
  423.       end .setglobal
  424.     } ifelse
  425.   } {
  426.     .setglobal
  427.   } ifelse
  428. } .bind odef        % bind in .setglobal
  429.  
  430. % ------ DeviceN color space ------ %
  431.  
  432. % This isn't quite right, because the ColorSpaceFamily resource will exist
  433. % even with languagelevel < 3, but it's close enough.
  434.  
  435. /.setdevicenspace where {
  436.   pop colorspacedict /DeviceN {
  437.     dup 2 get setcolorspace dup .setdevicenspace
  438.   } bind put
  439. } if
  440.  
  441. % ------ Miscellaneous ------ %
  442.  
  443. % Define additional user and system parameters.
  444. /HalftoneMode 0 .definepsuserparam
  445. /MaxSuperScreen 1016 .definepsuserparam
  446. pssystemparams begin        % read-only, so use .forcedef
  447.   /MaxDisplayAndSourceList 160000 .forcedef
  448. end
  449.  
  450. % Define the IdiomSet resource category.
  451. { /IdiomSet } {
  452.   /Generic /Category findresource dup maxlength 3 add dict .copydict begin
  453.     /InstanceType /dicttype def
  454.   currentdict end /Category defineresource pop
  455. } forall
  456.  
  457. /languagelevel 3 def
  458. % When running in LanguageLevel 3 mode, this interpreter is supposed to be
  459. % compatible with Adobe version 3010.
  460. /version (3010) readonly def
  461.  
  462. .setlanguagelevel
  463.  
  464. end            % ll3dict
  465.