% File:        thumbpdf.sty
% Project:     thumbpdf
% Version:     2000/01/19 v1.11
% Author:      Heiko Oberdiek
%
% Function:    Inclusion of thumbnails
%
% 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.
%
% Requirement: * pdf(e)tex or pdf(e)latex
%              * thumbdta.tex: it contains the thumbnails and
%                can be generated by the perl script 'thumbpdf(.pl)'.
%
% Use:         * \input thumbpdf.sty (pdf(e)tex)
%              * \usepackage{thumbpdf} (pdf(e)latex)
%
% History:     1999/02/14 v1.0: first public release
%              1999/02/23 v1.1: bug in plain-\@thumbwarning removed.
%              1999/03/01 v1.2: \DefLastObj no longer needed.
%              1999/03/12 v1.3: Copyright: LPPL
%              1999/05/05 v1.4: added \the before \inputlineno.
%                               sharing RGB objects.
%              1999/06/13 v1.5
%              1999/07/27 v1.6: Bug of handling with \pdfpageattr fixed.
%                               The same bug is in hyperref versions
%                               below 6.64 (option pagetransition).
%                               Warning added, if there are missing
%                               thumbnails.
%              1999/08/08 v1.7: Bug in \@ThumbPageAttr fixed
%                               (introduced in version 1.6).
%              1999/09/09 v1.8: \PackageWarning does not work at
%                               the beginning of \shipout, because
%                               \protect has been set to \noexpand
%                               (mysterious LaTeX behaviour?).
%              1999/09/16 v1.9
%              2000/01/11 v1.10: Revised.
%              2000/01/19 v1.11
%
% \THB@ is the prefix for internal command names.
%
% Prevent that the package is loaded twice (for plain formats).
\expandafter\ifx\csname THB@name\endcsname\relax
  \expandafter\def\csname THB@name\endcsname{thumbpdf}
\else
  \expandafter\endinput
\fi

