You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.7 KiB

1 month ago
  1. % Copyright (C) 2001-2023 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. % Implementation of ReusableStreamDecode filter.
  16. % This file must be loaded after gs_lev2.ps and gs_res.ps.
  17. level2dict begin
  18. % ------ ReusableStreamDecode filter ------ %
  19. /.reusablestreamdecode { % <source> <dict> .reusablestreamdecode <file>
  20. % <source> .reusablestreamdecode <file>
  21. % Collect the filter parameters.
  22. dup type /dicttype eq { 2 copy } { dup 0 dict } ifelse
  23. dup .rsdparams
  24. % Construct the filter pipeline.
  25. % The very first filter should use the value of CloseSource
  26. % from the RSD dictionary; all the others should have
  27. % CloseSource = true.
  28. % Stack: source dict filters parms
  29. 2 index /CloseSource .knownget not { //false } if 5 -1 roll
  30. % Stack: dict filters parms CloseSource source
  31. 0 1 5 index length 1 sub {
  32. 4 index 1 index get
  33. % Stack: dict filters parms CloseSource source index filtname
  34. 4 index //null eq {
  35. 0 dict
  36. } {
  37. 4 index 2 index get dup //null eq { pop 0 dict} if
  38. } ifelse
  39. 3 -1 roll pop exch filter
  40. exch pop //true exch % set CloseSource for further filters
  41. } for
  42. % If AsyncRead is true, try to create the filter directly.
  43. % Stack: dict filters parms CloseSource source
  44. 4 index /AsyncRead .knownget not { //false } if {
  45. 1 index { .reusablestream } //.internalstopped exec
  46. } {
  47. //null //true
  48. } ifelse {
  49. pop
  50. % No luck. Read the entire contents of the stream now.
  51. dup type /filetype ne {
  52. % Make a stream from a procedure or string data source.
  53. 0 () .subfiledecode
  54. } if
  55. % We must allocate the string in the same VM space as its
  56. % source, since the reusable stream must be allocated there.
  57. .currentglobal 1 index gcheck .setglobal exch
  58. currentpacking //false setpacking exch
  59. 2 dict begin /filelen 0 def % scratch dict for filelen and pos (below).
  60. % Stack: dict filters parms CloseSource oldglobal oldpacking file
  61. [ exch {
  62. dup 40000 string readstring
  63. /filelen 2 index length filelen add def % accumulate filelen
  64. not { exit } if exch
  65. } loop
  66. exch pop
  67. ]
  68. { filelen string } stopped { % try allocating a single string
  69. pop % couldn't make a string - discard filelen value
  70. } {
  71. % transfer the array-of-strings to the single string.
  72. % stack: ... [() ...] string
  73. /pos 0 def exch {
  74. 1 index exch pos exch putinterval /pos pos 40000 add def
  75. } forall
  76. } ifelse
  77. % top of stack is either array of strings or one string == stream_data
  78. end % done with scratch dict
  79. % Stack: dict filters parms CloseSource oldglobal oldpacking stream_data
  80. 3 1 roll setpacking setglobal
  81. % Stack: dict filters parms CloseSource stream_data
  82. 1 index .reusablestream
  83. } if
  84. % We created the stream successfully: clean up.
  85. 4 { exch pop } repeat
  86. 1 index type /dicttype eq { exch pop } if exch pop
  87. } odef
  88. filterdict /ReusableStreamDecode /.reusablestreamdecode load put
  89. end % level2dict