pphs.ps 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. % Copyright (C) 2001 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This software is provided AS-IS with no warranty, either express or
  4. % implied.
  5. %
  6. % This software is distributed under license and may not be copied,
  7. % modified or distributed except as expressly authorized under the terms
  8. % of the license contained in the file LICENSE in this distribution.
  9. %
  10. % For more information about licensing, please refer to
  11. % http://www.ghostscript.com/licensing/. For information on
  12. % commercial licensing, go to http://www.artifex.com/licensing/ or
  13. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  14. % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  15. % $Id: pphs.ps,v 1.6 2003/08/08 18:45:05 ray Exp $
  16. % Print Linearized PDF hint streams
  17. % Utilities
  18. /read1 { % <file> read1 <value>
  19. read not {
  20. (**** Unexpected EOF) = flush quit
  21. } if
  22. } bind def
  23. /read2 { % <file> read2 <value>
  24. dup read1 8 bitshift exch read1 add
  25. } bind def
  26. /read4 { % <file> read4 <value>
  27. dup read2 16 bitshift exch read2 add
  28. } bind def
  29. % Free variables: Bits, Bitsleft
  30. /readninit { % - <readninit> -
  31. /Bits 0 def
  32. /Bitsleft 0 def
  33. } bind def
  34. /pdftoken { % <file> pdftoken <token>
  35. dup token pop
  36. dup type /nametype eq 1 index xcheck and {
  37. dup dup (<<) cvn eq exch ([) eq or {
  38. exec exch {
  39. dup pdftoken dup dup (>>) cvn eq exch (]) eq or {
  40. exch pop exec exit
  41. } if exch
  42. } loop
  43. } {
  44. exch pop
  45. } ifelse
  46. } {
  47. exch pop
  48. } ifelse
  49. } bind def
  50. /makemask { % <nbits> makemask <mask>
  51. 1 exch bitshift 1 sub
  52. } bind def
  53. /readn { % <file> <nbits> readn <value>
  54. dup Bitsleft le {
  55. exch pop
  56. /Bitsleft Bitsleft 2 index sub def
  57. makemask Bits Bitsleft neg bitshift and
  58. } {
  59. Bitsleft makemask Bits and
  60. exch Bitsleft sub exch 1 index bitshift 3 1 roll
  61. /Bits 2 index read1 def /Bitsleft 8 def
  62. readn add
  63. } ifelse
  64. } bind def
  65. /sread { % <string> sread <file>
  66. 0 () /SubFileDecode filter
  67. } bind def
  68. /ptag { % <pre-tag> <proc> <post-tag> ptag -
  69. 3 -1 roll print (: ) print
  70. exch exec
  71. ( % ) print =
  72. } bind def
  73. % Print the linearization parameters dictionary.
  74. /plpkeys <<
  75. /E (end of p. 1 objects)
  76. /L (total file length)
  77. /H (PHS start + length)
  78. /N (# of pages)
  79. /O (p. 1 object #)
  80. /T (offset of first main xref entry)
  81. >> def
  82. /plpdict { % <dict> plpdict -
  83. (<<) = plpkeys {
  84. 2 index 2 index .knownget {
  85. % Stack: dict key label value
  86. ( ) print 3 -1 roll ===only ( ) print ===only
  87. ( % ) print =
  88. } {
  89. pop pop
  90. } ifelse
  91. } forall {
  92. plpkeys 2 index known {
  93. pop pop
  94. } {
  95. ( ) print exch ===only ( ) print ===
  96. } ifelse
  97. } forall (>>) =
  98. } bind def
  99. % Print the Page Offset Hint Table.
  100. /ppoht { % <npages> <file> ppoht -
  101. 20 dict begin
  102. /f exch def
  103. /npages exch def
  104. readninit
  105. (1) { f read4 =only } (least # objs/page) ptag
  106. (2) { f read4 =only } (offset of p. 1 object (+PHS length if beyond PHS)) ptag
  107. (3) { f read2 dup =only /nb3 exch def } (# bits for # objs/page delta) ptag
  108. (4) { f read4 =only } (least # bytes/page) ptag
  109. (5) { f read2 dup =only /nb5 exch def } (# bits for # bytes/page delta) ptag
  110. (6) { f read4 =only } (least content stream offset-in-page) ptag
  111. (7) { f read2 dup =only /nb7 exch def } (# bits for content stream offset delta) ptag
  112. (8) { f read4 =only } (least content stream length) ptag
  113. (9) { f read2 dup =only /nb9 exch def } (# bits for content stream length delta) ptag
  114. (10) { f read2 dup =only /nb10 exch def } (# bits for # of shared obj refs) ptag
  115. (11) { f read2 dup =only /nb11 exch def } (# bits for shared obj indices) ptag
  116. (12) { f read2 dup =only /nb12 exch def } (# bits for shared obj ref pos numerators) ptag
  117. (13) { f read2 =only } (shared obj ref pos denominator) ptag
  118. (*1) { [ npages { f nb3 readn } repeat ] ==only } (# objs/page deltas (see 1,3)) ptag
  119. (*2) { [ npages { f nb5 readn } repeat ] ==only } (# bytes/page deltas (see 4,5)) ptag
  120. (*3) { [ npages { f nb10 readn } repeat ] dup ==only /nso exch def } (# of shared obj refs (see 10)) ptag
  121. (*4) { [ nso { [ exch { f nb11 readn } repeat ] } forall ] ==only } (shared obj indices (see 11)) ptag
  122. (*5) { [ nso { [ exch { f nb12 readn } repeat ] } forall ] ==only } (shared obj ref pos numerators (see 12)) ptag
  123. (*6) { [ npages { f nb7 readn } repeat ] ==only } (content stream offset-in-page deltas (see 6,7)) ptag
  124. (*7) { [ npages { f nb9 readn } repeat ] ==only } (content stream length deltas (see 8,9)) ptag
  125. end % temp dict
  126. } bind def
  127. % Print the Shared Objects Hint Table.
  128. /psoht { % <file> psoht -
  129. 20 dict begin
  130. /f exch def
  131. readninit
  132. (1) { f read4 =only } (first shared obj #) ptag
  133. (2) { f read4 =only } (first shared obj offset (+PHS length if beyond PHS)) ptag
  134. (3) { f read4 dup =only /n3 exch def } (# of p. 1 shared objs) ptag
  135. (4) { f read4 dup =only /n4 exch def } (total # of shared objs) ptag
  136. (5) { f read2 dup =only /nb5 exch def } (# bits for # of shared objs/group) ptag
  137. (6) { f read4 =only } (least shared obj group length) ptag
  138. (7) { f read2 dup =only /nb7 exch def } (# bits for shared obj group length delta) ptag
  139. /nse n4 def
  140. (*1) { [ nse { f nb7 readn } repeat ] ==only } (shared obj group length deltas (see 6,7)) ptag
  141. (*2) { [ nse { f 1 readn } repeat ] dup ==only /md5s exch def } (MD5 present?) ptag
  142. (*3:) = md5s {
  143. 0 ne {
  144. ( ) print f 16 string readstring pop
  145. (%stdout) (w) file dup 3 -1 roll writehexstring closefile () =
  146. } if
  147. } forall
  148. (*4) { [ nse { f nb5 readn } repeat ] ==only } (# objs/group (see 5)) ptag
  149. end % temp dict
  150. } bind def
  151. % Print the Primary Hint Stream of a PDF file.
  152. /pphs { % <file> pphs -
  153. /pdf exch def
  154. % Read the linearization parameter dictionary.
  155. { pdf pdftoken /obj eq { exit } if } loop
  156. pdf pdftoken /lpdict exch def
  157. /lpdict type /dicttype eq { lpdict /Linearized known } { false } ifelse {
  158. (Not a linearized PDF file.) = stop
  159. } if
  160. lpdict plpdict flush
  161. % Read the primary hint stream.
  162. null {
  163. pdf pdftoken dup /stream eq { pop exit } if
  164. exch pop
  165. } loop
  166. /phsdict exch def
  167. % Remove Length if indirect reference.
  168. phsdict 0 known {
  169. phsdict 0 undef phsdict /Length undef
  170. } if
  171. (PHS: ) print phsdict === flush
  172. pdf 0 (endstream) /SubFileDecode filter
  173. dup 64000 string readstring pop exch closefile
  174. sread /phsdata exch def
  175. % Decode the hint stream data if necessary.
  176. phsdict /Filter .knownget {
  177. phsdata exch filter
  178. dup 5000 string readstring pop exch closefile
  179. sread /phsdata exch def
  180. } if
  181. % Adobe says we can assume /P = 0.
  182. (Page Offset Hint Table:) =
  183. lpdict /N get
  184. phsdata phsdict /S get string readstring pop sread
  185. ppoht
  186. (Shared Objects Hint Table:) =
  187. phsdata psoht
  188. } bind def
  189. % Check for command line arguments.
  190. [ shellarguments
  191. { ] dup length 1 eq
  192. { 0 get (r) file dup pphs closefile }
  193. { (Usage: pphs filename.pdf\n) print flush }
  194. ifelse
  195. }
  196. { pop }
  197. ifelse