Debian Codegraph @

texk/texlive/linked_scripts/thumbpdf/thumbpdf.pl

raw
1 eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $argv:q'
2 if 0;
3 use strict;
4 $^W=1; # turn warning on
5 #
6 # thumbpdf.pl
7 #
8 # Copyright (C) 1999-2018 Heiko Oberdiek.
9 #
10 # This work may be distributed and/or modified under the
11 # conditions of the LaTeX Project Public License, either version 1.3
12 # of this license or (at your option) any later version.
13 # The latest version of this license is in
14 # http://www.latex-project.org/lppl.txt
15 # and version 1.3 or later is part of all distributions of LaTeX
16 # version 2003/12/01 or later.
17 #
18 # This work has the LPPL maintenance status "maintained".
19 #
20 # This Current Maintainer of this work is Heiko Oberdiek.
21 #
22 # See file "readme.txt" for a list of files that belong to this project.
23 #
24 # This file "thumbpdf.pl" may be renamed to "thumbpdf"
25 # for installation purposes.
26 #
27 my $file = "thumbpdf.pl";
28 my $program = uc($&) if $file =~ /^\w+/;
29 my $prj = 'thumbpdf';
30 my $version = "3.17";
31 my $date = "2018/09/07";
32 my $author = "Heiko Oberdiek";
33 my $copyright = "Copyright (c) 1999-2018 by $author.";
34 #
35 # Reqirements: Perl5, Ghostscript
36 # History:
37 # 1999/02/14 v1.0: First release.
38 # 1999/02/23 v1.1:
39 # * Looking for the media box to calculate the resolution
40 # for Ghostscript
41 # * new option --resolution
42 # 1999/03/01 v1.2:
43 # * optimization: indirect objects for length values removed.
44 # * "first line" from epstopdf
45 # 1999/03/12 v1.3:
46 # * Copyright: LPPL
47 # 1999/05/05 v1.4:
48 # * Detecting of cygwin32 environment.
49 # * Minor corrections of output of error messages.
50 # * Sharing RGB objects.
51 # 1999/06/13 v1.5:
52 # * gs detection extended.
53 # 1999/07/27 v1.6
54 # 1999/08/08 v1.7:
55 # * \immediate before \pdfobj (pdfTeX 0.14a)
56 # 1999/09/09 v1.8
57 # 1999/09/06 v1.9:
58 # * Check for direct /Length values (for jpg images)
59 # 2000/01/11 v1.10:
60 # * Bug fix: /Length (direct) as last entry.
61 # * Direct /Length in RGB objects supported.
62 # 2000/01/19 v1.11:
63 # * "for (my $j=0;...;...)" replaced by "my $j; for($j=0;...;...)",
64 # because there exist perl versions that have problems with.
65 # 2000/02/11 v1.12:
66 # * Option `clean' added.
67 # * The name of thumbnail data file: jobname.tnd,
68 # if thumbpdf is called: thumbpdf jobname[.pdf] [options]
69 # 2000/02/22 v2.0:
70 # * pdfmark support for dvips/ps2pdf route added.
71 # * <jobname>.tpt replaces thumbdta.tex (for pdfTeX)
72 # <jobname>.tpm (for pdfmark)
73 # <jobname>.top replaces thumbopt.tex
74 # * Options `useps', `modes' added,
75 # `makedef' renamed to `makedata'.
76 # 2000/02/28 v2.1:
77 # * Environment variable `THUMBPDF' supported.
78 # 2000/03/07 v2.2:
79 # * Support for Distiller 3 and 4, the streams are uncompressed.
80 # * Call of gs is changed in order to show the currently processed
81 # page number of the pdf file.
82 # * Option --printgscmd creates the command line file `thumbpdf.gs'
83 # for the Ghostscript call.
84 # 2000/03/22 v2.3:
85 # * Bug fix: --useps now works.
86 # 2000/04/10 v2.4:
87 # * Fix for ActiveState Perl 5.6.0: uc line changed, fork removed.
88 # (Thanks to Andreas Buehmann <[email protected]>.)
89 # * Version test for thumbpdf.tex added for users that
90 # mix versions, sigh.
91 # 2000/07/29 v2.5:
92 # * `save' trick in call of ghostscript.
93 # * Undocumented option --gspages added.
94 # 2000/09/27 v2.6
95 # 2000/10/27 v2.7:
96 # * -dFIXEDMEDIA=0 added in gs call
97 # 2001/01/12 v2.8:
98 # * Bug fix in dvips mode and active option `level2':
99 # pack parameter corrected for little-endian machines.
100 # * /Rotate in pdf pages:
101 # ghostscript versions around 6.01 have added a hack
102 # in /pdfshowpage_setpage, that ignores the /Rotate entry.
103 # A patch is added to disable the hack.
104 # * Ghostscript uses the MediaBox for calculating the
105 # page size. For version 6.50 a patch is added to use
106 # the /CropBox instead.
107 # 2001/03/29 v2.9:
108 # * Option --password added.
109 # 2001/04/02 v2.10
110 # 2001/04/26 v2.11
111 # * Option --antialias added (suggestion of Juergen Bausa).
112 # 2002/01/11 v3.0
113 # * Syntax of option --antialias changed (see readme.txt).
114 # * Support for VTeX's PS mode added.
115 # * Greek mode added (see readme.txt).
116 # * Signal handlers added for cleanup.
117 # 2002/05/26 v3.1
118 # * SIG_HUP unkown in Windows.
119 # * Bug fix: The signal function for __DIE__ "cleanup" aborts
120 # before the error message of "die" is printed.
121 # Replaced by "clean" that does not contain "exit 1".
122 # * Small bug fix in mode detection and mode "vtex"
123 # removed from list. "vtexpdfmark" was detected,
124 # "vtex" did not work and perhaps it will be used
125 # later for VTeX in PDF mode.
126 # 2002/05/26 v3.2
127 # * Fix: "MacOS/X: darwin" is now not interpreted as
128 # Windows.
129 # 2003/03/19 v3.3
130 # * Fix for gs 8.00 in mode dvips:
131 # THB_DistillerPatch also applied to ghostscript >= 8.00.
132 # 2003/06/06 v3.4
133 # * Bug fix, two forgotten "pop"s added for Distiller case.
134 # 2004/10/24 v3.5
135 # * Revert Cygwin detection: is unix (request by Jan Nieuwenhuizen).
136 # * LPPL 1.3.
137 # 2004/11/19 v3.6
138 # * Bug fix for dvips mode and gs < 8.00 (/stackunderflow in pop).
139 # 2004/11/19 v3.7:
140 # * For easier debugging, the special thumbpdf objects of
141 # thumbpdf.pdf are now valid PDF objects (dictionaries).
142 # * Remove of extra '\n' before "endstream" that is added
143 # by pdfTeX 1.20a.
144 # 2005/07/06 v3.8:
145 # * Fix because of pdfTeX 1.30.
146 # 2007/11/07 v3.9:
147 # * Deprecation warning of perl 5.8.8 fixed.
148 # 2008/04/16 v3.10
149 # 2010/07/07 v3.11
150 # * \input is used with file name extension for "thumbpdf.tex".
151 # 2011/08/10 v3.13
152 # * Use gswin64c in Windows with 64 bits.
153 # 2012/04/09 v3.14
154 # 2012/04/18 v3.15
155 # * Option --version added.
156 # 2014/07/15 v3.16
157 # * Patch for "thumbpdf.pl" by Norbert Preining because of
158 # pdfTeX 1.40.15 (TeX Live 2014).
159 # 2018/09/07 v3.17
160 # * { } quoted as \{ \} in regex for "new" stricter perl syntax.
161 #
162
163 ### program identification
164 my $title = "$program $version, $date - $copyright\n";
165
166 ### error strings
167 my $Error = "!!! Error:"; # error prefix
168
169 ### string constants for Ghostscript run
170 # get Ghostscript command name
171 my $GS = "gs";
172 $GS = "gs386" if $^O =~ /dos/i;
173 $GS = "gsos2" if $^O =~ /os2/i;
174 if ($^O =~ /mswin32c/i) {
175 # http://perldoc.perl.org/perlport.html#DOS-and-Derivatives
176 use Config;
177 $GS = "gswin32c";
178 $GS = "gswin64c" if $Config{'archname'} =~ /mswin32-x64/i;
179 }
180
181 # Windows detection (no SIGHUP)
182 my $Win = 0;
183 $Win = 1 if $^O =~ /mswin32/i;
184
185 my $gspages = 1;
186 $gspages = 0 if $^O =~ /dos/i;
187
188 ### variables
189 my $jobname = "";
190 my $jobfile = "";
191 my $pdftexfile = "";
192 my $pdfmarkfile = "";
193 my $psext = ".ps";
194 my $pdfext = ".pdf";
195 my $pdftexext = ".tpt";
196 my $pdfmarkext = ".tpm";
197 my $thumbprefix = "thb";
198 my $envvar = "THUMBPDF";
199 my $pdffile = "thumbpdf.pdf";
200 my $logfile = "thumbpdf.log";
201 my $texfile = "thumbpdf.tex";
202 my $package = "thumbpdf.sty";
203 my $readme = "readme.txt";
204 my $gscnffile = "thumbpdf.gs";
205 my $gssection = "section I. `Known Problems'";
206 my @cleanlist = ();
207 my $resolution = 9;
208 my $mode_pdftex = 0;
209 my $mode_pdfmark = 0;
210 my $antialias_default = "4";
211 my @arglist = @ARGV;
212 my $gskidrunning = 0;
213
214 ### option variables
215 my @bool = ("false", "true");
216 $::opt_device = "png16m";
217 $::opt_compress = "10";
218 $::opt_resolution = "";
219 $::opt_modes = "pdftex";
220 $::opt_gscmd = "";
221 $::opt_level2 = 0;
222 $::opt_help = 0;
223 $::opt_version = 0;
224 $::opt_quiet = 0;
225 $::opt_debug = 0;
226 $::opt_verbose = 0;
227 $::opt_useps = 0;
228 $::opt_printgscmd = 0;
229 $::opt_gspages = $gspages; # undocumented
230 $::opt_makepng = 1;
231 $::opt_makepdf = 1;
232 $::opt_makedata = 1;
233 $::opt_clean = 1;
234 $::opt_password = "";
235 $::opt_antialias = $antialias_default;
236 $::opt_greek = 0;
237
238 my $usage = <<"END_OF_USAGE";
239 ${title}Syntax: \L$program\E [options] <jobname[.pdf|.ps]>
240 Function: Support of thumbnails for pdfTeX or dvips/ps2pdf (pdfmark).
241 Thumbnails are generated by Ghostscript and the result is
242 written to data files for package `$package':
243 `<jobname>$pdftexext' (pdfTeX), `<jobname>$pdfmarkext' (pdfmark)
244 Options: (defaults:)
245 --help print usage
246 --version print version number
247 --(no)quiet suppress messages ($bool[$::opt_quiet])
248 --(no)verbose verbose printing ($bool[$::opt_verbose])
249 --(no)debug debug informations ($bool[$::opt_debug])
250 --(no)makepng make thumbnails `$thumbprefix*.png' ($bool[$::opt_makepng])
251 --(no)makepdf make `$pdffile' with thumbnails as images ($bool[$::opt_makepdf])
252 --(no)makedata make data file(s) for package `$package' ($bool[$::opt_makedata])
253 --(no)clean clear temp files ($bool[$::opt_clean])
254 --(no)useps `makepng' uses `.ps' instead of `.pdf' file ($bool[$::opt_useps])
255 --(no)level2 `<jobname>.tpm' with ps level 2 features ($bool[$::opt_level2])
256 --(no)greek text in greek style (experimental) ($bool[$::opt_greek])
257 --gscmd <name> call of ghostscript ($GS)
258 --antialias <num1>[num2] anti-aliasing, 0 = disable, 4 = max ($::opt_antialias)
259 --device|png [png]<dev> Ghostscript device for thumbnails,
260 dev = mono, gray, 16, 256, 16m ($::opt_device)
261 --resolution <res> thumbnail resolution for makepng ($resolution)
262 --compress <n> thumbnail compress level, n = 0..10 ($::opt_compress)
263 --modes <mode>[,mode] mode=pdftex|pdfmark|dvips|ps2pdf|
264 vtexpdfmark|all ($::opt_modes)
265 --password <password> for an encrypted pdf file ($::opt_password)
266 END_OF_USAGE
267
268 ### environment variable THUMBPDF
269 if ($ENV{$envvar}) {
270 unshift(@ARGV, split(/\s+/, $ENV{$envvar}));
271 }
272
273 ### process options
274 my @OrgArgv = @ARGV;
275 use Getopt::Long;
276 GetOptions(
277 "help!",
278 "version!",
279 "quiet!",
280 "debug!",
281 "verbose!",
282 "device|png=s",
283 "gscmd=s",
284 "level2!",
285 "compress=i",
286 "resolution=f",
287 "modes=s",
288 "useps!",
289 "printgscmd!",
290 "gspages!",
291 "makepng!",
292 "makepdf!",
293 "makedata!",
294 "clean!",
295 "password=s",
296 "antialias=s",
297 "greek!"
298 ) or die $usage;
299 !$::opt_help or die $usage;
300 if ($::opt_version) {
301 print "$prj $date v$version\n";
302 exit(0);
303 }
304 @ARGV < 2 or die "$usage$Error Too many files!\n";
305 @ARGV == 1 or die "$usage$Error Missing jobname!\n";
306
307 $::opt_device = "png$::opt_device" unless $::opt_device =~ /^png/;
308 $::opt_quiet = 0 if $::opt_verbose;
309 $::opt_clean = 0 if $::opt_debug or !$::opt_makepdf or !$::opt_makedata;
310
311 $::opt_compress = 0 if $::opt_compress < 0;
312 $::opt_compress = 10 if $::opt_compress > 10;
313 my $J = "^^J";
314 $J = "" if $::opt_compress == 10;
315
316 $::opt_antialias = $antialias_default if $::opt_antialias eq "";
317 $::opt_antialias =~ /^[0124][0124]?$/ or
318 die "$usage$Error Wrong value for option --antialias!\n";
319 $::opt_antialias .= $::opt_antialias if length($::opt_antialias) < 2;
320 my $AntiAliasText = substr($::opt_antialias, 0, 1);
321 my $AntiAliasGraphics = substr($::opt_antialias, 1, 1);
322 $AntiAliasText = "1" if $AntiAliasText eq "0";
323 $AntiAliasGraphics = "1" if $AntiAliasGraphics eq "0";
324
325 $GS = $::opt_gscmd if $::opt_gscmd;
326 $gspages = $::opt_gspages;
327
328 ### get modes
329 $::opt_modes = "\L$::opt_modes\E";
330 $::opt_modes =~ s/dvips/pdfmark/g;
331 $::opt_modes =~ s/ps2pdf/pdfmark/g;
332 $::opt_modes =~ s/vtexpdfmark/pdfmark/g;
333 $::opt_modes =~ s/vtexpdfmark/pdfmark/g;
334 if ($::opt_modes =~ /pdftex/)
335 {
336 $mode_pdftex = 1;
337 $::opt_modes =~ s/pdftex//g;
338 }
339 if ($::opt_modes =~ /pdfmark/)
340 {
341 $mode_pdfmark = 1;
342 $::opt_modes =~ s/pdfmark//g;
343 }
344 if ($::opt_modes =~ /all/)
345 {
346 $mode_pdftex = 1;
347 $mode_pdfmark = 1;
348 $::opt_modes =~ s/all//g;
349 }
350 $::opt_modes =~ s/\s+//g;
351 $::opt_modes =~ s/,+/,/g;
352 $::opt_modes =~ s/^,//;
353 $::opt_modes =~ s/,$//;
354 if ($::opt_modes ne "")
355 {
356 die "$usage$Error Unknown mode(s): `$::opt_modes'\n";
357 }
358 if ($::opt_makedata)
359 {
360 $mode_pdftex or $mode_pdfmark or
361 die "$usage$Error Missing mode!\n";
362 }
363
364 ### get jobname
365 $jobname = $ARGV[0];
366 if ($::opt_useps)
367 {
368 $jobname =~ s/\.ps$//i;
369 $jobname =~ s/\\/\//g;
370 $jobfile = $jobname . $psext;
371 }
372 else
373 {
374 $jobname =~ s/\.pdf$//i;
375 $jobname =~ s/\\/\//g;
376 $jobfile = $jobname . $pdfext;
377 }
378 $pdftexfile = $jobname . $pdftexext;
379 $pdfmarkfile = $jobname . $pdfmarkext;
380
381 print $title unless $::opt_quiet;
382
383 print "* jobname: `$jobname'\n" if $::opt_verbose;
384
385 if ($::opt_debug) {
386 print <<"END_DEB";
387 * OSNAME: $^O
388 * PERL_VERSION: $]
389 * ARGV: @OrgArgv
390 END_DEB
391 }
392
393 ### set signals
394 $SIG{__DIE__} = \&clean;
395 setsignals(\&cleanup);
396
397 my $MaxThumb = 0;
398
399 ###
400 ### make thumbnails
401 ###
402 if ($::opt_makepng)
403 {
404 print "*** make png files / run Ghostscript ***\n"
405 unless $::opt_quiet or $::opt_printgscmd;
406 if ($::opt_useps)
407 {
408 print "* ps file: $jobfile\n" if $::opt_verbose;
409 }
410 else
411 {
412 print "* pdf file: $jobfile\n" if $::opt_verbose;
413 }
414 print "* Ghostscript command: `$GS'\n" .
415 "* Ghostscript png device: `$::opt_device'\n" if $::opt_verbose;
416
417 if ($::opt_resolution)
418 {
419 $resolution = $::opt_resolution
420 }
421 else
422 {
423 # looking for MediaBox
424
425 my $max_x = 0;
426 my $max_y = 0;
427 {
428 my $MB = $jobfile;
429 open(MB, $MB) or die "$Error Cannot open `$MB'!\n";
430 binmode(MB);
431 my $xy_patt = '[\-\.\d]';
432 while (<MB>)
433 {
434 if (/\/MediaBox\s*\[\s*($xy_patt+)\s+($xy_patt+)\s+($xy_patt+)\s+($xy_patt+)\s*\]/)
435 {
436 my $x = $3 - $1;
437 my $y = $4 - $2;
438 $max_x = $x if $x > $max_x;
439 $max_y = $y if $y > $max_y;
440 }
441 }
442 close(MB);
443 }
444 if ($max_x <= 0 || $max_y <= 0)
445 {
446 print "!!! Warning: MediaBox not found, " .
447 "using default resolution: $resolution DPI\n";
448 }
449 else
450 {
451 print "* Max. Size of MediaBox: $max_x x $max_y\n" if $::opt_verbose;
452
453 my $rx = 106 * 72 / $max_x;
454 my $ry = 106 * 72 / $max_y;
455 $resolution = $rx;
456 $resolution = $ry if $ry < $rx;
457 print "* Resolution: $resolution DPI\n" if $::opt_verbose;
458 }
459 }
460
461 # Ghostscript's pdfshowpage_setpage is patched for solving
462 # some problems:
463 # * gs6.0* includes a hack that ignores the /Rotate entry
464 # in the PDF page, if OutputFile is set.
465 # gs6.50 does not need a fix and it is not applied,
466 # because pdfshowpage_setpage does not contain /OutputFile.
467 # * If /CropBox is set, then it should be used instead
468 # of the /MediaBox entry. Because the CropBox area should
469 # be part of the MediaBox, the MediaBox is overwritten
470 # with the CropBox values for generating the thumbnails.
471 # The fixes are only applied for versions >= 6.0, because
472 # gs5.50 gets a /PageSize problem with this fix.
473 #
474 my $SetPageHack = <<'SET_PAGE_HACK';
475 currentglobal true setglobal
476 false
477 /product where {
478 pop
479 product (Ghostscript) search {
480 pop pop pop
481 revision 600 ge {
482 pop true
483 } if
484 }{pop} ifelse
485 } if
486 { /pdfdict where {
487 pop
488 pdfdict begin
489 /pdfshowpage_setpage
490 [ pdfdict /pdfshowpage_setpage get
491 { dup type /nametype eq
492 { dup /OutputFile eq
493 { pop /AntiRotationHack
494 }{
495 dup /MediaBox eq revision 650 ge and
496 { /THB.CropHack {
497 1 index /CropBox pget
498 { 2 index exch /MediaBox exch put
499 } if
500 } def
501 /THB.CropHack cvx
502 } if
503 } ifelse
504 } if
505 } forall
506 ] cvx def
507 end
508 } if
509 } if
510 setglobal
511 SET_PAGE_HACK
512
513 my $Greek = "";
514 $Greek = <<'END_GREEK' if $::opt_greek;
515 currentglobal true setglobal
516 userdict begin
517 % * Patch for `show'
518 /THB_ORG_show {show} bind def
519 /THB_greekstring
520 /.charboxpath where
521 {
522 pop
523 {
524 currentpoint newpath moveto
525 true .charboxpath closepath fill
526 } bind def
527 }{
528 {
529 {
530 1 string dup 0 4 -1 roll put
531 dup stringwidth pop exch
532 true charpath flattenpath pathbbox
533 2 index sub exch 3 index sub exch rectfill
534 0 rmoveto
535 } forall
536 } bind def
537 } ifelse
538 /show {
539 currentfont /FontType get 1 eq
540 {
541 dup
542 gsave
543 % assuming white background
544 [ currentrgbcolor ]
545 { 1 add 2 div } forall
546 setrgbcolor
547 THB_greekstring
548 grestore
549 stringwidth pop 0 rmoveto
550 }{
551 THB_ORG_show
552 } ifelse
553 } bind def
554
555 % * Patch for the PDF case
556 userdict /GS_PDF_ProcSet known
557 {
558 % GS_PDF_ProcSet is readonly, so it will be copied first
559 GS_PDF_ProcSet length 10 add dict dup
560 GS_PDF_ProcSet {
561 put dup
562 } forall
563 /GS_PDF_ProcSet exch def
564 dup
565 begin
566 % `setshowstate' contains the use of `show', so it has to
567 % be overwritten, because it was defined with `bind'.
568 % The definition is taken from `pdf_ops.ps'.
569 revision 710 lt
570 { % 5.50, 6.51, 7.00, 7.02
571
572 /setshowstate
573 { WordSpacing 0 eq TextSpacing 0 eq and
574 { TextRenderingMode 0 eq
575 { { setfillstate show } }
576 { { false charpath textrenderingprocs TextRenderingMode get exec } }
577 ifelse
578 }
579 { TextRenderingMode 0 eq
580 { WordSpacing 0 eq
581 { { setfillstate TextSpacing exch 0 exch ashow } }
582 { TextSpacing 0 eq
583 { { setfillstate WordSpacing exch 0 exch 32 exch widthshow } }
584 { { setfillstate WordSpacing exch TextSpacing exch 0 32 4 2 roll 0 exch awidthshow } }
585 ifelse
586 }
587 ifelse
588 }
589 { { WordSpacing TextSpacing
590 % Implement the combination of t3 and false charpath.
591 % Note that we must use cshow for this, because we
592 % can't parse multi-byte strings any other way.
593 % Stack: string xword xchar
594 { pop pop (x) dup 0 3 index put false charpath
595 % Stack: xword xchar ccode
596 3 copy 32 eq { add } { exch pop } ifelse 0 rmoveto pop
597 }
598 4 -1 roll cshow pop pop
599 textrenderingprocs TextRenderingMode get exec
600 }
601 }
602 ifelse
603 }
604 ifelse /Show gput
605 } bdef
606
607 }{ % 7.10
608
609 /setshowstate
610 { WordSpacing 0 eq TextSpacing 0 eq and
611 { TextRenderingMode 0 eq
612 { { setfillstate show } }
613 { { false charpath textrenderingprocs TextRenderingMode get exec } }
614 ifelse
615 }
616 { TextRenderingMode 0 eq
617 { WordSpacing 0 eq
618 { { setfillstate TextSpacing 0 Vexch 3 -1 roll ashow } }
619 { TextSpacing 0 eq
620 { { setfillstate WordSpacing 0 Vexch 32 4 -1 roll widthshow } }
621 { { setfillstate WordSpacing 0 Vexch 32
622 TextSpacing 0 Vexch 6 -1 roll awidthshow } }
623 ifelse
624 }
625 ifelse
626 }
627 { { WordSpacing TextSpacing
628 % Implement the combination of t3 and false charpath.
629 % Note that we must use cshow for this, because we
630 % can't parse multi-byte strings any other way.
631 % Stack: string xword xchar
632 { pop pop (x) dup 0 3 index put false charpath
633 % Stack: xword xchar ccode
634 3 copy 32 eq { add } { exch pop } ifelse 0 Vexch rmoveto pop
635 }
636 4 -1 roll cshow pop pop
637 textrenderingprocs TextRenderingMode get exec
638 }
639 }
640 ifelse
641 }
642 ifelse /Show gput
643 } bdef
644
645 } ifelse
646 end
647 readonly pop
648 } if
649 end
650 setglobal
651 END_GREEK
652
653 my $SetPassword = "";
654 $SetPassword = "/PDFPassword($::opt_password)def" if $::opt_password;
655
656 my $PSHeader = "save pop $SetPassword $SetPageHack $Greek";
657 $PSHeader =~ s/%\s.*\n/ /g;
658 $PSHeader =~ s/\s+/ /g;
659 $PSHeader =~ s/\s+([\(\/\[\]\{\}])/$1/g;
660 $PSHeader =~ s/([\)\[\]\{\}])\s+/$1/g;
661 $PSHeader =~ s/\s+$//;
662
663 my $AntiAlias = "";
664 $AntiAlias = "\n-dTextAlphaBits=$AntiAliasText\n" .
665 "-dGraphicsAlphaBits=$AntiAliasGraphics"
666 if $::opt_antialias;
667
668 my $gs_cmd = <<"GS_CMD_END";
669 $GS$AntiAlias
670 -dNOPAUSE
671 -dBATCH
672 -sDEVICE=$::opt_device
673 -r$resolution
674 -sOutputFile=$thumbprefix%d.png
675 -c "$PSHeader"
676 -f $jobfile
677 GS_CMD_END
678 # The trick with `save' comes from `ps2pdf':
679 # Doing an initial `save' helps keep fonts from being flushed
680 # between pages.
681
682 if ($::opt_printgscmd)
683 {
684 open(GSCNF, ">$gscnffile") or die "$Error Cannot open `$gscnffile'!\n";
685 $gs_cmd =~ s/^[^\r\n]+[\r\n]+//;
686 print GSCNF $gs_cmd;
687 close(GSCNF);
688 my $options = "@arglist";
689 $options =~ s/\s*--?pr[intgscmd]*\s*/ /i;
690 $options =~ s/^\s+//;
691 $options =~ s/\s+$//;
692 print <<"END_PERL" if $::opt_verbose;
693 * Perl interpreter: $^X
694 * Perl script: $0
695 END_PERL
696 print <<"END_GS";
697 1. Run `Ghostscript' manually:
698 ==> $GS \@$gscnffile
699 2. Call `thumbpdf' again with the additional option `--nomakepng':
700 ==> thumbpdf --nomakepng $options
701 END_GS
702 exit(0);
703 }
704
705 chomp($gs_cmd);
706 $gs_cmd =~ s/\n/ /mg;
707 print "> $gs_cmd\n" if $::opt_verbose;
708
709 if ($::opt_debug)
710 {
711 if ($gspages)
712 {
713 print "* Ghostscript with page numbers\n";
714 }
715 else
716 {
717 print "* Ghostscript without page numbers\n";
718 }
719 }
720
721 setsignals(\&gscleanup);
722 my $capture = "";
723 if ($gspages)
724 {
725 my $newline = 0;
726 open(KID, "$gs_cmd|") or die "$Error Cannot open Ghostscript ($!)!\n";
727 *::GSKID = *KID;
728 $gskidrunning = 1;
729 my $orgbar = $|;
730 $|=1;
731 while (<KID>)
732 {
733 $capture .= $_;
734 if ($::opt_verbose)
735 {
736 print;
737 }
738 else
739 {
740 if (!$::opt_quiet)
741 {
742 print if /^Processing pages/;
743 if (/^Page\s+(\d+)/)
744 {
745 print " " if $newline;
746 $newline = 1;
747 print "[$1]";
748 }
749 }
750 }
751 }
752 $gskidrunning =0;
753 if (!close(KID))
754 {
755 if ($!)
756 {
757 die "$Error Closing Ghostscript ($!)!\n";
758 }
759 else
760 {
761 my $exitvalue = $? >> 8;
762 die "$Error Closing Ghostscript (exit status: $exitvalue)!\n";
763 }
764 }
765 print "\n" if $newline;
766 $| = $orgbar;
767 }
768 else # without pages
769 {
770 $capture = `$gs_cmd`;
771 if (!defined($capture))
772 {
773 die "$Error Cannot execute Ghostscript!\n";
774 }
775 print $capture if $::opt_verbose;
776 }
777
778 if ($capture =~ /Error:\s*(.*)\n/)
779 {
780 die <<"END_DIE";
781 $Error `$1' (Ghostscript)!
782 See `$readme', $gssection, for further information.
783 END_DIE
784 }
785 if ($capture =~ /Unknown device:\s*(.*)\n/)
786 {
787 die "$Error Unknown device `$1' (Ghostscript)!\n";
788 }
789 if ($?)
790 {
791 my $exitvalue = $?;
792 if ($exitvalue > 255)
793 {
794 $exitvalue >>= 8;
795 die "$Error Closing Ghostscript (exit status: $exitvalue)!\n";
796 }
797 die "$Error Closing Ghostscript ($exitvalue)!\n";
798 }
799 if ($capture =~ /Processing pages \d+ through (\d+)./)
800 {
801 $MaxThumb = $1;
802 }
803 print "* max. page: $MaxThumb\n" if $::opt_debug;
804 setsignals(\&cleanup);
805 }
806
807 ###
808 ### make thumbpdf.pdf file
809 ###
810 if ($::opt_makepdf)
811 {
812 print "*** make `$pdffile' / run pdfTeX ***\n" unless $::opt_quiet;
813
814 if ($MaxThumb > 0)
815 {
816 my $i;
817 for ($i=1; $i<=$MaxThumb; $i++)
818 {
819 push(@cleanlist, "$thumbprefix$i.png");
820 }
821 }
822 else
823 {
824 # get max thumb number to speed up the pdfTeX run
825 $MaxThumb = 0;
826 foreach (glob("$thumbprefix*.png"))
827 {
828 next unless /$thumbprefix(\d+).png/;
829 $MaxThumb = $1 if $1 > $MaxThumb;
830 push(@cleanlist, $_);
831 }
832 }
833
834 push(@cleanlist, $logfile);
835 push(@cleanlist, $pdffile);
836
837 my $compress = $::opt_compress;
838 $compress = 9 if $::opt_compress == 10;
839 my $cmd = "pdftex \"" .
840 "\\nonstopmode" .
841 "\\pdfcompresslevel$compress" .
842 "\\def\\thumbjob{$jobname}" .
843 "\\def\\thumbmax{$MaxThumb}" .
844 "\\input $texfile" .
845 "\"";
846 print "> $cmd\n" if $::opt_verbose;
847 my @capture = `$cmd`;
848 if (!@capture)
849 {
850 die "$Error Cannot execute pdfTeX!\n";
851 }
852 if ($::opt_verbose)
853 {
854 print @capture;
855 }
856 else
857 {
858 foreach (@capture)
859 {
860 print if /^!\s/;
861 }
862 }
863 if ($?)
864 {
865 my $exitvalue = $?;
866 if ($exitvalue > 255)
867 {
868 $exitvalue >>= 8;
869 die "$Error Closing pdfTeX (exit status: $exitvalue)!\n";
870 }
871 die "$Error Closing pdfTeX ($exitvalue)!\n";
872 }
873 # test version
874 my $versionfound = 0;
875 foreach (@capture)
876 {
877 if (/File:.*thumbpdf.*(\d\d\d\d\/\d\d\/\d\d)\s+v(\d+\.\d+)/)
878 {
879 $versionfound = 1;
880 if ($1 ne $date or $2 ne $version) {
881 print <<"END_WARN";
882 !!! Warning: Version of `thumbpdf.tex' does not match with perl script!
883 Current `thumbpdf.tex': $1 v$2
884 Please install version: $date v$version
885 END_WARN
886 }
887 }
888 }
889 print "!!! Warning: Version of `thumbpdf.tex' not found!\n"
890 if !$versionfound;
891
892 $_ = pop(@cleanlist);
893 }
894
895 ###
896 ### parse thumbpdf.pdf
897 ###
898
899 if ($::opt_makedata)
900 {
901 push(@cleanlist, $pdffile);
902
903 print "*** parse `$pdffile' ***\n" unless $::opt_quiet;
904
905 ### reading file and parse obj structure
906
907 my @objno = (); # obj number
908 my @objdict = (); # boolean, object is dict
909 my @objtext = (); # text of object
910 my @objstream = (); # stream of object if any
911 my $maxobj = 0;
912
913 my @getobjindex = (); # $getobj[obj number] ==> index for $obj...[index]
914
915 # open file
916 my $PDF = $pdffile;
917 open(PDF, $PDF) or die "$Error Cannot open `$PDF'!\n";
918 binmode(PDF);
919 my $lineno = 0;
920
921 # read header
922 $_ = <PDF>; $lineno++;
923 $_ or die "$Error Cannot read header of `$PDF' or file is empty!\n";
924 /^%PDF/ or die "$Error No PDF specification found!\n";
925 print "* pdf header: $_" if ($::opt_debug);
926
927 # read body objects
928 my $count = 0;
929 while (<PDF>)
930 {
931 $lineno++;
932
933 # continue, if comment line (2nd line of PDF output by pdfTeX 1.30)
934 next if /^%/;
935
936 # stop at xref
937 last if /^xref$/;
938
939 # scan first obj line
940 /^(\d+)\s+0\s+obj\s*(<<)?$/ or
941 die "$Error `obj' expected on line $lineno!\n";
942 $objno[$count] = $1;
943 $getobjindex[$1] = $count;
944 $objdict[$count] = ($2); # boolean (if $2 exists)
945 if (!$objdict[$count]) {
946 # check for << on thext line, new PDF-X/2014
947 $_ = <PDF>;
948 if (/^<<$/) {
949 $objdict[$count] = 1;
950 $lineno++;
951 $_ = <PDF>;
952 $lineno++;
953 }
954 }
955 my $stream = 0;
956 print "* obj $objno[$count]" .
957 (($objdict[$count]) ? " (dict)" : "") .
958 "\n" if $::opt_debug;
959
960 # get obj
961 $objtext[$count] = "";
962 while ($_)
963 {
964 if ($objdict[$count])
965 {
966 if (/^>>/)
967 {
968 last if /^>>\s+endobj$/; # obj without stream
969
970 $_ = <PDF>; $lineno++;
971 last if /^endobj$/; # obj without stream, new PDF-X/2014
972
973 # get stream
974 /^stream$/ or die "$Error `stream' expected on line $lineno!\n";
975
976 print "* stream\n" if $::opt_debug;
977 $objstream[$count] = "";
978 while (<PDF>)
979 {
980 $lineno++;
981
982 if (/(.*)endstream$/)
983 {
984 $objstream[$count] .= $1;
985 last;
986 }
987 $objstream[$count] .= $_;
988 }
989
990 $_ = <PDF>; $lineno++;
991 /^endobj$/ or die "$Error `endobj' expected on line $lineno!\n";
992 last;
993 }
994 }
995 else # no dict
996 {
997 last if /^endobj$/;
998 }
999 $objtext[$count] .= $_;
1000
1001 $_ = <PDF>;
1002 $lineno++;
1003 }
1004 $count++;
1005 }
1006 close(PDF);
1007 $maxobj = $count;
1008 print "* $maxobj objects found.\n" if $::opt_debug;
1009
1010 ### get thumbnail page numbers
1011 my @thumbpageno = ();
1012 my $found = 0;
1013 foreach (@objtext)
1014 {
1015 if (/^<<\/ListThumbs\s+(.+)>>$/)
1016 {
1017 $_ = $1;
1018 chomp;
1019 @thumbpageno = split / /; # split(/ /, $_);
1020 print "* ListThumbs: @thumbpageno\n" if $::opt_debug;
1021 $found = 1;
1022 last;
1023 }
1024 }
1025 $found or die "$Error `/ListThumbs' not found!\n";
1026 {
1027 my $j;
1028 for ($j=0; $j<@thumbpageno; $j++)
1029 {
1030 $thumbpageno[$j] = $1 if $thumbpageno[$j] =~ /^{(.+)}$/;
1031 }
1032 }
1033
1034 ### identify thumb objects
1035
1036 my @thumbobj = (); # index for @obj... with image stream
1037 my @thumblength = (); # stream length values
1038 my @thumbrgbobj = (); # index for @obj... with rgb stream
1039 my @thumbrgblength = (); # rgb stream length values
1040 my $maxthumb = 0;
1041
1042 $count = 0;
1043 my $i;
1044 for ($i=0; $i<$maxobj; $i++)
1045 {
1046 if ($objtext[$i] =~
1047 /^\/Type\s+\/XObject\n\/Subtype\s+\/Image\n/m)
1048 {
1049 $thumbobj[$count] = $i;
1050 $_ = $';
1051 $objtext[$i] = $_;
1052
1053 # check width and height
1054 /\/Width\s+(\d+)\n\/Height\s+(\d+)/m or
1055 die "$Error width/height of thumbnail not found!\n";
1056 print "* Size: $1x$2\n" if $::opt_debug;
1057 print "==> Width ($1) " .
1058 "of thumbnail `$thumbpageno[$count]' " .
1059 "is larger than recommended (106).\n"
1060 if $1 > 106;
1061 print "==> Height ($2) " .
1062 "of thumbnail `$thumbpageno[$count]' " .
1063 "is larger than recommended (106).\n"
1064 if $2 > 106;
1065
1066 # get stream length
1067 if (/\/Length\s+(\d+)\s+([\/\>]|$)/m)
1068 {
1069 $thumblength[$count] = $1;
1070 print "* Length (direct): $1\n" if $::opt_debug;
1071 # remove whitespace after length obj
1072 $objtext[$i] =~ s/(\/Length\s+\d+)\s+\n/$1\n/;
1073 }
1074 else # looking for indirect reference
1075 {
1076 /\/Length\s+(\d+)\s+0\s+R/m or
1077 die "$Error `/Length' entry not found!\n";
1078 # save obj text for later correction
1079 my $objpre = $`;
1080 my $objpost = $';
1081 # look for length obj
1082 $getobjindex[$1] or die "$Error Length obj not found!\n";
1083 $objtext[$getobjindex[$1]] =~ /^(\d+)$/m or
1084 die "$Error length value not found!\n";
1085 $thumblength[$count] = $1;
1086 print "* Length (indirect): $1\n" if $::opt_debug;
1087 # insert obj length directly:
1088 $objtext[$i] = $objpre . "/Length $1" . $objpost;
1089 }
1090
1091 # remove \n from end of stream
1092 if ($thumblength[$count] < length($objstream[$i])) {
1093 chop($objstream[$i]);
1094 }
1095
1096 # check /Indexed /DeviceRGB
1097 if ($objtext[$i] =~
1098 /\/ColorSpace\s+\[\/Indexed\s+\/DeviceRGB\s+(\d+)\s+(\d+)\s+0\s+R\]/m)
1099 {
1100 # correct thumb object text
1101 $objtext[$i] =
1102 "$`/ColorSpace [/Indexed /DeviceRGB $1 \\the\\pdflastobj\\ 0 R]$'";
1103 # get RGB obj number
1104 $getobjindex[$2] or die "$Error RGB object not found!\n";
1105 $_ = $getobjindex[$2];
1106 $thumbrgbobj[$count] = $_;
1107 # get stream length
1108 if ($objtext[$_] =~ /\/Length\s+(\d+)\s+([\/\>]|$)/m)
1109 {
1110 $thumbrgblength[$count] = $1;
1111 print "* RGB length (direct): $1\n" if $::opt_debug;
1112 $objtext[$_] =~ s/(\/Length\s+\d+)\s+\n/$1\n/;
1113 }
1114 else # looking for indirect reference
1115 {
1116 $objtext[$_] =~ /\/Length\s+(\d+)\s+0\s+R/m or
1117 die "$Error Length entry of rgb object not found\n";
1118 # save obj text for later correction
1119 my $objrgbpre = $`;
1120 my $objrgbpost = $';
1121 # get rgb stream length
1122 $getobjindex[$1] or die "$Error RGB length object not found!\n";
1123 $objtext[$getobjindex[$1]] =~ /^(\d+)$/m or
1124 die "$Error length value not found!\n";
1125 $thumbrgblength[$count] = $1;
1126 print "* RGB length (indirect): $1\n" if $::opt_debug;
1127 # insert RGB object length directly:
1128 $objtext[$_] = $objrgbpre . "/Length $1" . $objrgbpost;
1129 }
1130 }
1131
1132 $count++;
1133 }
1134 }
1135 $maxthumb = $count;
1136
1137 if ($maxthumb != @thumbpageno)
1138 {
1139 my $pagecount = @thumbpageno;
1140 die "$Error $maxthumb thumbnails found, but there should be $pagecount!\n";
1141 }
1142 print "* $maxthumb thumbnails found.\n" if $::opt_verbose;
1143
1144
1145 ###
1146 ### write data files
1147 ###
1148
1149 my $timestamp;
1150 {
1151 my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
1152 $mon++;
1153 $year += 1900;
1154 $timestamp = sprintf("%04d/%02d/%02d %02d:%02d:%02d",
1155 $year, $mon, $mday, $hour, $min, $sec);
1156 }
1157 if ($mode_pdftex)
1158 {
1159 print "*** write `$pdftexfile' (pdfTeX thumbnail data) ***\n"
1160 unless $::opt_quiet;
1161 my $DTA_PT = $pdftexfile;
1162 open(DTA_PT, ">$DTA_PT") or die "$Error Cannot open `$DTA_PT'!\n";
1163 binmode(DTA_PT);
1164 print DTA_PT <<"END_DTA";
1165 % File: $DTA_PT
1166 % Producer: $program $version
1167 % Mode: pdftex
1168 % Date: $timestamp
1169 END_DTA
1170 }
1171 my $maxpagethumb;
1172 my $dictbegin;
1173 if ($mode_pdfmark)
1174 {
1175 print "*** write `$pdfmarkfile' (pdfmark thumbnail data) ***\n"
1176 unless $::opt_quiet;
1177 my $DTA_PM = $pdfmarkfile;
1178 open(DTA_PM, ">$DTA_PM") or die "$Error Cannot open `$DTA_PM'!\n";
1179 binmode(DTA_PM);
1180
1181 # write tex part
1182 print DTA_PM <<"END_DTA";
1183 % \\iffalse
1184 % File: $DTA_PM
1185 % Producer: $program $version
1186 % Mode: pdfmark
1187 % Date: $timestamp
1188 % \\fi
1189 END_DTA
1190
1191 # looking for max. number of regular thumbnails
1192 $maxpagethumb = $maxthumb;
1193 for ($i=0; $i<$maxobj; $i++)
1194 {
1195 if ($objtext[$i] =~ /<<\/MaxThumbNumber\s+(\d+)>>/)
1196 {
1197 $maxpagethumb = $1;
1198 last;
1199 }
1200 }
1201
1202 # write TeX part
1203 for ($i=$maxpagethumb; $i<$maxthumb; $i++)
1204 {
1205 print DTA_PM "% \\DefThisThumb{$thumbpageno[$i]}\n";
1206 }
1207
1208 # write PostScript header
1209 my $dictstart = <<'END_DICT';
1210 4 dict begin
1211 /enddict {
1212 counttomark 2 idiv dup dict begin {def} repeat pop
1213 currentdict end
1214 } bind def
1215 END_DICT
1216 chomp($dictstart);
1217 $dictbegin = "[";
1218 my $dictend = "enddict";
1219 my $filter = "";
1220 my $read = "readhexstring";
1221 if ($::opt_level2)
1222 {
1223 $dictstart = " 3 dict begin";
1224 $dictbegin = "<<";
1225 $dictend = ">>";
1226 $filter = " /ASCII85Decode filter";
1227 $read = "readstring";
1228 }
1229
1230 print DTA_PM <<"END_DTA";
1231 % \\endinput
1232 % TeX part ends here
1233
1234 % PostScript definitions
1235 END_DTA
1236
1237 my $PS_Header = <<"END_HEAD";
1238 %
1239 % Default definition of pdfmark
1240 /pdfmark where {pop} {userdict /pdfmark /cleartomark load put} ifelse
1241 %
1242 % Check the version of Ghostscript. If it is below 6.0,
1243 % the commands, that produce the thumbnails, are defined
1244 % to be dummies.
1245 %
1246 true
1247 /product where {
1248 pop
1249 product (Ghostscript) search {
1250 pop pop pop
1251 revision 600 lt {
1252 (!!! Warning (thumbpdf): Ghostscript 6.0 required for thumbnails!\\n)
1253 print pop false
1254 } if
1255 }{pop} ifelse
1256 } if
1257 {
1258 % Syntax: <thumb object> thisTHB -
1259 % thisTHB is used globally, so it is defined in the current
1260 % dictionary (perhaps userdict should explicitly be set).
1261 /thisTHB {[ exch /Thumb exch /PAGE pdfmark} bind def
1262 $dictstart
1263 % Syntax: <page object> <thumb object> pagethumb -
1264 /pagethumb {
1265 [ 3 1 roll $dictbegin exch /Thumb exch $dictend /PUT pdfmark
1266 } bind def
1267 % Syntax: <thumb object> <stream length>
1268 % <mark> <key value pairs> streamobj -
1269 %
1270 % Distiller ignores the compression of previously compressed
1271 % streams and uses its own settings. Therefore for Distiller
1272 % the streams are uncompressed.
1273 % Now ghostscript versions greater than 8 behave in the same way
1274 % as Distiller. Therefore detection for this versions is added.
1275 % The detection and patch for distiller require features of level 2
1276 % and the filter /FlateDecode (level 3), that is used by pdfTeX's
1277 % compression.
1278 /THB_DistillerPatch false def
1279 /languagelevel where {
1280 pop
1281 languagelevel 2 ge {
1282 product (Distiller) search {pop pop pop true}{pop false} ifelse
1283 product (Ghostscript) search {
1284 pop pop pop
1285 revision 800 ge
1286 }{pop false} ifelse
1287 or
1288 {
1289 (FlateDecode) {
1290 pop
1291 % Syntax: <dict> THB_DistillerPatch <dict> false
1292 % <dict> THB_DistillerPatch <dict> <filter>
1293 /THB_DistillerPatch {
1294 dup /Filter known {
1295 dup dup /Filter get exch /Filter undef
1296 }{false} ifelse
1297 } bind def
1298 } (FlateDecode) /Filter resourceforall
1299 } if
1300 } if
1301 } if
1302 /streamobj {
1303 $dictend exch
1304 % Stack: <thumb> <dict> <length>
1305 3 -1 roll dup
1306 % Stack: <dict> <length> <thumb> <thumb>
1307 [ /_objdef 3 -1 roll /type /stream /OBJ pdfmark
1308 % Stack: <dict> <length> <thumb>
1309 dup dup 5 -1 roll
1310 % Stack: <length> <thumb> <thumb> <thumb> <dict>
1311 THB_DistillerPatch
1312 % Stack: <length> <thumb> <thumb> <thumb> <dict> <filter/false>
1313 [ 4 -2 roll /PUT pdfmark
1314 % Stack: <length> <thumb> <thumb> <filter/false>
1315 [ 3 1 roll currentfile${filter}
1316 % Stack: <length> <thumb> [ <thumb> <filter/false> <file>
1317 6 -1 roll string $read pop
1318 % Stack: <thumb> [ <thumb> <filter/false> <string>
1319 exch dup type /booleantype ne {true} if {filter} if
1320 % Stack: <thumb> [ <thumb> <file>
1321 /PUT pdfmark
1322 % Stack: <thumb>
1323 [ exch /CLOSE pdfmark
1324 } bind def
1325 }{
1326 % Syntax: <thumb object> thisTHB -
1327 /thisTHB {pop} bind def
1328 2 dict begin
1329 % Syntax: <page object> <thumb object> pagethumb -
1330 /pagethumb {pop pop} bind def
1331 % Syntax: <thumb object> <stream length>
1332 % <mark> <key value pairs> streamobj -
1333 /streamobj {
1334 cleartomark exch pop
1335 string currentfile${filter}
1336 exch $read pop pop
1337 } bind def
1338 } ifelse
1339 END_HEAD
1340 $PS_Header =~ s/%[^\r\n]*[\r\n]+//gm;
1341 print DTA_PM $PS_Header;
1342 print DTA_PM <<"END_DTA";
1343
1344 % adding thumbnails to pages
1345 END_DTA
1346
1347 for ($i=0; $i<$maxpagethumb; $i++)
1348 {
1349 print DTA_PM <<"END_DTA";
1350 {Page$thumbpageno[$i]} {THB$thumbpageno[$i]} pagethumb
1351 END_DTA
1352 }
1353 print DTA_PM "\n% thumbnail data\n";
1354 }
1355
1356 for ($i=0; $i<$maxthumb; $i++)
1357 {
1358 # rgb object
1359 if ($thumbrgbobj[$i])
1360 {
1361 # find the same rgb object
1362 my $j;
1363 for ($j=0; $j<$i; $j++)
1364 {
1365 next unless $thumbrgbobj[$j];
1366 next unless $objtext[$thumbrgbobj[$j]] eq
1367 $objtext[$thumbrgbobj[$i]];
1368 next unless $objstream[$thumbrgbobj[$j]] eq
1369 $objstream[$thumbrgbobj[$i]];
1370 last;
1371 }
1372 if ($j==$i) # not found
1373 {
1374 if ($mode_pdftex)
1375 {
1376 {
1377 my $rgbstream = pdftexstream($objstream[$thumbrgbobj[$i]]);
1378 my $dict = $objtext[$thumbrgbobj[$i]];
1379 if ($::opt_compress == 10)
1380 {
1381 chomp($dict);
1382 $dict =~ s/\n([^\/])/^^J\n$1/mg;
1383 $dict =~ s/[ ]+\//\//mg;
1384 }
1385 else
1386 {
1387 $dict =~ s/\n/^^J\n/mg;
1388 }
1389 print DTA_PT <<"END_DTA";
1390 \\immediate\\pdfobj{<<$J
1391 $dict>>$J
1392 stream^^J
1393 $rgbstream
1394 endstream}
1395 \\DefRGB{$i}
1396 END_DTA
1397 }
1398 }
1399 if ($mode_pdfmark)
1400 {
1401 {
1402 my $rgbstream = pdfmarkstream($objstream[$thumbrgbobj[$i]]);
1403 my $rgblength = $thumbrgblength[$i];
1404 my $dict = $objtext[$thumbrgbobj[$i]];
1405 $dict =~ s/\/Length\s+\d+\s*//;
1406 $dict =~ s/^\s+//;
1407 $dict =~ s/\s+$//;
1408 print DTA_PM <<"END_DTA";
1409 {RGB_$i} $rgblength $dictbegin
1410 $dict
1411 streamobj
1412 $rgbstream
1413 END_DTA
1414 }
1415 }
1416 }
1417 else # $j with same rgb obj
1418 {
1419 $objtext[$thumbobj[$i]] =~
1420 s/\\the\\pdflastobj/\\UseRGB{$j}/;
1421 print "* Reuses RGB object $j for $i\n" if $::opt_debug;
1422 }
1423 }
1424
1425 # thumb object
1426 if ($mode_pdftex)
1427 {
1428 {
1429 my $dict = $objtext[$thumbobj[$i]];
1430 if ($::opt_compress == 10)
1431 {
1432 chomp($dict);
1433 $dict =~ s/\n([^\/])/^^J\n$1/mg;
1434 $dict =~ s/[ ]+\//\//mg;
1435 $dict =~ s/[ ]+\[/\[/mg;
1436 }
1437 else
1438 {
1439 $dict =~ s/\n/^^J\n/mg;
1440 }
1441 my $stream = pdftexstream($objstream[$thumbobj[$i]]);
1442 print DTA_PT <<"END_DTA";
1443 \\immediate\\pdfobj{<<$J
1444 $dict>>$J
1445 stream^^J
1446 $stream
1447 endstream}
1448 \\DefThumb{$thumbpageno[$i]}
1449 END_DTA
1450 }
1451 }
1452 if ($mode_pdfmark)
1453 {
1454 {
1455 my $stream = pdfmarkstream($objstream[$thumbobj[$i]]);
1456 my $length = $thumblength[$i];
1457 my $dict = $objtext[$thumbobj[$i]];
1458 $dict =~ s/\\the\\pdflastobj\\\s*\d+\s*R/{RGB_$i}/;
1459 $dict =~ s/\\UseRGB\{(\d+)\}\\\s*\d+\s*R/{RGB_$1}/;
1460 $dict =~ s/\/Length\s+\d+\s*//;
1461 $dict =~ s/^\s+//;
1462 $dict =~ s/\s+$//;
1463 my $thismarker = "";
1464 $thismarker = "_", if $i >= $maxpagethumb;
1465 print DTA_PM <<"END_DTA";
1466 {THB$thismarker$thumbpageno[$i]} $length $dictbegin
1467 $dict
1468 streamobj
1469 $stream
1470 END_DTA
1471 }
1472 }
1473 }
1474
1475 if ($mode_pdftex)
1476 {
1477 print DTA_PT "\\endinput\n";
1478 close(DTA_PT);
1479 }
1480 if ($mode_pdfmark)
1481 {
1482 print DTA_PM <<"END_DTA";
1483 end
1484 % end of thumbnail data file
1485 END_DTA
1486 close(DTA_PM);
1487 }
1488 }
1489
1490 sub pdftexstream
1491 {
1492 my $str = "";
1493 my $mod = 0;
1494 foreach (split(//, $_[0]))
1495 {
1496 my $num = ord($_);
1497 if ($num == 13) { $str .= '\\/'; }
1498 elsif ($num < 32) { $str .= '^^' . chr($num + 64); }
1499 elsif ($num == 32) { $str .= '\\~'; } # space
1500 elsif ($num == 37) { $str .= '\\%'; } # percent
1501 elsif ($num == 92) { $str .= '\\\\'; } # backslash
1502 elsif ($num == 94) { $str .= '\\+'; } # caret
1503 elsif ($num == 123) { $str .= '\\{'; } # curly brace left
1504 elsif ($num == 125) { $str .= '\\}'; } # curly brace right
1505 else { $str .= $_; }
1506 $mod++;
1507 if ($mod == 26)
1508 {
1509 $mod = 0;
1510 $str .= "\n";
1511 }
1512 }
1513 chomp $str;
1514 return $str;
1515 }
1516
1517 sub pdfmarkstream
1518 {
1519 my $str;
1520 if ($::opt_level2)
1521 {
1522 my $s = $_[0];
1523 my $len = length($s);
1524 $str = "";
1525 my $i;
1526 for ($i=0; $i<$len-4; $i+=4)
1527 {
1528 $_ = ASCII85Encode(substr($s, $i, 4));
1529 s/!!!!!/z/;
1530 $str .= $_;
1531 }
1532 my $r = $len % 4;
1533 if ($r)
1534 {
1535 $_ = substr($s, $i, $r) . "\000\000\000";
1536 $_ = ASCII85Encode(substr($_, 0, 4));
1537 $str .= substr($_, 0, $r+1);
1538 }
1539 $str =~ s/(.{60})/$1\n/g;
1540 chomp($str);
1541 $str .= "~>";
1542 }
1543 else
1544 {
1545 $str = uc(unpack('H*', $_[0]));
1546 $str =~ s/(.{60})/$1\n/g;
1547 chomp($str);
1548 }
1549 return $str;
1550 }
1551
1552 sub ASCII85Encode
1553 {
1554 my $val = unpack("N", $_[0]);
1555 my @c;
1556 $c[4] = $val % 85 + 33;
1557 $val = int($val/85);
1558 $c[3] = $val % 85 + 33;
1559 $val = int($val/85);
1560 $c[2] = $val % 85 + 33;
1561 $val = int($val/85);
1562 $c[1] = $val % 85 + 33;
1563 $c[0] = int($val/85) + 33;
1564 return pack("C*", @c);
1565 }
1566
1567 sub setsignals {
1568 my $func = $_[0];
1569 $SIG{'HUP'} = $func unless $Win;
1570 $SIG{'INT'} = $func;
1571 $SIG{'QUIT'} = $func;
1572 $SIG{'TERM'} = $func;
1573 }
1574
1575 sub clean {
1576 if ($::opt_clean) {
1577 print "*** clear temp files ***\n" unless $::opt_quiet;
1578 foreach (@cleanlist) {
1579 unlink;
1580 }
1581 }
1582 }
1583
1584 sub cleanup {
1585 print "\n" unless $::opt_quiet;
1586 clean();
1587 exit 1;
1588 }
1589
1590 sub gscleanup {
1591 print "\n" unless $::opt_quiet;
1592 clean();
1593 close(::GSKID) if $gskidrunning;
1594 foreach (glob("$thumbprefix*.png")) {
1595 unlink;
1596 }
1597 exit 1;
1598 }
1599
1600 clean();
1601
1602 print "*** ready. ***\n" unless $::opt_quiet;
1603
1604 __END__