Debian Codegraph @

texmf-dist/tex/generic/thumbpdf/thumbpdf.sty

raw
1 % File: thumbpdf.sty
2 % Project: thumbpdf
3 % Version: 2018/09/07 v3.17
4 % Author: Heiko Oberdiek
5 %
6 % Function: Inclusion of thumbnails
7 %
8 % Copyright: Copyright (C) 1999-2018 Heiko Oberdiek.
9 %
10 % This work may be distributed and/or modified under
11 % the conditions of the LaTeX Project Public License,
12 % either version 1.3 of this license or (at your option)
13 % any later version. The latest version of this license
14 % is in
15 % http://www.latex-project.org/lppl.txt
16 % and version 1.3 or later is part of all distributions
17 % of LaTeX version 2003/12/01 or later.
18 %
19 % This work has the LPPL maintenance status "maintained".
20 %
21 % This Current Maintainer of this work is Heiko Oberdiek.
22 %
23 % See file `readme.txt' for a list of files that
24 % belong to this project.
25 %
26 % Requirement: * pdftex mode:
27 % * pdfTeX (plain or latex formats)
28 % * thumbnail data file: `<jobname>.tpt'
29 % * pdfmark mode:
30 % * Ghostscript 6.0, versions below do not work.
31 % Eventually Adobe Distiller can work, but I
32 % cannot test this.
33 % * thumbnail data file: `<jobname>.tpm'
34 % * Both thumbnail data files are generated by the
35 % perl script `thumbpdf(.pl)'.
36 %
37 % Use: * LaTeX2e: \usepackage{thumbpdf}
38 % Options: pdftex,
39 % pdfmark, dvips, ps2pdf
40 % vtex, vtexpdfmark
41 % * plain: \input thumbpdf.sty
42 % A driver can be defined previously:
43 % \def\ThumbpdfDriver{pdfmark}
44 % * It is not necessary to set driver `pdftex', `vtex',
45 % or `vtexpdfmark', because pdfTeX and VTeX are
46 % detected at run time. So package `thumbpdf'
47 % chooses the correct driver and ignores other
48 % specified drivers.
49 %
50 % History: 1999/02/14 v1.0: first public release
51 % 1999/02/23 v1.1: bug in plain-\@thumbwarning removed.
52 % 1999/03/01 v1.2: \DefLastObj no longer needed.
53 % 1999/03/12 v1.3: Copyright: LPPL
54 % 1999/05/05 v1.4: added \the before \inputlineno.
55 % sharing RGB objects.
56 % 1999/06/13 v1.5
57 % 1999/07/27 v1.6: Bug of handling with \pdfpageattr
58 % fixed. The same bug is in hyperref
59 % versions below 6.64 (option
60 % pagetransition).
61 % Warning added, if there are missing
62 % thumbnails.
63 % 1999/08/08 v1.7: Bug in \@ThumbPageAttr fixed
64 % (introduced in version 1.6).
65 % 1999/09/09 v1.8: \PackageWarning does not work at
66 % the beginning of \shipout, because
67 % \protect has been set to \noexpand
68 % (mysterious LaTeX behaviour?).
69 % 1999/09/16 v1.9
70 % 2000/01/11 v1.10: Revised.
71 % 2000/01/19 v1.11
72 % 2000/02/11 v1.12: \jobname.tnd supported,
73 % \DeclareThumbs added.
74 % 2000/02/22 v2.0: pdfmark support, extra thumbnails are
75 % scanned in file `<jobname>.tno'.
76 % 2000/02/28 v2.1: support of `thumbpdf.cfg'.
77 % 2000/03/07 v2.2
78 % 2000/03/22 v2.3
79 % 2000/04/10 v2.4
80 % 2000/07/29 v2.5
81 % 2000/09/27 v2.6
82 % 2000/10/27 v2.7
83 % 2001/01/12 v2.8
84 % 2001/03/29 v2.9
85 % 2001/04/02 v2.10
86 % 2001/04/26 v2.11
87 % 2002/01/11 v3.0: Support for VTeX's PostScript mode added.
88 % 2002/05/26 v3.1
89 % 2002/05/26 v3.2
90 % 2003/03/19 v3.3
91 % 2003/06/06 v3.4
92 % 2004/10/24 v3.5: LPPL 1.3.
93 % 2004/11/19 v3.6
94 % 2004/11/19 v3.7
95 % 2005/07/06 v3.8
96 % 2007/11/07 v3.9
97 % 2008/04/16 v3.10
98 % 2010/07/07 v3.11: Warning if \pdfobjcompresslevel>0.
99 % 2011/08/09 v3.12: Support for LuaTeX added.
100 % 2011/08/10 v3.13: Version date fixed.
101 % 2012/04/09 v3.14: Fix for plain TeX compatibility
102 % (offending \RequirePackage).
103 % 2012/04/18 v3.15
104 % 2014/07/15 v3.16
105 % 2018/09/07 v3.17 Abort not just warn on terminal if pdfcompression is enabled.
106 % (David Carlisle as suggested by Karl Berry)
107 % https://github.com/ho-tex/thumbpdf
108 %
109 % \THB@ is the prefix for internal command names.
110 %
111 % Prevent that the package is loaded twice (for plain formats).
112 \expandafter\ifx\csname THB@name\endcsname\relax
113 \expandafter\gdef\csname THB@name\endcsname{thumbpdf}
114 \else
115 \expandafter\endinput
116 \fi
117
118 % Package identification in the log file
119 \begingroup
120 \def\x[#1]{\endgroup\immediate\write-1{Package: thumbpdf #1}}%
121 \ifx\ProvidesPackage\UnDeFiNeD
122 \else
123 \ifx\ProvidesPackage\relax
124 \else
125 \def\x{\endgroup\ProvidesPackage{thumbpdf}}%
126 \fi
127 \fi
128 \x[2018/09/07 v3.17 Inclusion of thumbnails (HO)]%
129
130 % Dummy for \thisthumb and \DeclareThumbs. It is overwritten,
131 % if the checks succeed and the whole package is loaded.
132 \def\thisthumb#1{}
133 \def\DeclareThumbs#1{}
134
135 % Check part, keep help macros local:
136 \begingroup
137
138 % For use of `@' in command names.
139 \catcode`\@=11
140
141 % Now we define utils of the LaTeX kernel, if they aren't.
142 \expandafter\ifx\csname @firstoftwo\endcsname\relax
143 \long\def\@firstoftwo#1#2{#1}%
144 \long\def\@secondoftwo#1#2{#2}%
145 \fi
146 \expandafter\ifx\csname @ifundefined\endcsname\relax
147 \def\@ifundefined#1{%
148 \expandafter\ifx\csname#1\endcsname\relax
149 \expandafter\@firstoftwo
150 \else
151 \expandafter\@secondoftwo
152 \fi
153 }%
154 \fi
155
156 % If a check fails, a warning is produced and the package exits.
157 \@ifundefined{PackageWarningNoLine}{%
158 \def\PackageWarningNoLine#1#2{%
159 \immediate\write16{}%
160 \immediate\write16{Package #1 Warning: #2.}%
161 \immediate\write16{}%
162 }%
163 }{}
164
165 \long\def\@ReturnAfterFi#1\fi{\fi#1}
166 \long\def\@ReturnAfterElseFi#1\else#2\fi{\fi#1}
167
168 \def\THB@WarnEnd#1{%
169 \PackageWarningNoLine{thumbpdf}{#1}%
170 \endgroup
171 \endinput
172 }
173
174 % \csname-Trick because of \outer definition of \newread.
175 % \globaldefs=-1 in order to make the allocation of \newread local.
176 \@ifundefined{IfFileExists}{%
177 \edef\x{\the\globaldefs}%
178 \globaldefs-1\relax
179 \csname newread\endcsname\THB@TempRead
180 \globaldefs\x\relax
181 \def\IfFileExists#1{%
182 \openin\THB@TempRead=#1\relax
183 \ifeof\THB@TempRead
184 \closein\THB@TempRead
185 \expandafter\@secondoftwo
186 \else
187 \closein\THB@TempRead
188 \expandafter\@firstoftwo
189 \fi
190 }%
191 }{}
192
193 % Load .cfg file if it exists
194 \IfFileExists{thumbpdf.cfg}{%
195 \@ifundefined{@@input}{%
196 \input thumbpdf.cfg\relax
197 }{%
198 \input{thumbpdf.cfg}%
199 }%
200 }{}
201
202 % LaTeX2e options
203 \def\THB@pdftex{pdftex}
204 \def\THB@pdfmark{pdfmark}
205 \def\THB@vtex{vtex}
206 \@ifundefined{ThumbpdfDriver}{\let\ThumbpdfDriver\empty}{}
207 \@ifundefined{DeclareOption}{}{%
208 \DeclareOption{pdftex}{\let\ThumbpdfDriver\THB@pdftex}%
209 \DeclareOption{pdfmark}{\let\ThumbpdfDriver\THB@pdfmark}%
210 \DeclareOption{dvips}{\let\ThumbpdfDriver\THB@pdfmark}%
211 \DeclareOption{ps2pdf}{\let\ThumbpdfDriver\THB@pdfmark}%
212 \DeclareOption{vtex}{\let\ThumbpdfDriver\THB@vtex}%
213 \DeclareOption{vtexpdfmark}{\let\ThumbpdfDriver\THB@vtex}%
214 \ProcessOptions\relax
215 }
216 \edef\x{\ThumbpdfDriver}
217 \expandafter\lowercase\expandafter{%
218 \expandafter\gdef\expandafter\ThumbpdfDriver\expandafter{\x}%
219 }
220 \def\x{dvips}\ifx\ThumbpdfDriver\x
221 \global\let\ThumbpdfDriver\THB@pdfmark
222 \fi
223 \def\x{ps2pdf}\ifx\ThumbpdfDriver\x
224 \global\let\ThumbpdfDriver\THB@pdfmark
225 \fi
226 \def\x{vtexpdfmark}\ifx\ThumbpdfDriver\x
227 \global\let\ThumbpdfDriver\THB@vtex
228 \fi
229
230 % Check: Called by pdfTeX or VTeX? Driver correct?
231 \ifcase\@ifundefined{pdfoutput}0\pdfoutput\relax
232 \@ReturnAfterElseFi{%
233 \ifcase0\@ifundefined{OpMode}1{\ifnum\OpMode=2 \else 1\fi} %
234 \global\let\ThumbpdfDriver\THB@vtex
235 \else
236 \@ReturnAfterFi{%
237 \ifx\ThumbpdfDriver\THB@pdfmark
238 \else
239 \@ReturnAfterFi{%
240 \ifx\ThumbpdfDriver\empty
241 \@ReturnAfterElseFi{%
242 \THB@WarnEnd{%
243 Missing driver name%
244 }%
245 }%
246 \else
247 \@ReturnAfterFi{%
248 \ifx\ThumbpdfDriver\THB@pdftex
249 \@ReturnAfterElseFi{%
250 \THB@WarnEnd{%
251 You need pdfTeX in PDF mode for driver `pdftex'%
252 }%
253 }%
254 \else
255 \@ReturnAfterFi{%
256 \ifx\ThumbpdfDriver\THB@vtex
257 \@ReturnAfterElseFi{%
258 \THB@WarnEnd{%
259 You need VTeX in PS mode for driver `vtex'%
260 }%
261 }%
262 \else
263 \@ReturnAfterFi{%
264 \THB@WarnEnd{%
265 Driver `\ThumbpdfDriver' not supported%
266 }%
267 }%
268 \fi
269 }%
270 \fi
271 }%
272 \fi
273 }%
274 \fi
275 }%
276 \fi
277 }%
278 \else
279 \global\let\ThumbpdfDriver\THB@pdftex
280 \fi
281
282 % Check \pdfobjcompresslevel
283 \expandafter\ifx\csname pdfobjcompresslevel\endcsname\relax
284 \else
285 \ifnum\pdfobjcompresslevel>0 %
286 \wlog{THUMBPDF: Compressed PDF objects of PDF 1.5 are not supported}%
287 \endgroup\expandafter\expandafter\expandafter\endinput
288 \fi
289 \fi
290
291 % Check: Does data file exists?
292 \edef\THB@datafile{%
293 \jobname.tp\ifx\ThumbpdfDriver\THB@pdftex t\else m\fi
294 }%
295 \IfFileExists{\THB@datafile}{}{%
296 \THB@WarnEnd{Thumbnail data file `\THB@datafile' not found}%
297 }%
298
299 % Some macros need \PackageWarning
300 \expandafter\ifx\csname PackageWarning\endcsname\relax
301 \gdef\PackageWarning#1#2{%
302 \immediate\write16{}%
303 \immediate\write16{%
304 Package `#1' warning: #2 on input line \the\inputlineno.%
305 }%
306 \immediate\write16{}%
307 }%
308 \fi
309
310 % definition of \DeclareThumbs
311 \gdef\DeclareThumbs#1{%
312 \begingroup
313 % check LaTeX's \if@filesw
314 \expandafter\ifx\csname if@filesw\expandafter\endcsname
315 \csname iffalse\endcsname
316 \else
317 \csname newwrite\endcsname\THB@thumbopt
318 \immediate\openout\THB@thumbopt=\jobname.tno
319 % Definition of \thumb that also works with plain format.
320 \def\thumb##1##{\THB@thumb{##1}}%
321 \def\THB@thumb##1##2{%
322 \immediate\write\THB@thumbopt{%
323 \string\thumb##1{##2}%
324 }%
325 }%
326 \let\protect\string
327 % process data
328 #1%
329 % end of file
330 \immediate\write\THB@thumbopt{\string\endinput}%
331 \fi
332 \endgroup
333 \global\let\DeclareThumbs\THB@DeclareThumbs
334 }
335 \def\THB@DeclareThumbs#1{%
336 \PackageWarning{\THB@name}{Too many \string\DeclareThumbs}%
337 }
338
339 % pdfmark part
340 \ifx\ThumbpdfDriver\THB@pdfmark
341 % the header file includes already the regular thumbnails.
342 \special{header=\THB@datafile}%
343 \begingroup
344 \def\DefThisThumb#1{%
345 \global\expandafter\let\csname thumb#1\endcsname\empty
346 }%
347 % ignore PostScript comment char
348 \catcode`\%=9
349 \@ifundefined{@@input}{\input\THB@datafile\relax}
350 {\input{\THB@datafile}}
351 \endgroup
352 % definition of \thisthumb
353 \gdef\thisthumb#1{%
354 \expandafter\ifx\csname thumb#1\endcsname\relax
355 \PackageWarning{\THB@name}{Thumbnail `#1' undefined}%
356 \else
357 \special{ps:{THB\string_#1} thisTHB}%
358 \fi
359 }%
360 % end of pdfmark part
361 \endgroup\expandafter\endinput
362 \fi
363
364 % vtex part
365 \ifx\ThumbpdfDriver\THB@vtex
366 % the header file includes already the regular thumbnails.
367 \ifnum\@ifundefined{VTeXversion}0\VTeXversion<753 %
368 \ifcase\@ifundefined{gexmode}1\gexmode
369 % without GeX
370 \immediate\special{header=\THB@datafile}%
371 \else
372 % with GeX
373 \begingroup
374 \edef\x{\the\globaldefs}%
375 \globaldefs-1\relax
376 \csname newread\endcsname\THB@TempRead
377 \globaldefs\x\relax
378 \openin\THB@TempRead=\THB@datafile\relax
379 \def\x{%
380 \catcode`\%=12 %
381 \catcode`\|=0 %
382 \catcode`\{=12 %
383 \catcode`\}=12 %
384 \catcode`\ =12 %
385 \catcode`\\=12 %
386 \catcode`\_=12 %
387 \let\par\empty
388 \loop
389 \ifcase\ifeof\THB@TempRead 1 \else 0 \fi
390 \read\THB@TempRead to \THB@line
391 \immediate\special{!=\THB@line}%
392 \repeat
393 \closein\THB@TempRead
394 \endgroup
395 }%
396 \x
397 \fi
398 \else
399 \immediate\special{headercopy=\THB@datafile}%
400 \fi
401 \begingroup
402 \def\DefThisThumb#1{%
403 \global\expandafter\let\csname thumb#1\endcsname\empty
404 }%
405 % ignore PostScript comment char
406 \catcode`\%=9
407 \@ifundefined{@@input}{\input\THB@datafile\relax}
408 {\input{\THB@datafile}}
409 \endgroup
410 % definition of \thisthumb
411 \gdef\thisthumb#1{%
412 \expandafter\ifx\csname thumb#1\endcsname\relax
413 \PackageWarning{\THB@name}{Thumbnail `#1' undefined}%
414 \else
415 \special{!={THB\string_#1} thisTHB}%
416 \fi
417 }%
418 % end of vtex part
419 \endgroup\expandafter\endinput
420 \fi
421
422 % pdftex part
423 \global\let\THB@datafile\THB@datafile
424 \endgroup
425
426 \expandafter\chardef\csname THB@AtCode\endcsname=\the\catcode`\@
427 \catcode`\@=11
428
429 % The thumbnails are numbered with the output pages:
430 % `thb1.png' means the thumbnail of the first output page,
431 % `thb28.png' the thumbnail of the 28th written page,
432 % regardless of the page number.
433 % Therefore we need a new counter that is incremented with each
434 % page that is written to the pdf file.
435 \newcount\c@thumb
436 \c@thumb 0\relax
437
438 \newif\ifTHB@thisthumb
439
440 \def\thisthumb#1{%
441 \global\THB@thisthumbtrue
442 \THB@AddPageAttr{#1}%
443 }
444
445 % Now we enhance \shipout in order to
446 % * increment the output page counter (\c@thumb),
447 % * check if a thumbnail exists,
448 % * add/remove the thumbnail entry to the pdf page attributes.
449 \let\THBorg@shipout\shipout
450 \def\shipout{\THB@AddThumb\THBorg@shipout}
451 \def\THB@AddThumb{%
452 \begingroup
453 \global\advance\c@thumb by 1\relax
454 \ifTHB@thisthumb
455 \global\THB@thisthumbfalse % reset switch
456 \else
457 \THB@AddPageAttr{\the\c@thumb}%
458 \fi
459 \endgroup
460 }
461 \def\THB@AddPageAttr#1{%
462 % Remove previous /Thumb entry.
463 \expandafter\THB@RemoveThumbAttr\the\pdfpageattr^^J/Thumb{} 0 R\END
464 % Check, if thumb exists.
465 \expandafter\ifx\csname thumb#1\endcsname\relax
466 % \PackageWarning cannot be used, because \protect=\noexpand.
467 \immediate\write16{}%
468 \immediate\write16{Package \THB@name\space Warning: %
469 Thumbnail `#1' undefined %
470 on input line \the\inputlineno.%
471 }%
472 \immediate\write16{}%
473 \else
474 \begingroup
475 \edef\x{\endgroup
476 \global\pdfpageattr{%
477 \the\pdfpageattr
478 ^^J/Thumb \csname thumb#1\endcsname\space 0 R%
479 }%
480 }%
481 \x
482 \fi
483 }
484 \def\THB@RemoveThumbAttr#1^^J/Thumb#2#3 0 R#4\END{%
485 \ifx\\#2\\%
486 \global\pdfpageattr{#1}%
487 \else
488 \THB@RemoveThumbAttr#1#4\END
489 \fi
490 }
491
492 \begingroup\expandafter\expandafter\expandafter\endgroup
493 \expandafter\ifx\csname RequirePackage\endcsname\relax
494 \input ifluatex.sty\relax
495 \else
496 \RequirePackage{ifluatex}[2010/03/01]%
497 \fi
498 \ifluatex
499 \def\THB@InitLua{%
500 \catcode39=12 % '
501 \catcode40=12 % (
502 \catcode41=12 % )
503 \catcode43=12 % +
504 \catcode45=12 % -
505 \catcode46=12 % .
506 \catcode60=12 % <
507 \catcode95=12 % _
508 \escapechar=-1 %
509 \edef\\{\string\\}%
510 \edef\%{\string\%}%
511 \escapechar=92 %
512 \newlinechar=13 %
513 \catcode13=12 %
514 \endlinechar=13 %
515 }%
516 \ifnum\luatexversion<36 %
517 \def\THB@directlua#{\directlua0}%
518 \else
519 \let\THB@directlua\directlua
520 \fi
521 \begingroup
522 \THB@InitLua %
523 \THB@directlua{%
524 thumbpdf = {}
525 thumbpdf.callback = callback.find('process_input_buffer')
526 % if thumbpdf.callback then
527 % texio.write_nl('log', 'Package thumbpdf Info: Callback saved.')
528 % texio.write_nl('log', '')
529 % end
530 function thumbpdf.process_input_buffer(buffer)
531 return string.gsub(buffer, '[\\100-\\255]',
532 function (s)
533 local num = string.byte(s)
534 if num < 128 then
535 return s
536 else
537 return unicode.utf8.char(num + 1114112)
538 end
539 end
540 )
541 end
542 thumbpdf.id, thumbpdf.error =
543 callback.register('process_input_buffer',
544 thumbpdf.process_input_buffer)
545 if thumbpdf.id == nil then
546 tex.print(string.format(
547 '\\\\endgroup\\\\begingroup%
548 \\\\PackageError{thumbpdf}%
549 {\%s:\\\\MessageBreak \%s}\\\\@ehc',
550 'Setting process_input_buffer failed',
551 string.gsub(thumbpdf.error, '\\.$', '')))
552 % else
553 % texio.write_nl('log',
554 % 'Package thumbpdf Info: Setting process_input_buffer.')
555 % texio.write_nl('log', '')
556 end
557 }%
558 \endgroup%
559 \fi
560
561 % Many commands and catcode changes are only necessary to read
562 % the data file, so we do this in a group.
563 \begingroup
564
565 % These commands are used in `<jobname>.tpt'
566 \def\DefThumb#1{%
567 \expandafter\xdef\csname thumb#1\endcsname{\the\pdflastobj}%
568 }%
569
570 \def\DefRGB#1{%
571 \expandafter\edef\csname thumbRGB#1\endcsname{\the\pdflastobj}%
572 }%
573 \def\UseRGB#1{\csname thumbRGB#1\endcsname}%
574
575 % Now the catcodes to read `<jobname>.tpt' follows:
576 \def\SetCatcodeRange#1#2{%
577 \c@thumb=#1
578 \loop
579 \catcode\c@thumb=12
580 \ifnum\c@thumb<#2
581 \advance\c@thumb by 1
582 \repeat
583 }
584 \SetCatcodeRange{0}{12}%
585 % ^^M
586 \SetCatcodeRange{14}{31}%
587 % space
588 \SetCatcodeRange{33}{36}% ! " # $
589 % percent
590 \SetCatcodeRange{38}{63}% & ' ( ) * + , - . / 0-9 : ; < = > ?
591 % @ A-Z
592 \catcode`\[=12
593 % backslash
594 \catcode`\]=12
595 % ^
596 \SetCatcodeRange{95}{96}% _ '
597 % a-z {
598 \catcode`\|=12
599 % }
600 \SetCatcodeRange{126}{255}% ~ 127 8-bit
601
602 % commands for special characters used in `<jobname>.tpt'.
603 \edef\~{\space}% in stream data to avoid `\ ' at line end.
604 \let\ \~%
605 \edef\+{\string^}%
606 \begingroup
607 \lccode`1=`\\
608 \lccode`2=`\^^M
609 \lccode`3=`\{
610 \lccode`4=`\}
611 \lccode`5=`\%
612 \lowercase{\endgroup
613 \def\\{1}%
614 \def\/{2}%
615 \def\{{3}%
616 \def\}{4}%
617 \def\%{5}%
618 }
619
620 % no white space by line ends
621 \endlinechar=-1
622
623 % process data file and include the thumbnails
624 % in the current pdf file
625 \expandafter\ifx\csname @@input\endcsname\relax
626 \input\THB@datafile\relax
627 \else
628 \input{\THB@datafile}%
629 \fi
630 \endgroup
631
632 \ifluatex
633 \begingroup
634 \THB@InitLua %
635 \THB@directlua{%
636 thumbpdf.id, thumbpdf.error =
637 callback.register('process_input_buffer', thumbpdf.callback)
638 if thumbpdf.id == nil then
639 tex.print(string.format(
640 '\\\\endgroup\\\\begingroup%
641 \\\\PackageError{thumbpdf}%
642 {\%s:\\\\MessageBreak \%s}\\\\@ehc',
643 'Restoring process_input_buffer failed',
644 string.gsub(thumbpdf.error, '\\.$', '')))
645 % else
646 % texio.write_nl('log', 'Package thumbpdf Info: Callback restored.')
647 % texio.write_nl('log', '')
648 end
649 }%
650 \endgroup
651 \fi
652
653 % restore catcode of `@'.
654 \catcode`\@=\THB@AtCode\relax
655 \endinput