summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/thumbpdf/thumbpdf.tex
blob: 148edbed8e2e283d541d605a545fcfcda8c2f718 (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
% File:        thumbpdf.tex
% Project:     thumbpdf
% Version:     2010/07/07 v3.11
% Author:      Heiko Oberdiek
%
% Copyright:   Copyright (C) 1999-2010 Heiko Oberdiek.
%
%              This work may be distributed and/or modified under
%              the conditions of the LaTeX Project Public License,
%              either version 1.3 of this license or (at your option)
%              any later version. The latest version of this license
%              is in
%                http://www.latex-project.org/lppl.txt
%              and version 1.3 or later is part of all distributions
%              of LaTeX version 2003/12/01 or later.
%
%              This work has the LPPL maintenance status "maintained".
%
%              This Current Maintainer of this work is Heiko Oberdiek.
%
%              See file "readme.txt" for a list of files that
%              belong to this project.
%
% Function:    Program to include thumbnails as images.
%              It looks for thumbnail files `thb*.png' and
%              includes them as images in the output pdf file
%              `thumbpdf.pdf'. The perl script `thumbpdf(.pl)'
%              automatically calls pdftex with this file to
%              generate `thumbpdf.pdf' and produces the data
%              files, that contains the necessary pdf image objects
%              for package `thumbpdf.sty'.
%
% Requirement: * pdftex
%              * thumbnails `thb*.png'
%
% Use:         * Syntax: pdftex thumbpdf
%              * \thumbmax:
%                Because it takes some time to look for 1000 thumbnail
%                files, a maximum number can be specified:
%                  pdftex \def\thumbmax{20}\input thumbpdf
%              * \thumbjob:
%                This macro contains the name of the user's TeX file
%                without extension (`<jobname>'). It is needed to build
%                the name of the optional file `<jobname>.tno', that
%                contains extra thumbnail declarations:
%                  pdftex \def\thumbjob{example}\input thumbpdf
%                Then `example.tno' are read to get additional
%                thumbnails.
%
% History:     1999/02/14 v1.0:  first public release
%              1999/02/23 v1.1
%              1999/03/01 v1.2
%              1999/03/12 v1.3:  Copyright: LPPL
%              1999/05/05 v1.4
%              1999/06/13 v1.5
%              1999/07/27 v1.6
%              1999/08/08 v1.7:  pdfTeX 0.14a
%              1999/09/09 v1.8
%              1999/09/16 v1.9
%              2000/01/11 v1.10: \immediate before \pdfximage removed,
%                because it is obsolete and because of a problem with
%                a pre-release version of pdfTeX 0.14d.
%              2000/01/19 v1.11
%              2000/02/11 v1.12: \thumbjob added.
%              2000/02/22 v2.0:  Looking for `\thumbjob.tno' only, the
%                file name `thumbopt.tex' is not supported anymore.
%              2000/02/28 v2.1
%              2000/03/07 v2.2
%              2000/03/22 v2.3
%              2000/04/10 v2.4: Version is also printed on screen.
%              2000/07/29 v2.5
%              2000/09/27 v2.6
%              2000/10/27 v2.7
%              2001/01/12 v2.8
%              2001/03/29 v2.9
%              2001/04/02 v2.10
%              2001/04/26 v2.11
%              2002/01/11 v3.0
%              2002/05/26 v3.1
%              2002/05/26 v3.2
%              2003/03/19 v3.3
%              2003/06/06 v3.4
%              2004/10/24 v3.5: LPPL 1.3.
%              2004/11/19 v3.6
%              2004/11/19 v3.7: For easier debugging, the special
%                thumbpdf objects are now valid PDF objects that
%                are referenced in the Catalog.
%              2005/07/06 v3.8
%              2007/11/07 v3.9
%              2008/04/16 v3.10
%              2010/07/07 v3.11: \pdfcompresslevel=0 and
%                \pdfminorversion=4 added.

\immediate\write16{%
  File: thumbpdf.tex 2010/07/07 v3.11 %
  Including thumbnails as images (HO)%
}
\immediate\write16{%
****************************************************************}
\immediate\write16{%
* Generating `thumbpdf.pdf', each page with a found thumbnail. *}
\immediate\write16{%
****************************************************************}

\def\END{\csname @@end\endcsname\end}

% Check, if LaTeX format
\ifx\mbox\undefined
\else
  \immediate\write16{%
    !!! Process this file `\jobname.tex' with pdf(e)tex %
    (plain format) !!!%
  }%
  \expandafter\END
\fi

% Check if pdfTeX
\ifx\pdfoutput\undefined
  \immediate\write16{%
    !!! Process this file `\jobname.tex' with pdf(e)tex %
    (NOT tex) !!!%
  }%
  \expandafter\END
\fi

\pdfoutput=1

% Disable \pdfobjcompresslevel
\ifx\pdfobjcompresslevel\undefined
\else
  \pdfobjcompresslevel=0 %
\fi
\ifx\pdfminorversion\undefined
\else
  \ifnum\pdfminorversion>4 %
    \pdfminorversion=4 %
  \fi
\fi

% this file is tested with pdfTeX 0.13a
% but for downward compatibilty a trick from Hans Hagen:
\ifnum\pdftexversion<13 \ifnum\expandafter `\pdftexrevision<`s
  \let\normalpdfimage\pdfimage
  \def\grabpdfimage#1#2{\normalpdfimage#1 #2\relax}
  \def\pdfimage#1#{\grabpdfimage{#1}}
\fi\fi

% syntax has changed with pdfTeX 0.14a:
\ifnum\pdftexversion>13
  \def\pdfimage#1#{\grabpdfimage{#1}}
  \def\grabpdfimage#1#2{%
    \pdfximage#1{#2}\pdfrefximage\pdflastximage
  }
\fi

% max. 106x106 / 72 dpi
\hsize 1.5in
\vsize 1.5in

\nopagenumbers

\newcount\thumbcount
\def\thumbcountstep{\advance\thumbcount by 1 }

\def\thumbfilename{thb\the\thumbcount.png}

\newread\thumbread
\def\ifthumbexists{%
  \openin\thumbread=\thumbfilename\relax
  \ifeof\thumbread
    \expandafter\noneofone
  \else
    \closein\thumbread
    \expandafter\firstofone
  \fi
}
\def\noneofone#1{}
\def\firstofone#1{#1}

\def\iffileexists#1{%
  \openin\thumbread=#1\relax
  \ifeof\thumbread
    \expandafter\secondoftwo
  \else
    \closein\thumbread
    \expandafter\firstoftwo
  \fi
}
\long\def\firstoftwo#1#2{#1}
\long\def\secondoftwo#1#2{#2}

\let\listthumbs\empty

\def\printthumbfile{%
  \vfill\eject
  \centerline{%
    \pdfimage height \vsize {\thumbfilename}%
  }%
}

\newcount\thumbfound
\thumbfound=0
\def\processthumb{%
  \edef\listthumbs{\listthumbs\space\the\thumbcount}%
  \printthumbfile
  \pageno=\thumbcount
  \advance\thumbfound by 1
}

\expandafter\ifx\csname thumbmax\endcsname\relax
  \def\thumbmax{999}%
\fi

\immediate\write16{* Looking for thumbnails 0..\thumbmax:}

\thumbcount=\thumbmax\relax
\advance\thumbcount by 1
\edef\thumbmax{\the\thumbcount}%
\thumbcount=0
\loop
\ifnum\thumbcount<\thumbmax\relax
  \ifthumbexists\processthumb
  \thumbcountstep
\repeat

\break
\immediate\write16{==> \the\thumbfound\space thumbnail(s) found.}
\immediate\pdfobj{<</MaxThumbNumber \the\thumbfound>>}
\edef\CatalogHook{\the\pdflastobj\space 0 R}%
\immediate\write16{* Looking for extra thumbnails:}

\def\END{%
  \ifx\allthumbs\empty
  \else
    \immediate\pdfobj{<</ListThumbs\listthumbs>>}%
    \edef\CatalogHook{\CatalogHook\space\the\pdflastobj\space 0 R}%
  \fi
  \pdfcatalog{/ThumbpdfHook [\CatalogHook]}%
  \vfill\eject
  \csname bye\endcsname % \bye is \outer
}

% <jobname>.tno
\ifx\thumbjob\UnDeFiNeD
  \immediate\write16{!!! Warning: \string\thumbjob\space undefined,
    search for extra thumbnails skipped!%
  }
  \expandafter\END
\fi
\def\optfile{\thumbjob.tno}%
\iffileexists{\optfile}{}{%
  \immediate\write16{==> No optional thumbnail file.}%
  \END
}%
\thumbfound=0

\def\thumb{\futurelet\nexttoken\thumbA}
\let\braceleft=[
\def\thumbA{%
  \if\nexttoken\braceleft
    \expandafter\thumbB
  \else
    \expandafter\thumbC
  \fi
}
\def\thumbC#1{\thumbB[#1]{#1}}
\def\thumbB[#1]#2{% #1: label, #2: file[.png]
  \iffileexists{#2.png}{%
    \def\thumbfilename{#2.png}%
  }{%
    \iffileexists{#2}{%
      \def\thumbfilename{#2}%
    }{%
      \let\thumbfilename\empty
    }%
  }%
  \ifx\thumbfilename\empty
    \immediate\write16{%
      !!! Warning: Thumbnail `#2' does not exist!"}%
  \else
    \edef\listthumbs{\listthumbs\space{#1}}%
    \printthumbfile
    \advance\thumbcount by -1
    \pageno=\thumbcount
    \advance\thumbfound by 1
  \fi
}

\thumbcount=0
\input \optfile
\break
\immediate\write16{==> \the\thumbfound\space extra thumbnail(s) found.}%

\END