summaryrefslogtreecommitdiff
path: root/gs/lib/dumphint.ps
blob: 444692a0c8385dde90a6850b8831d2f61cca1d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
% Copyright (C) 2000, 2001 Aladdin Enterprises.  All rights reserved.
%
% This software is provided AS-IS with no warranty, either express or
% implied.
%
% This software is distributed under license and may not be copied,
% modified or distributed except as expressly authorized under the terms
% of the license contained in the file LICENSE in this distribution.
%
% For more information about licensing, please refer to
% http://www.ghostscript.com/licensing/. For information on
% commercial licensing, go to http://www.artifex.com/licensing/ or
% contact Artifex Software, Inc., 101 Lucas Valley Road #110,
% San Rafael, CA  94903, U.S.A., +1(415)492-9861.

% $Id$
% Linearized PDF hint formatting utility.

%
% Linearized PDF hints generated by Acrobat suite seem to deviate from
% the published specification.
%
% /P (page offset hint table) key in hint stream is not generated by
% Adobe products. The key is no longer required in PDF 1.5.
%
% Per-page items 4 and 5 of the page offset hint table start from 1st
% page rather than 2nd page as the spec claims.
%
% All array entries start from the new byte boundary.
%

/table_width 79 def
/col1_width  66 def

% Skip bits to the next byte boundary
/bytealign {		% <stream> bytealign -
  begin /N 0 def /B 0 def end
} bind def

% Set bit stream position and align it to byte boundary
/set_align {            % <<>> pos set_align -
  exch begin
  S exch setfileposition
  /N 0 def /B 0 def
  end
} bind def

% Read requested number of bits from the bit stream.
/bitread {		% <bstream> <width> bitwrite <value>
  exch begin
  0                     %  bit val
  {
    1 index N .min      %  bit val m
    dup 3 1 roll        %  bit m val m
    bitshift            %  bit m val<<m
    B 2 index N sub     %  bit m val<<m B m-N
    bitshift add        %  bit m val<<m+B>>(N-m)
    3 -1 roll           %  m val' bit
    2 index sub         %  m val' bit'
    3 -1 roll           %  val' bit' m
    N exch sub dup      %  val' bit' N' N'
    1 exch bitshift     %  val' bit' N' 1<<N'
    1 sub B and         %  val' bit' N' B&(1<<N')
    /B exch def         %  val' bit' N'
    /N exch def         %  val' bit'
    dup 0 le {
      pop exit          %  val'
    } if
    /N N 8 add def
    /B B 8 bitshift S read not { 0 ( *** EOF! *** ) = } if add def
    exch
  } loop                %  bit' val'
  end
} bind def

% Print a string several times
/multiprint {           % cnt (s) multiprint -
  exch { dup print } repeat pop
} bind def

% Split the line into 2 substrings.
/split_line {           % (s) split_line () ()
  dup length col1_width gt {
    col1_width 1 sub
    dup -1 0 {
      dup               % (s) w i i
      3 index           % (s) w i i ()
      exch get          % (s) w i c
      32 eq {
        exch pop exit
      } if
      pop
    } for
    1 add               % (s) w'
    1 index exch        % (s) (s) w'
    0 exch              % (s) (s) 0 w'
    getinterval         % (s) (v)
  } {
    dup                 % (s) (s)
  } ifelse
  (\n) search {
    4 1 roll            % (pre) (s) (post) (match)
    pop pop             % (pre) (s)
    1 index length      % (pre) (s) len
    1 add               % (pre) (s) len+1
  } {
    exch                % (pre) (s)
    1 index length      % (pre) (s) len
  } ifelse
  1 index length        % (pre) (s) len Len
  1 index sub           % (pre) (s) len Len-len
  getinterval           % (pre) (post)
} bind def

% Print a 2 column table. The string is printed in 1st column
% left-aligned. The number is printed in 2nd column right-aligned.
/two_column {           % n () two_column -
  split_line            % n (a) ()
  3 1 roll              % () n (a)
  dup length            % () n (a) len
  exch print            % () n len
  exch =string cvs      % () len (n)
  dup length            % () len (n) len2
  3 -1 roll add         % () (n) len+len2
  //table_width         % () (n) len+len2 78
  exch sub              % () (n) 78-len+len2
  ( ) multiprint        % () (n)
  =                     % ()
  { dup length 0 eq { exit } if
    split_line exch =
  } loop
  pop
  () =
} bind def

