Debian Codegraph @

Resource/Init/gs_frsd.ps

raw
1 % Copyright (C) 2001-2025 Artifex Software, Inc.
2 % All Rights Reserved.
3 %
4 % This software is provided AS-IS with no warranty, either express or
5 % implied.
6 %
7 % This software is distributed under license and may not be copied,
8 % modified or distributed except as expressly authorized under the terms
9 % of the license contained in the file LICENSE in this distribution.
10 %
11 % Refer to licensing information at http://www.artifex.com or contact
12 % Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 % CA 94129, USA, for further information.
14 %
15
16 % Implementation of ReusableStreamDecode filter.
17 % This file must be loaded after gs_lev2.ps and gs_res.ps.
18
19 level2dict begin
20
21 % ------ ReusableStreamDecode filter ------ %
22
23 /.reusablestreamdecode { % <source> <dict> .reusablestreamdecode <file>
24 % <source> .reusablestreamdecode <file>
25 % Collect the filter parameters.
26 dup type /dicttype eq { 2 copy } { dup 0 dict } ifelse
27 dup .rsdparams
28 % Construct the filter pipeline.
29 % The very first filter should use the value of CloseSource
30 % from the RSD dictionary; all the others should have
31 % CloseSource = true.
32 % Stack: source dict filters parms
33 2 index /CloseSource .knownget not { //false } if 5 -1 roll
34 % Stack: dict filters parms CloseSource source
35 0 1 5 index length 1 sub {
36 4 index 1 index get
37 % Stack: dict filters parms CloseSource source index filtname
38 4 index //null eq {
39 0 dict
40 } {
41 4 index 2 index get dup //null eq { pop 0 dict} if
42 } ifelse
43 3 -1 roll pop exch filter
44 exch pop //true exch % set CloseSource for further filters
45 } for
46 % If AsyncRead is true, try to create the filter directly.
47 % Stack: dict filters parms CloseSource source
48 4 index /AsyncRead .knownget not { //false } if {
49 1 index { .reusablestream } //.internalstopped exec
50 } {
51 //null //true
52 } ifelse {
53 pop
54 % No luck. Read the entire contents of the stream now.
55 dup type /filetype ne {
56 % Make a stream from a procedure or string data source.
57 0 () /SubFileDecode filter
58 } if
59 % We must allocate the string in the same VM space as its
60 % source, since the reusable stream must be allocated there.
61
62 .currentglobal 1 index gcheck .setglobal exch
63 currentpacking //false setpacking exch
64 2 dict begin /filelen 0 def % scratch dict for filelen and pos (below).
65 % Stack: dict filters parms CloseSource oldglobal oldpacking file
66 [ exch {
67 dup 40000 string readstring
68 /filelen 2 index length filelen add def % accumulate filelen
69 not { exit } if exch
70 } loop
71 exch pop
72 ]
73 { filelen string } stopped { % try allocating a single string
74 pop % couldn't make a string - discard filelen value
75 } {
76 % transfer the array-of-strings to the single string.
77 % stack: ... [() ...] string
78 /pos 0 def exch {
79 1 index exch pos exch putinterval /pos pos 40000 add def
80 } forall
81 } ifelse
82 % top of stack is either array of strings or one string == stream_data
83 end % done with scratch dict
84 % Stack: dict filters parms CloseSource oldglobal oldpacking stream_data
85 3 1 roll setpacking setglobal
86 % Stack: dict filters parms CloseSource stream_data
87 1 index .reusablestream
88 } if
89 % We created the stream successfully: clean up.
90 4 { exch pop } repeat
91 1 index type /dicttype eq { exch pop } if exch pop
92 } odef
93
94 filterdict /ReusableStreamDecode /.reusablestreamdecode load put
95
96 end % level2dict