pphs.ps 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. % Copyright (C) 2001 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This file is part of AFPL Ghostscript.
  4. %
  5. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  6. % distributor accepts any responsibility for the consequences of using it, or
  7. % for whether it serves any particular purpose or works at all, unless he or
  8. % she says so in writing. Refer to the Aladdin Free Public License (the
  9. % "License") for full details.
  10. %
  11. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  12. % in a plain ASCII text file named PUBLIC. The License grants you the right
  13. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14. % conditions described in the License. Among other things, the License
  15. % requires that the copyright notice and this notice be preserved on all
  16. % copies.
  17. % $Id: pphs.ps,v 1.3 2001/07/24 22:24:28 lpd Exp $
  18. % Print Linearized PDF hint streams
  19. % Utilities
  20. /read1 { % <file> read1 <value>
  21. read not {
  22. (**** Unexpected EOF) = flush quit
  23. } if
  24. } bind def
  25. /read2 { % <file> read2 <value>
  26. dup read1 8 bitshift exch read1 add
  27. } bind def
  28. /read4 { % <file> read4 <value>
  29. dup read2 16 bitshift exch read2 add
  30. } bind def
  31. % Free variables: Bits, Bitsleft
  32. /readninit { % - <readninit> -
  33. /Bits 0 def
  34. /Bitsleft 0 def
  35. } bind def
  36. /pdftoken { % <file> pdftoken <token>
  37. dup token pop
  38. dup type /nametype eq 1 index xcheck and {
  39. dup dup (<<) cvn eq exch ([) eq or {
  40. exec exch {
  41. dup pdftoken dup dup (>>) cvn eq exch (]) eq or {
  42. exch pop exec exit
  43. } if exch
  44. } loop
  45. } {
  46. exch pop
  47. } ifelse
  48. } {
  49. exch pop
  50. } ifelse
  51. } bind def
  52. /makemask { % <nbits> makemask <mask>
  53. 1 exch bitshift 1 sub
  54. } bind def
  55. /readn { % <file> <nbits> readn <value>
  56. dup Bitsleft le {
  57. exch pop
  58. /Bitsleft Bitsleft 2 index sub def
  59. makemask Bits Bitsleft neg bitshift and
  60. } {
  61. Bitsleft makemask Bits and
  62. exch Bitsleft sub exch 1 index bitshift 3 1 roll
  63. /Bits 2 index read1 def /Bitsleft 8 def
  64. readn add
  65. } ifelse
  66. } bind def
  67. /sread { % <string> sread <file>
  68. 0 () /SubFileDecode filter
  69. } bind def
  70. /ptag { % <pre-tag> <proc> <post-tag> ptag -
  71. 3 -1 roll print (: ) print
  72. exch exec
  73. ( % ) print =
  74. } bind def
  75. % Print the linearization parameters dictionary.
  76. /plpkeys <<
  77. /E (end of p. 1 objects)
  78. /L (total file length)
  79. /H (PHS start + length)
  80. /N (# of pages)
  81. /O (p. 1 object #)
  82. /T (offset of first main xref entry)
  83. >> def
  84. /plpdict { % <dict> plpdict -
  85. (<<) = plpkeys {
  86. 2 index 2 index .knownget {
  87. % Stack: dict key label value
  88. ( ) print 3 -1 roll ===only ( ) print ===only
  89. ( % ) print =
  90. } {
  91. pop pop
  92. } ifelse
  93. } forall {
  94. plpkeys 2 index known {
  95. pop pop
  96. } {
  97. ( ) print exch ===only ( ) print ===
  98. } ifelse
  99. } forall (>>) =
  100. } bind def
  101. % Print the Page Offset Hint Table.
  102. /ppoht { % <npages> <file> ppoht -
  103. 20 dict begin
  104. /f exch def
  105. /npages exch def
  106. readninit
  107. (1) { f read4 =only } (least # objs/page) ptag
  108. (2) { f read4 =only } (offset of p. 1 object (+PHS length if beyond PHS)) ptag
  109. (3) { f read2 dup =only /nb3 exch def } (# bits for # objs/page delta) ptag
  110. (4) { f read4 =only } (least # bytes/page) ptag
  111. (5) { f read2 dup =only /nb5 exch def } (# bits for # bytes/page delta) ptag
  112. (6) { f read4 =only } (least content stream offset-in-page) ptag
  113. (7) { f read2 dup =only /nb7 exch def } (# bits for content stream offset delta) ptag
  114. (8) { f read4 =only } (least content stream length) ptag
  115. (9) { f read2 dup =only /nb9 exch def } (# bits for content stream length delta) ptag
  116. (10) { f read2 dup =only /nb10 exch def } (# bits for # of shared obj refs) ptag
  117. (11) { f read2 dup =only /nb11 exch def } (# bits for shared obj indices) ptag
  118. (12) { f read2 dup =only /nb12 exch def } (# bits for shared obj ref pos numerators) ptag
  119. (13) { f read2 =only } (shared obj ref pos denominator) ptag
  120. (*1) { [ npages { f nb3 readn } repeat ] ==only } (# objs/page deltas (see 1,3)) ptag
  121. (*2) { [ npages { f nb5 readn } repeat ] ==only } (# bytes/page deltas (see 4,5)) ptag
  122. (*3) { [ npages { f nb10 readn } repeat ] dup ==only /nso exch def } (# of shared obj refs (see 10)) ptag
  123. (*4) { [ nso { [ exch { f nb11 readn } repeat ] } forall ] ==only } (shared obj indices (see 11)) ptag
  124. (*5) { [ nso { [ exch { f nb12 readn } repeat ] } forall ] ==only } (shared obj ref pos numerators (see 12)) ptag
  125. (*6) { [ npages { f nb7 readn } repeat ] ==only } (content stream offset-in-page deltas (see 6,7)) ptag
  126. (*7) { [ npages { f nb9 readn } repeat ] ==only } (content stream length deltas (see 8,9)) ptag
  127. end % temp dict
  128. } bind def
  129. % Print the Shared Objects Hint Table.
  130. /psoht { % <file> psoht -
  131. 20 dict begin
  132. /f exch def
  133. readninit
  134. (1) { f read4 =only } (first shared obj #) ptag
  135. (2) { f read4 =only } (first shared obj offset (+PHS length if beyond PHS)) ptag
  136. (3) { f read4 dup =only /n3 exch def } (# of p. 1 shared objs) ptag
  137. (4) { f read4 dup =only /n4 exch def } (total # of shared objs) ptag
  138. (5) { f read2 dup =only /nb5 exch def } (# bits for # of shared objs/group) ptag
  139. (6) { f read4 =only } (least shared obj group length) ptag
  140. (7) { f read2 dup =only /nb7 exch def } (# bits for shared obj group length delta) ptag
  141. /nse n4 def
  142. (*1) { [ nse { f nb7 readn } repeat ] ==only } (shared obj group length deltas (see 6,7)) ptag
  143. (*2) { [ nse { f 1 readn } repeat ] dup ==only /md5s exch def } (MD5 present?) ptag
  144. (*3:) = md5s {
  145. 0 ne {
  146. ( ) print f 16 string readstring pop
  147. (%stdout) (w) file dup 3 -1 roll writehexstring closefile () =
  148. } if
  149. } forall
  150. (*4) { [ nse { f nb5 readn } repeat ] ==only } (# objs/group (see 5)) ptag
  151. end % temp dict
  152. } bind def
  153. % Print the Primary Hint Stream of a PDF file.
  154. /pphs { % <file> pphs -
  155. /pdf exch def
  156. % Read the linearization parameter dictionary.
  157. { pdf pdftoken /obj eq { exit } if } loop
  158. pdf pdftoken /lpdict exch def
  159. /lpdict type /dicttype eq { lpdict /Linearized known } { false } ifelse {
  160. (Not a linearized PDF file.) = stop
  161. } if
  162. lpdict plpdict flush
  163. % Read the primary hint stream.
  164. null {
  165. pdf pdftoken dup /stream eq { pop exit } if
  166. exch pop
  167. } loop
  168. /phsdict exch def
  169. % Remove Length if indirect reference.
  170. phsdict 0 known {
  171. phsdict 0 undef phsdict /Length undef
  172. } if
  173. (PHS: ) print phsdict === flush
  174. pdf 0 (endstream) /SubFileDecode filter
  175. dup 5000 string readstring pop exch closefile
  176. sread /phsdata exch def
  177. % Decode the hint stream data if necessary.
  178. phsdict /Filter .knownget {
  179. phsdata exch filter
  180. dup 5000 string readstring pop exch closefile
  181. sread /phsdata exch def
  182. } if
  183. % Adobe says we can assume /P = 0.
  184. (Page Offset Hint Table:) =
  185. lpdict /N get
  186. phsdata phsdict /S get string readstring pop sread
  187. ppoht
  188. (Shared Objects Hint Table:) =
  189. phsdata psoht
  190. } bind def
  191. % Check for command line arguments.
  192. [ shellarguments
  193. { ] dup length 1 eq
  194. { 0 get (r) file dup pphs closefile }
  195. { (Usage: pphs filename.pdf\n) print flush }
  196. ifelse
  197. }
  198. { pop }
  199. ifelse