% File:        thumbpdf.tex
% Project:     thumbpdf
% Version:     2018/09/07 v3.17
% Author:      Heiko Oberdiek
%
% Copyright:   Copyright (C) 1999-2018 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.
%              2011/08/09 v3.12
%              2011/08/10 v3.13
%              2012/04/09 v3.14
%              2012/04/18 v3.15
%              2014/07/15 v3.16
%              2018/09/07 v3.17 https://github.com/ho-tex/thumbpdf

\immediate\write16{%
  File: thumbpdf.tex 2018/09/07 v3.17 %
  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