% Print the header of a hint table
/table_header {               % () table_header -
  dup length dup
  table_width exch sub 2 idiv % () len sp
  dup ( ) multiprint          % () len sp
  3 -1 roll =                 % len sp
  ( ) multiprint              % len
  (=) multiprint
  ()= ()=
} bind def

% Pretty-print an array on top level
/dump_array {               % [ ] dump_array -
  ([) = ( ) print
  1 exch {
    =string cvs         % pos ()
    dup length dup      % pos () len len
    3 index add 1 add   % pos () len len+pos+1
    table_width gt {
      () =
      (  ) print        % pos () len
      2 add             % pos () pos'
      3 1 roll print pop % pos'
    } {
      ( ) print         % pos () len
      exch print        % pos len
      add 1 add         % pos'
    } ifelse
  } forall
  pop
  () = (]) =
} bind def

% Pretty-print an array on 2nd level
/dump_array2 {             % [ ] dump_array2 -
  (  [) print
  3 exch {
    =string cvs            % pos ()
    dup length dup         % pos () len len
    3 index add 1 add      % pos () len len+pos+1
    table_width 2 sub gt {
      () =
      (    ) print         % pos () len
      4 add                % pos () pos'
      3 1 roll print pop   % pos'
    } {
      ( ) print            % pos () len
      exch print           % pos len
      add 1 add            % pos'
    } ifelse
  } forall
  pop
  ( ]) =
} bind def

% Print an array header
/array_header {
  () = =
} bind def

