summaryrefslogtreecommitdiff
path: root/Master/xemtex/gslib/gs_ll3.ps
blob: dfcd3a09445ea2610384da057a117bf8b0ce13e7 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
%    Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
% 
% This program is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation; either version 2 of the License, or (at your
% option) any later version.
%
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
% Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 59 Temple Place, Suite 330, Boston, MA, 02111-1307.

% $Id: gs_ll3.ps,v 1.10.2.2.2.1 2003/04/12 14:02:38 giles Exp $
% 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
% Do the right thing if NOBIND or DELAYBIND is in effect.
% Note also 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 load /.bind load ne
/bind {		% <proc> bind <proc'>
  //.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
} odef
{ /.bind /bind load def
  /bind { } def
} if
currentdict /.bindscratch .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 {	% <dict> <dict> <source> <Width> <Height>
			%   (<Width2> <Height2> | null) <BPS> .makehalftone7
			%   <setdict> <dict5> { .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 load /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 {	% <size> .bigstring <string|bytestring>
  dup 65400 gt { .bytestring } { string } ifelse
} bind def

/.readbytestring {	% <source> <bytestring> .readbytestring
			%   <bytestring> <filled>
		% 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 {	% <dict> <dict> .sethalftone6 <setdict> <dict5>
			%   { .sethalftone5 }
			% Keys: Width, Height, Thresholds, T'Function
  dup /Thresholds get
  1 index /Width get 2 index /Height get
  null 8 .makehalftone7
} bind def

/.sethalftone10 {	% <dict> <dict> .sethalftone10 <setdict> <dict5>
			%   { .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 {	% <dict> <dict> .sethalftone16 <setdict> <dict5>
			%   { .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
  } {
    pop .getdefaulthalftone
    { //sethalftone }
    { .setdefaulthalftone }
    ifelse
  } ifelse
} odef

% ------ ImageTypes 3 and 4 (masked images) ------ %

.imagetypes
  dup 3 /.image3 load 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

% ------ 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 {	% <shadingdict> .buildshading <shading>
        % 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
      .currentglobal 1 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 get //.shadingtypes exch get
  1 index /ColorSpace get setcolorspace exec
} bind def
systemdict /.reuseparamdict undef

/.buildpattern2 {	% <template> <matrix> .buildpattern2
			%   <template> <pattern>
	% We want to build the pattern without doing gsave/grestore,
	% since we want it to load the CIE caches.
  1 index /Shading get
  mark currentcolor currentcolorspace
  counttomark 4 add -3 roll mark 4 1 roll
	% Stack: -mark- ..color.. cspace -mark- template matrix shadingdict
  { .buildshading } stopped {
    cleartomark setcolorspace setcolor pop stop
  } if
  .buildshadingpattern
  3 -1 roll pop counttomark 1 add 2 roll setcolorspace setcolor pop
} bind def

.patterntypes
  2 /.buildpattern2 load put

/shfill {		% <shadingdict> shfill -
	% Currently, .shfill requires that the color space
	% in the pattern be the current color space.
  dup gsave { .buildshading .shfill } stopped grestore { stop } if
  pop
} odef

% Establish an initial smoothness value that matches Adobe RIPs.
0.02 setsmoothness

% ------ UseCIEColor ------ %

% The library maintains and detects the UseCIEColor device parameter,
% but it doesn't have access to the resource dictionaries.  We also
% want color space substitution to work in systems without a PostScript
% interpreter.  Therefore, we eagerly inform the library of changes in
% the (effective) ColorSpace category that might affect the operation of
% UseCIEColor.  We must notice the following events:
%	1) defineresource and undefineresource of the Default ColorSpaces.
%	2) restore.
%	3) Changes in current VM, which cause a different set of resources
%	to become visible.
% #1 is rare.  #2 is handled in C code.  If checking the ColorSpace
% category on #3 turns out to be expensive, we can cache more information
% about whether these operations actually affect UseCIEColor.

% This operator implements color space substitution in the library:
%	<index> <bool> .setsubstitutecolorspace -
% <bool> = true means substitute the current color space for the one given
% by <index>; <bool> = false means stop substituting.  Substitution is not
% affected by grestore/setgstate, but it is affected by restore.

% ColorSpace defineresource and undefineresource for the Default keys
% call .definedefaultcs and .undefinedefaultcs.  See gs_res.ps.

/.useciecolorkeydict mark
  /UseCIEColor null
.dicttomark readonly def
/.definedefaultcs {	% <index> <value> .definedefaultcs -
  currentcolorspace
	% Temporarily disable color substitution, in case the substitute
	% color space is or mentions a color space that is currently
	%  being substituted.
  currentdevice //.useciecolorkeydict .getdeviceparams exch pop exch pop
  mark 5 -2 roll
	% Stack: cspace UseCIEColor mark index value
   { setcolorspace true .setsubstitutecolorspace }
  stopped counttomark 1 add 1 roll cleartomark
	% Stack: cspace UseCIEColor stopped?
  3 1 roll
  currentdevice null true mark /UseCIEColor 6 -1 roll .putdeviceparams pop pop
  setcolorspace { stop } if
} bind def
currentdict /.useciecolorkeydict undef

/.undefinedefaultcs {	% <index> .undefinedefaultcs -
  false .setsubstitutecolorspace
} bind def

/.setdefaultcs {	% <index> <value|null> .setdefaultcs -
  dup null eq { pop .undefinedefaultcs } { .definedefaultcs } ifelse
} bind def

/.getdefaultcs {	% <key> .getdefaultcs <value|null>
  .GetInstance { 0 get } { null } ifelse
} bind def

/.setglobal {		% <bool> .setglobal -
  dup .currentglobal ne {
	% If there are no local definitions of the Default keys,
	% changing VM can't affect the definition of any resource.
    /.localcsdefaults .uservar {
	% We only want to change substitutions for color spaces
	% whose definitions are actually changing.
      /ColorSpace /Category findresource begin
      /DefaultGray .getdefaultcs
      /DefaultRGB .getdefaultcs
      /DefaultCMYK .getdefaultcs
      end
      3 index .setglobal
      /ColorSpace /Category findresource begin
      /DefaultGray .getdefaultcs
      /DefaultRGB .getdefaultcs
      /DefaultCMYK .getdefaultcs
      end
			% Stack: bool oldgray oldrgb oldcmyk
			%   newgray newrgb newcmyk
      dup 4 index ne { 2 exch .setdefaultcs } { pop } ifelse
      dup 4 index ne { 1 exch .setdefaultcs } { pop } ifelse
      dup 4 index ne { 0 exch .setdefaultcs } { pop } ifelse
      pop pop pop pop
    } {
      .setglobal
    } ifelse
  } {
    .setglobal
  } ifelse
} .bind odef		% bind in .setglobal


% Define default device color spaces to allow substitution
% on UseCIEColor == true :

/DefaultGray
[ /CIEBasedA
  3 dict begin
     /WhitePoint [ 0.9505 1.0 1.089 ] def
     /DecodeLMN  [ { 1.8 exp } { 1.8 exp } { 1.8 exp } ] def
     /MatrixLMN  [ 0.4439 0.2522 0.0436 0.3341 0.6371 0.1387 0.1725 0.1107 0.9067 ] def
  currentdict end
]
/ColorSpace defineresource pop

/DefaultRGB
[ /CIEBasedABC
  3 dict begin
     /WhitePoint [ 0.9505 1.0 1.089 ] def
     /DecodeLMN  [ { 1.8 exp } { 1.8 exp } { 1.8 exp } ]  def
     /MatrixLMN  [ 0.4439 0.2522 0.0436 0.3341 0.6371 0.1387 0.1725 0.1107 0.9067 ] def
  currentdict end
]
/ColorSpace defineresource pop

/DefaultCMYK
[ /CIEBasedDEFG
  4 dict begin
     /WhitePoint [ 0.9505 1.0 1.089 ] def
     /Table [ 2 2 2 2
            % Trivial table :
            % [
            %   [ < FFFFFF 000000 FFFF00 000000>
            %     < FF00FF 000000 FF0000 000000> ]
            %   [ < 00FFFF 000000 00FF00 000000>
            %     < 0000FF 000000 000000 000000> ]
            % ]

            % AcrobatReader-compatible table :
              [
                [ < FFFFFF 000000 FFFF00 000000>
                  < EA007F 000000 FC000F 000000> ]

                [ < 00A0C8 000000 008436 000000>
                  < 00007C 000000 000000 000000> ]
              ]
            ] def
     /DecodeLMN [ { 1.8 exp } { 1.8 exp } { 1.8 exp } ] def
     /MatrixLMN [ 0.4439 0.2522 0.0436 0.3341 0.6371 0.1387 0.1725 0.1107 0.9067 ] def
  currentdict end
]
/ColorSpace defineresource pop


% ------ DeviceN color space ------ %

% This isn't quite right, because the ColorSpaceFamily resource will exist
% even with languagelevel < 3, but it's close enough.

/.setdevicenspace where {
  pop colorspacedict /DeviceN {
    dup 2 get setcolorspace dup
    dup 1 get length		% # of components
    .converttinttransform .setdevicenspace
  } bind put
} if

% ------ Miscellaneous ------ %

% Define additional user and system parameters.
/HalftoneMode 0 .definepsuserparam
/MaxSuperScreen 1016 .definepsuserparam
pssystemparams begin		% read-only, so use .forcedef
  /MaxDisplayAndSourceList 160000 .forcedef
end

% Define the IdiomSet resource category.
{ /IdiomSet } {
  /Generic /Category findresource dup maxlength 3 add dict .copydict begin
    /InstanceType /dicttype def
  currentdict end /Category defineresource pop
} forall

/languagelevel 3 def
% When running in LanguageLevel 3 mode, this interpreter is supposed to be
% compatible with Adobe version 3010.
/version (3010) readonly def

.setlanguagelevel

end			% ll3dict