% File:        thumbpdf.tex
% Project:     thumbpdf
% Version:     2000/01/19 v1.11
% Author:      Heiko Oberdiek
%
% Copyright:   Copyright (C) 1999, 2000 Heiko Oberdiek.
%
%              This program may be distributed and/or modified under
%              the conditions of the LaTeX Project Public License,
%              either version 1.2 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.2 or later is part of all distributions
%              of LaTeX version 1999/12/01 or later.
%
%              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 'thumb???.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 'thumbdta.tex'
%              that contains the necessary pdf image objects.
%              These are then read by the package 'thumbpdf.sty'.
%
% Requirement: * pdf(e)tex
%              * thumb???.png
%
% Use:         pdftex thumbpdf
%              Because it takes some time to look for 1000 thumbnail
%              files, a maximum number can be specified:
%              pdftex \def\thumbmax{20}\input thumbpdf
%
% 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

\immediate\write-1{%
  File: thumbpdf.tex 2000/01/19 v1.11 %
  Program to include 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
%\pdfcompresslevel=9
%\shipout\hbox{}

% 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{thumb%
  \ifnum\thumbcount<100 0\fi
  \ifnum\thumbcount<10 0\fi
  \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}%
  }%
}

\def\processthumb{%
  \edef\listthumbs{\listthumbs\space\the\thumbcount}%
  \printthumbfile
  \pageno=\thumbcount
}

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

\ifnum\thumbmax>999
  \immediate\write16{%
    !!! Warning: \thumbmax\space too large, %
    999 is the highest supported thumbnail number!%
  }%
  \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

\def\END{%
  \ifx\allthumbs\empty
  \else
    \immediate\pdfobj{/ListThumbs\listthumbs}%
  \fi
  \vfill\eject
  \csname bye\endcsname % \bye is \outer
}

% thumbopt.tex
\iffileexists{thumbopt}{}{\END}

\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
  \fi
}

\immediate\write16{* Looking for extra thumbnails:}
\thumbcount=0
\input thumbopt

\END
