% Copyright (C) 2001 Aladdin Enterprises. All rights reserved. % % This file is part of AFPL Ghostscript. % % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or % distributor accepts any responsibility for the consequences of using it, or % for whether it serves any particular purpose or works at all, unless he or % she says so in writing. Refer to the Aladdin Free Public License (the % "License") for full details. % % Every copy of AFPL Ghostscript must include a copy of the License, normally % in a plain ASCII text file named PUBLIC. The License grants you the right % to copy, modify and redistribute AFPL Ghostscript, but only under certain % conditions described in the License. Among other things, the License % requires that the copyright notice and this notice be preserved on all % copies. % $Id: pphs.ps,v 1.3 2001/07/24 22:24:28 lpd Exp $ % Print Linearized PDF hint streams % Utilities /read1 { % read1 read not { (**** Unexpected EOF) = flush quit } if } bind def /read2 { % read2 dup read1 8 bitshift exch read1 add } bind def /read4 { % read4 dup read2 16 bitshift exch read2 add } bind def % Free variables: Bits, Bitsleft /readninit { % - - /Bits 0 def /Bitsleft 0 def } bind def /pdftoken { % pdftoken dup token pop dup type /nametype eq 1 index xcheck and { dup dup (<<) cvn eq exch ([) eq or { exec exch { dup pdftoken dup dup (>>) cvn eq exch (]) eq or { exch pop exec exit } if exch } loop } { exch pop } ifelse } { exch pop } ifelse } bind def /makemask { % makemask 1 exch bitshift 1 sub } bind def /readn { % readn dup Bitsleft le { exch pop /Bitsleft Bitsleft 2 index sub def makemask Bits Bitsleft neg bitshift and } { Bitsleft makemask Bits and exch Bitsleft sub exch 1 index bitshift 3 1 roll /Bits 2 index read1 def /Bitsleft 8 def readn add } ifelse } bind def /sread { % sread 0 () /SubFileDecode filter } bind def /ptag { % ptag - 3 -1 roll print (: ) print exch exec ( % ) print = } bind def % Print the linearization parameters dictionary. /plpkeys << /E (end of p. 1 objects) /L (total file length) /H (PHS start + length) /N (# of pages) /O (p. 1 object #) /T (offset of first main xref entry) >> def /plpdict { % plpdict - (<<) = plpkeys { 2 index 2 index .knownget { % Stack: dict key label value ( ) print 3 -1 roll ===only ( ) print ===only ( % ) print = } { pop pop } ifelse } forall { plpkeys 2 index known { pop pop } { ( ) print exch ===only ( ) print === } ifelse } forall (>>) = } bind def % Print the Page Offset Hint Table. /ppoht { % ppoht - 20 dict begin /f exch def /npages exch def readninit (1) { f read4 =only } (least # objs/page) ptag (2) { f read4 =only } (offset of p. 1 object (+PHS length if beyond PHS)) ptag (3) { f read2 dup =only /nb3 exch def } (# bits for # objs/page delta) ptag (4) { f read4 =only } (least # bytes/page) ptag (5) { f read2 dup =only /nb5 exch def } (# bits for # bytes/page delta) ptag (6) { f read4 =only } (least content stream offset-in-page) ptag (7) { f read2 dup =only /nb7 exch def } (# bits for content stream offset delta) ptag (8) { f read4 =only } (least content stream length) ptag (9) { f read2 dup =only /nb9 exch def } (# bits for content stream length delta) ptag (10) { f read2 dup =only /nb10 exch def } (# bits for # of shared obj refs) ptag (11) { f read2 dup =only /nb11 exch def } (# bits for shared obj indices) ptag (12) { f read2 dup =only /nb12 exch def } (# bits for shared obj ref pos numerators) ptag (13) { f read2 =only } (shared obj ref pos denominator) ptag (*1) { [ npages { f nb3 readn } repeat ] ==only } (# objs/page deltas (see 1,3)) ptag (*2) { [ npages { f nb5 readn } repeat ] ==only } (# bytes/page deltas (see 4,5)) ptag (*3) { [ npages { f nb10 readn } repeat ] dup ==only /nso exch def } (# of shared obj refs (see 10)) ptag (*4) { [ nso { [ exch { f nb11 readn } repeat ] } forall ] ==only } (shared obj indices (see 11)) ptag (*5) { [ nso { [ exch { f nb12 readn } repeat ] } forall ] ==only } (shared obj ref pos numerators (see 12)) ptag (*6) { [ npages { f nb7 readn } repeat ] ==only } (content stream offset-in-page deltas (see 6,7)) ptag (*7) { [ npages { f nb9 readn } repeat ] ==only } (content stream length deltas (see 8,9)) ptag end % temp dict } bind def % Print the Shared Objects Hint Table. /psoht { % psoht - 20 dict begin /f exch def readninit (1) { f read4 =only } (first shared obj #) ptag (2) { f read4 =only } (first shared obj offset (+PHS length if beyond PHS)) ptag (3) { f read4 dup =only /n3 exch def } (# of p. 1 shared objs) ptag (4) { f read4 dup =only /n4 exch def } (total # of shared objs) ptag (5) { f read2 dup =only /nb5 exch def } (# bits for # of shared objs/group) ptag (6) { f read4 =only } (least shared obj group length) ptag (7) { f read2 dup =only /nb7 exch def } (# bits for shared obj group length delta) ptag /nse n4 def (*1) { [ nse { f nb7 readn } repeat ] ==only } (shared obj group length deltas (see 6,7)) ptag (*2) { [ nse { f 1 readn } repeat ] dup ==only /md5s exch def } (MD5 present?) ptag (*3:) = md5s { 0 ne { ( ) print f 16 string readstring pop (%stdout) (w) file dup 3 -1 roll writehexstring closefile () = } if } forall (*4) { [ nse { f nb5 readn } repeat ] ==only } (# objs/group (see 5)) ptag end % temp dict } bind def % Print the Primary Hint Stream of a PDF file. /pphs { % pphs - /pdf exch def % Read the linearization parameter dictionary. { pdf pdftoken /obj eq { exit } if } loop pdf pdftoken /lpdict exch def /lpdict type /dicttype eq { lpdict /Linearized known } { false } ifelse { (Not a linearized PDF file.) = stop } if lpdict plpdict flush % Read the primary hint stream. null { pdf pdftoken dup /stream eq { pop exit } if exch pop } loop /phsdict exch def % Remove Length if indirect reference. phsdict 0 known { phsdict 0 undef phsdict /Length undef } if (PHS: ) print phsdict === flush pdf 0 (endstream) /SubFileDecode filter dup 5000 string readstring pop exch closefile sread /phsdata exch def % Decode the hint stream data if necessary. phsdict /Filter .knownget { phsdata exch filter dup 5000 string readstring pop exch closefile sread /phsdata exch def } if % Adobe says we can assume /P = 0. (Page Offset Hint Table:) = lpdict /N get phsdata phsdict /S get string readstring pop sread ppoht (Shared Objects Hint Table:) = phsdata psoht } bind def % Check for command line arguments. [ shellarguments { ] dup length 1 eq { 0 get (r) file dup pphs closefile } { (Usage: pphs filename.pdf\n) print flush } ifelse } { pop } ifelse