summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlgs/lib/gs_patrn.ps
blob: 6f1e9dd5e575e9e2fb5b641a16c0e68159f62243 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
%    Copyright (C) 2001, 2002 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_patrn.ps 7186 2006-11-10 12:18:42Z alexcher $
% Pattern color space method dictionary.


% verify that Pattern color spaces are supported
/.setpatternspace where
  { pop }
  { currentfile closefile }
ifelse

.currentglobal true .setglobal
.cspace_util begin

%
%   <name1 | array1>   get_pattern_base_cspace   <null | name2 | array2>
%
% If the Pattern color space has a base color space, push that base
% color space onto the stack. Otherwise, push a null object.
%
/get_pattern_base_cspace
  {
    dup type /nametype eq
      { pop //null }
      {
        dup length 1 gt
          { 1 get }
          { pop //null }
        ifelse
      }
    ifelse
  }
bind def


%
%   <dict>   has_base_color   <bool>
%
% Determine if a Pattern "color" includes a base color. This is the case
% if the pattern dictionary has PatternType 1 and PaintType 2.
%
/has_base_color
  {
    dup //null eq
      { pop //false }
      {
        dup /PatternType get 1 eq
          { /PaintType get 2 eq }
          { pop //false }
        ifelse
      }
    ifelse
  }
bind def

%
%   <c1> ... <cn>  <pattern_dict>  <pattern_cspace>
%   get_pattern_base_color
%   <c1> ... <cn>  <base_cspace>  true
% or
%   <?c1?> ... <?cn?>  <dict>  <pattern>
%   get_pattern_base_color
%   false
%
% If a pattern dictionary has a base color, set up that base color and
% color space, and push true. Otherwise, just push false. It is assumed
% that if the pattern includes a base color, the Pattern color space
% has a base color space.
%
/get_pattern_base_color
  {
    exch //has_base_color exec
      { 1 get //true }
      { pop //false }
    ifelse
  }
bind def


colorspacedict
/Pattern
  mark
    /cs_potential_indexed_base false
    /cs_potential_pattern_base false
    /cs_potential_alternate false
    /cs_potential_icc_alternate false

    %
    % We employ the same convention for describing the number of
    % components in a Pattern color space as is used by the graphic
    % library. For pattern spaces with no underlying color space,
    % the result is -1. If a Pattern space has an underlying color
    % space with n components, the result is -(n + 1).
    %
    /cs_get_ncomps
      {
        //get_pattern_base_cspace exec dup //null eq
          { pop 0 }
          //.cs_get_ncomps
        ifelse
        1 add neg
      }
    bind

    % there is no "range" for a Pattern color space
    /cs_get_range { {} cvlit } bind

    /cs_get_default_color { pop //null } bind

    /cs_get_currentgray
      {
        //get_pattern_base_color exec
          //.cs_get_currentgray
          { 0. }
        ifelse
      }
    bind

    /cs_get_currentrgb
      {
        //get_pattern_base_color exec
          //.cs_get_currentrgb
          { 0. 0. 0. }
        ifelse
      }
    bind

    /cs_get_currentcmyk
      {
        //get_pattern_base_color exec
          //.cs_get_currentcmyk
          { 0. 0. 0. 1. }
        ifelse
      }
    bind

    /cs_validate
      {
        dup //get_pattern_base_cspace exec dup //null eq
          { pop }
          {
            //.cs_validate exec //.cs_potential_pattern_base exec not
              //setcspace_rangecheck
            if
          }
        ifelse
      }
    bind

    % substitute the base space if appropriate
    /cs_substitute
      {
        dup //get_pattern_base_cspace exec dup //null eq
          { pop dup }
          {
            //.cs_substitute exec 2 copy eq
              { pop pop dup }
              {
                % retain only the new alternate space
                exch pop

                % build all new structures in local VM
               .currentglobal 3 1 roll //false .setglobal

                % construct a new array and insert the new base color space 
                1 index dup length array copy dup 1 4 -1 roll put

                % restore VM mode
                3 -1 roll .setglobal
              }
            ifelse
          }
        ifelse
      }
    bind

    /cs_prepare {}

    %
    % Install the current color space.
    %
    % The current Ghostscript color space implementation requires that
    % color spaces that provide a base or alternative color space set
    % that base/alternative color space to be the current color space
    % before attempting to set the original color space.
    %
    % In principle, the only errors that are possible for .setpatternspace
    % (given that setcolorspace itself is legal) are limitcheck and/or
    % VMerror. The Ghostscript implementation allows a few others, so 
    % we go through the full code to restore the current color space in
    % the event of an error.
    %
    /cs_install
      {
        dup //get_pattern_base_cspace exec dup //null eq
          {
            pop
            dup type /nametype eq
              { pop { /Pattern } cvlit }
            if
            .setpatternspace
          }
          {
            % save the current color space
            currentcolorspace

            % set the base color space as the current color space
            exch //forcesetcolorspace

            % set the pattern color space; restore the earlier space on error
            mark 2 index
              { .setpatternspace }
            stopped
              { cleartomark setcolorspace stop }
              { pop pop pop }
            ifelse
          }
        ifelse
      }
    bind


    %
    % Pattern dictionaries generated by makepattern will include an
    % Implementation entry whose value is an internal data structure.
    % Such structures are given executable type names that match their
    % internal structure names. The names used for pattern
    % implementations are gs_pattern1_instance_t and
    % gs_pattern2_instance_t. It is unfortunate to have to expose such
    % internal names at this level, but not easily avoided.
    % For CPSI/CET compatibility we 'hide' this inside an array
    %
    /cs_prepare_color
      {
        % verify that the topmost operand is a pattern dictionary
        1 index dup type /dicttype ne
          {
            //null ne
              //setcspace_typecheck
            if
            pop
          }
          {
            dup /Implementation .knownget
              { 0 get type dup dup
                /gs_pattern1_instance_t ne exch /gs_pattern2_instance_t ne and
                exch xcheck not
                or
                  //setcspace_typecheck
                if
              }
              //setcspace_typecheck
            ifelse

            % check if base color space operands are present
            dup /PatternType get 1 eq
              {
                /PaintType get 2 eq
                  {
                    % verify that a base color space exists
                    //get_pattern_base_cspace exec dup //null eq
                      //setcspace_rangecheck
                    if
                    //.cs_get_ncomps exec
		    % We cannot use check_num_stack since it does not
		    % properly clean the stack.  The CET test 09-47n.ps has
		    % a very bad test case with a faulty setpattern call with
		    % no color values.  To properly handle that case and to
		    % ensure that the stack is properly cleaned, we are
		    % testing the type of the color values inline.
		    1 exch 1 exch
		      {
		        index type dup /integertype ne exch /realtype ne and
		          //setcolor_typecheck
		        if
		      }
		    for
                  }
                  { pop }
                ifelse
              }
              { pop pop }
            ifelse
          }
        ifelse
      }
    bind

    /cs_complete_setcolor //pop_1

  .dicttomark
put

end     % .cspace_util
.setglobal