% Package identification in the log file
\begingroup
  \def\x[#1]{\endgroup\immediate\write-1{Package: thumbpdf #1}}%
  \ifx\ProvidesPackage\UnDeFiNeD
  \else
    \ifx\ProvidesPackage\relax
    \else
      \def\x{\endgroup\ProvidesPackage{thumbpdf}}%
    \fi
  \fi
\x[2000/01/19 v1.11 Inclusion of thumbnails (HO)]%

% Dummy for \thisthumb. It is overwritten, if the checks succeed
% and the whole package is loaded.
\def\thisthumb#1{}

% Check part, keep help macros local:
\begingroup

  % For use of '@' in command names.
  \catcode`\@=11

  % Now we define utils of the LaTeX kernel, if they aren't.
  \expandafter\ifx\csname @firstoftwo\endcsname\relax
    \long\def\@firstoftwo#1#2{#1}%
    \long\def\@secondoftwo#1#2{#2}%
  \fi
  \expandafter\ifx\csname @ifundefined\endcsname\relax
    \def\@ifundefined#1{%
      \expandafter\ifx\csname#1\endcsname\relax
        \expandafter\@firstoftwo
      \else
        \expandafter\@secondoftwo
      \fi
    }%
  \fi

  % If a check fails, a warning is produced and the package exits.
  \@ifundefined{PackageWarningNoLine}{%
    \def\PackageWarningNoLine#1#2{%
      \immediate\write16{}%
      \immediate\write16{Package #1 Warning: #2.}%
      \immediate\write16{}%
    }%
  }{}
  \def\THB@WarnEnd#1{%
    \PackageWarningNoLine{thumbpdf}{#1}%
    \endgroup
    \endinput
  }

  % Check: Called by pdfTeX?
  \@ifundefined{pdfoutput}{%
    \THB@WarnEnd{You need 'pdf(la)tex' to get the package work}%
  }{}

  % Check: pdfTeX in pdf mode?
  \ifnum\pdfoutput>0\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {}{%
    \THB@WarnEnd{You need pdfTeX in pdf mode (\string\pdfoutput>0)}%
  }

  % Check: Do file 'thumbdta.tex' exists?
  %
  % \csname-Trick because of \outer definition of \newread.
  % \globaldefs=-1 in order to make the allocation of \newread local.
  \@ifundefined{IfFileExists}{%
    \globaldefs=-1\relax
    \csname newread\endcsname\THB@TempRead
    \def\IfFileExists#1{%
      \openin\THB@TempRead=#1\relax
      \ifeof\THB@TempRead
        \closein\THB@TempRead
        \expandafter\@secondoftwo
      \else
        \closein\THB@TempRead
        \expandafter\@firstoftwo
      \fi
    }%
  }{}
  % Do the check.
  \IfFileExists{thumbdta.tex}{}{%
    \THB@WarnEnd{No file 'thumbdta.tex'}%
  }
\endgroup

\expandafter\chardef\csname THB@AtCode\endcsname=\the\catcode`\@
\catcode`\@=11

% The thumbnails are numbered with the output pages:
% 'thumb001.png' means the thumbnail of the first output page,
% 'thumb028.png' the thumbnail of the 28th written page,
% regardless of the page number.
%   Therefore we need a new counter that is incremented with each
% page that is written to the pdf file.
\newcount\c@thumb
\c@thumb 0\relax
\newif\ifTHB@thisthumb

\def\thisthumb#1{%
  \global\THB@thisthumbtrue
  \THB@AddPageAttr{#1}%
}

% Now we enhance \shipout in order to
% * increment the output page counter (\c@thumb),
% * check if a thumbnail exists,
% * add/remove the thumbnail entry to the pdf page attributes.
\let\THBorg@shipout\shipout
\def\shipout{\THB@AddThumb\THBorg@shipout}
\def\THB@AddThumb{%
  \begingroup
    \global\advance\c@thumb by 1\relax
    \ifTHB@thisthumb
      \global\THB@thisthumbfalse % reset switch
    \else
      \THB@AddPageAttr{\the\c@thumb}%
    \fi
  \endgroup
}
\def\THB@AddPageAttr#1{%
  % Remove previous /Thumb entry.
  \expandafter\THB@RemoveThumbAttr\the\pdfpageattr^^J/Thumb{} 0 R\END
  % Check, if thumb exists.
  \expandafter\ifx\csname thumb#1\endcsname\relax
    % \PackageWarning cannot be used, because \protect=\noexpand.
    \immediate\write16{}%
    \immediate\write16{Package \THB@name\space Warning: %
      Thumbnail '#1' undefined %
      on input line \the\inputlineno.%
    }%
    \immediate\write16{}%
  \else
    \begingroup
      \edef\x{\endgroup
        \global\pdfpageattr{%
          \the\pdfpageattr
          ^^J/Thumb \csname thumb#1\endcsname\space 0 R%
        }%
      }%
    \x
  \fi
}
\def\THB@RemoveThumbAttr#1^^J/Thumb#2#3 0 R#4\END{%
  \ifx\\#2\\%
    \global\pdfpageattr{#1}%
  \else
    \THB@RemoveThumbAttr#1#4\END
  \fi
}

% Many commands and catcode changes are only necessary to read
% the file 'thumbdta.tex', so we do this in a group.
\begingroup

  % These commands are used in 'thumbdta.tex'
  \def\DefThumb#1{%
    \expandafter\xdef\csname thumb#1\endcsname{\the\pdflastobj}%
  }%

  \def\DefRGB#1{%
    \expandafter\edef\csname thumbRGB#1\endcsname{\the\pdflastobj}%
  }%
  \def\UseRGB#1{\csname thumbRGB#1\endcsname}%

  % Now the catcodes to read 'thumbdta.tex' follows:
  \def\SetCatcodeRange#1#2{%
    \c@thumb=#1
    \loop
      \catcode\c@thumb=12
    \ifnum\c@thumb<#2
      \advance\c@thumb by 1
    \repeat
  }
  \SetCatcodeRange{0}{12}%
  % ^^0d
  \SetCatcodeRange{14}{31}%
  % space
  \SetCatcodeRange{33}{36}% ! " # $
  % percent
  \SetCatcodeRange{38}{63}% & ' ( ) * + , - . / 0-9 : ; < = > ?
  % @ A-Z
  \catcode`\[=12
  % backslash
  \catcode`\]=12
  % ^
  \SetCatcodeRange{95}{96}% _ '
  % a-z {
  \catcode`\|=12
  % }
  \SetCatcodeRange{126}{255}% ~ 127 8-bit

  % commands for special characters used in 'thumbdta.tex'.
  \def\ {\space}%
  \def\+{\string^}%
  \begingroup
    \escapechar=`\\
    \catcode`\|=0
    |catcode`\^^0d=\active%
    |catcode`\\=12%
    |edef|x{|endgroup%
      |noexpand|def|noexpand|\{\}%
      |noexpand|def|noexpand|/{|string^^0d}%
    }%
  |x%
  \begingroup
    \escapechar=-1
    \edef\x{\endgroup
      \noexpand\def\noexpand\{{\string\{}%
      \noexpand\def\noexpand\}{\string\}}%
      \noexpand\def\noexpand\%{\string\%}%
    }%
  \x

  % process 'thumbdta.tex' and include the thumbnails
  % in the current pdf file
  \def\x{\input thumbdta\relax}%
  \ifx\@@input\@undefined
  \else
    \ifx\@@input\relax
    \else
      \def\x{\input{thumbdta}}
    \fi
  \fi
  \x
\endgroup
% restore catcode of '@'.
\catcode`\@=\THB@AtCode\relax
\endinput