% Analyze the page offset hint table.
/dump_page_offset_table {  % - dump_page_offset_table -
  hint_stream

  dup 32 bitread
  dup /hint_minnop exch def
  (1. The least number of objects in a page.) two_column

  dup 32 bitread
  dup /hint_1st_obj exch def
  (2. Location of the first page's page object.) two_column

  dup 16 bitread
  dup /hint_maxnopbits exch def
  (3. Bits for difference between max and min number of page objects.) two_column

  dup 32 bitread
  dup /hint_minpl exch def
  (4. Least length of a page.) two_column

  dup 16 bitread
  dup /hint_maxplbits exch def
  (5. Bits for difference between max and min length of a page.) two_column

  dup 32 bitread
  dup /hint_minsco exch def
  (6. Least start of Contents offset. )
  1 index 0 ne { (\n*** Acrobat expects 0 ***) concatstrings } if
  two_column

  dup 16 bitread
  dup /hint_maxscobits exch def
  (7. Bits for difference between max and min offset to the start of the content stream.) two_column

  dup 32 bitread
  dup /hint_mincl exch def
  (8. Least contents length.) two_column

  dup 16 bitread
  dup /hint_maxclbits exch def
  (9. Bits needed to represent the greatest Contents length.) two_column

  dup 16 bitread
  dup /hint_maxsorbits exch def
  (10. Bits needed to represent the greatest number of Shared Object references.) two_column

  dup 16 bitread
  dup /hint_sobits exch def
  (11. Bits needed to identify a Shared Object.) two_column

  dup 16 bitread
  dup /hint_numfbits exch def
  (12. Bits needed to represent numerator of fraction.) two_column

  dup 16 bitread
  dup /hint_denf exch def
  (13. Denominator of fraction.) two_column

  pop

  LinearizationParams /N get

  % 1. Number of objects in the page.
  hint_stream bytealign
  /hint_page_obj [
    2 index {
      hint_stream hint_maxnopbits bitread
      hint_minnop add
    } repeat
  ] readonly def

  (1. Number of objects on the page) array_header
  hint_page_obj dump_array

  % 2, Page length in bytes.
  hint_stream bytealign
  /hint_page_len [
    2 index {
      hint_stream hint_maxplbits bitread
      hint_minpl add
    } repeat
  ] readonly def

  (2. Page length in bytes.) array_header
  hint_page_len dump_array

  % 3, Number of shared objects referenced from the page
  hint_stream bytealign
  /hint_page_sobj [
    2 index {
      hint_stream hint_maxsorbits bitread
    } repeat
  ] readonly def

  (3. Number of shared objects referenced from the page.) array_header
  hint_page_sobj dump_array

  % 4. Index into the shared objects hint table
  hint_stream bytealign
  /hint_page_sobj_id [
    0 1 4 index 1 sub {
      hint_page_sobj exch get [
        exch {
          hint_stream hint_sobits bitread
        } repeat
      ] readonly
    } for
  ] readonly def

  (4. Index into the shared objects hint table.) array_header
  ([) =
  hint_page_sobj_id { dump_array2 } forall
  (])=

  % 5. Fractional position for each shared object reference
  hint_stream bytealign
  /hint_page_sobj_pos [
    0 1 4 index 1 sub {
      hint_page_sobj exch get [
        exch {
          hint_stream hint_numfbits bitread
          hint_denf div
        } repeat
      ] readonly
    } for
  ] readonly def

  (5. Fractional position for each shared object reference. ) array_header
  ([)=
  hint_page_sobj_pos { dump_array2 } forall
  (])=

  % 6. Offset of the page's content stream from the beginning of the page.
  hint_stream bytealign
  /hint_page_content_offset [
    2 index {
      hint_stream hint_maxscobits bitread
      hint_minsco add
    } repeat
  ] readonly def

  (6. Offset of the page's content stream from the beginning of the page.) array_header
  hint_page_content_offset dump_array

  % 7. Length of the page's content stream in bytes.
  hint_stream bytealign
  /hint_page_content_len [
    2 index {
      hint_stream hint_maxclbits bitread
      hint_mincl add
    } repeat
  ] readonly def

  (7. Length of the page's content stream in bytes.) array_header
  hint_page_content_len dump_array

  pop
} bind def

% Analyze tha shared object hint table
/dump_shared_object_table {
  hint_stream

  dup 32 bitread
  dup /shint_1st_obj_id exch def
  (1. Object number of the first object in the shared objects section) two_column

  dup 32 bitread
  dup /shint_1st_obj_pos exch def
  (2. Location of the first object in the shared objects section.) two_column

  dup 32 bitread
  dup /shint_1st_shared exch def
  (3. The number of shared object entries for the first page.) two_column

  dup 32 bitread
  dup /shint_all_shared exch def
  (4. Number of shared object entries for the shared objects section including 1st page.) two_column

  dup 16 bitread
  dup /shint_group_bits exch def
  (5. Number of bits needed to represent the greatest number of objects in a shared object group.) two_column

  dup 32 bitread
  dup /shint_group_least_sz exch def
  (6. Least length of a shared object group in bytes.) two_column

  dup 16 bitread
  dup /shint_group_diff_bits exch def
  (7. Bits for the difference between the greatest and least length of a shared object group size.)  two_column
  pop

  (1. length of the object group in bytes.) array_header
  hint_stream bytealign
  /shint_group_sz [
    shint_all_shared {
      hint_stream shint_group_diff_bits bitread
      shint_group_least_sz add
    } repeat
  ] readonly def

  shint_group_sz dump_array

  (2. MD5 signature flag) array_header
    hint_stream bytealign
    /shint_md5_flags
    [ shint_all_shared
      { hint_stream 1 bitread
      } repeat
    ] readonly def

    shint_md5_flags dump_array

    (3. MD5 signature string) array_header
    false shint_md5_flags { 0 ne or } forall {
      shint_md5_flags {
        0 eq {
          (<>)=
        } {
          hint_stream /S get 128 string
          readstring pop
          dup length 128 eq {
            ==
          } { pop (Error reading nd5 string.) ==
          } ifelse
        } ifelse
      } forall
    } {
      () = (none) =
    } ifelse

    (4. The number of objects in the group.) array_header
    hint_stream bytealign
    /shint_group_cnt [
      shint_all_shared {
        hint_stream shint_group_bits bitread
      } repeat
    ] readonly def

    shint_group_cnt dump_array

  } bind def

% Analyze the thumbnail hint table.
/dump_thumbnail_table {
  hint_stream

  dup 32 bitread
  dup /thint_1st_obj_id exch def
  (1. Object number of the first thumbnail image.) two_column

  dup 32 bitread
  dup /thint_1st_obj_pos exch def
  (2. Location of the first thumbnail image.) two_column

  dup 32 bitread
  dup /thint_page_cnt exch def
  (3. Number of pages that have thumbnail images.) two_column

  dup 16 bitread
  dup /thint_no_thumbnail_bits exch def
  (4. Bits for the max number of consecutive pages without a thumbnail image.) two_column

  dup 32 bitread
  dup /thint_min_sz exch def
  (5. The least length of a thumbnail image in bytes.) two_column

  dup 15 bitread
  dup /thint_obj_sz_bits exch def
  (6. Bits for the difference between max and min length of a thumbnail image.) two_column

  dup 32 bitread
  dup /thint_min_obj_cnt exch def
  (7. The least number of objects in a thumbnail image.) two_column

  dup 16 bitread
  dup /thint_obj_cnt_bits exch def
  (8. Bits for the difference between max and min number of objects in a thumbnail image.) two_column

  dup 32 bitread
  dup /thint_1st_shared_obj exch def
  (9. First object in the thumbnail shared objects section.) two_column

  dup 32 bitread
  dup /thint_1st_shared_pos exch def
  (10. Location of the first object in the thumbnail shared objects section.) two_column

  dup 32 bitread
  dup /thint_shared_cnt exch def
  (11. Number of thumbnail shared objects.) two_column

  dup 32 bitread
  dup /thint_shared_section_sz exch def
  (12. Length of the thumbnail shared objects section in bytes.) two_column

  pop
  LinearizationParams /N get

  (1. The number of preceding pages lacking a thumbnail image.) array_header
  hint_stream bytealign
  /thint_no_thumbnail_pages [
    2 index {
      hint_stream thint_no_thumbnail_bits bitread
    } repeat
  ] readonly def

  thint_no_thumbnail_pages dump_array

  (2. Number of objects in this page's thumbnail image.) array_header
  hint_stream bytealign
  /thint_page_obj_cnt [
    2 index {
      hint_stream thint_obj_cnt_bits bitread
      thint_min_obj_cnt add
    } repeat
  ] readonly def

  thint_page_obj_cnt dump_array

  (3. Length of this page's thumbnail image in bytes.) array_header
  hint_stream bytealign
  /thint_page_obj_sz [
    2 index {
      hint_stream thint_obj_sz_bits bitread
      thint_min_sz add
    } repeat
  ] readonly def

  thint_page_obj_sz dump_array
  pop
} bind def

% Analyze the generic hint table.
% The hint field names are re-used.
/dump_generic_table {
  hint_stream

  dup 32 bitread
  dup /ghint_1st_obj exch def
  (1. Object number of the first object in the group.) two_column

  dup 32 bitread
  dup /ghint_1st_obj_pos exch def
  (2. Location of the first object in the group.) two_column

  dup 32 bitread
  dup /ghint_obj_cnt exch def
  (3. Number of objects in the group.) two_column

  dup 32 bitread
  dup /ghint_group_sz exch def
  (4. Length of the object group in bytes.) two_column

  pop
} bind def

% Analyze the interactive hint table.
% The hint field names are re-used.
/dump_interactive_table {
  hint_stream

  dup 32 bitread
  dup /ihint_1st_obj exch def
  (1. Object number of the first object in the group.) two_column

  dup 32 bitread
  dup /ihint_1st_obj_pos exch def
  (2. Location of the first object in the group.) two_column

  dup 32 bitread
  dup /ihint_obj_cnt exch def
  (3. Number of objects in the group.) two_column

  dup 32 bitread
  dup /ihint_group_sz exch def
  (4. Length of the object group in bytes.) two_column

  dup 32 bitread
  dup /ihint_shared_cnt exch def
  (5. Number of shared object references.) two_column

  dup 16 bitread
  dup /ihint_shared_obj_bits exch def
  (6. Bits for the max shared object id used by the interactive form or the logical structure hierarchy.)
  1 index hint_sobits ne {
    (\n*** This fiels is not equal to max shared object ID bits ***) concatstrings
  } if

  pop

  (7. Shared object identifier.) array_header
  hint_stream bytealign
  /ihint_shared_obj_id [
    ihint_shared_cnt {
      hint_stream hint_sobits bitread
    } repeat
  ] readonly def

  ihint_shared_obj_id dump_array
} bind def

% Enumerate all documented hint tables.
/dump_all_tables {  % <<stream>> dump_all_tables -
  (Page offset hint table) table_header
  hint_stream 0 set_align
  dump_page_offset_table

  (S, Shared object hint table) table_header
  dup /S .knownget {
    hint_stream exch set_align
    dump_shared_object_table
  } {
    (Required table is not found.) error_msg
  } ifelse

  dup /T .knownget {
    (T, Thumbnail hint table) table_header
    hint_stream exch set_align
    dump_thumbnail_table
  } if

  dup /O .knownget {
    (O, Outline hint table) table_header
    hint_stream exch set_align
    dump_generic_table
  } if

  dup /A .knownget {
    (A, Thread information hint table) table_header
    hint_stream exch set_align
    dump_generic_table
  } if

  dup /E .knownget {
    (E, Named destination hint table) table_header
    hint_stream exch set_align
    dump_generic_table
  } if

  dup /V .knownget {
    (V, Interactive form hint table) table_header
    hint_stream exch set_align
    dump_interactive_table
  } if

  dup /I .knownget {
    (I, Information dictionary hint table) table_header
    hint_stream exch set_align
    dump_generic_table
  } if

  dup /C .knownget {
    (C, Logical structure hint table) table_header
    hint_stream exch set_align
    dump_interactive_table
  } if

  dup /L .knownget {
    (L, Page label hint table) table_header
    hint_stream exch set_align
    dump_generic_table
  } if

    pop
} bind def

% Load PDF file and extract the hint stream.
/pdf_dump_hints {                             % <infile> pdf_dump_hints -
  dup (r) file                                % fname
  false exch                                  % fname F file
  {
    dup 7 string readstring pop               % fname F file ()
    (%PDF-1.) ne { pop exit } if              % fname F file
    dup 0 setfileposition
    dup token not { pop exit } if             % fname F file obj
    dup type /integertype ne { pop exit } if  % fname F file obj
    1 index token not { pop pop exit } if     % fname F file obj gen
    dup type /integertype ne {pop pop exit}if % fname F file obj gen
    4 2 roll                                  % fname obj gen F file
    dup 0 setfileposition
    exch true or                              % fname obj gen file T
    exit
  } loop
  {
    pdfdict begin
    pdfopenfile dup begin
    40 dict begin
    /IDict exch def
    .setsafe

    % Read all objects into memory.
    Trailer touch
    resolveR                                  % fname <<>>
    dup /Linearized known {
      dup /L get                              % fname <<>> Len
      3 -1 roll status not { 0 0 0 0 } if     % <<>> Len
      pop pop exch pop                        % <<>> Len len
      eq {
        /LinearizationParams exch def
        LinearizationParams /H get
        dup length 2 eq {
          0 get PDFoffset add PDFfile exch setfileposition
          PDFfile token pop
          PDFfile token pop
          resolveR dup true resolvestream
          /ReusableStreamDecode filter
          bitstream dup bytealign
          /hint_stream exch def

          dump_all_tables
        } {
          pop
          (Overflow hint stream is not supported.) =
        } ifelse
      } {
        pop
        (Wrong file length in linearization dictionary.) =
      } ifelse
    } {
      pop (The file is not linearized.) =
    } ifelse
    end		% temporary dict
    end		% IDict
    end
  } {
    pop
    (Input file is not a valid PDF file.) =
  } ifelse
} bind def

% Initial setup
/dump_hints {
  counttomark 1 eq {
    exch pop
    save exch
    3000000 setvmthreshold
    pdfoptdict begin
    pdf_dump_hints
    end
    restore
    true
  } {
    cleartomark false
  } ifelse
} bind def

/shellarguments {false} def
(pdfopt.ps) runlibfile
currentdict /shellarguments undef

% Check for command line arguments.
mark shellarguments {
  dump_hints not {
    (Usage: gs -dNODISPLAY  -- dumphint.ps input.pdf) = flush
  } if
} {
  pop
} ifelse

% EOF