% File:        thumbpdf.sty
% Project:     thumbpdf
% Version:     1999/05/05 v1.4
% Author:      Heiko Oberdiek
%
% Function:    Inclusion of thumbnails
%
% Copyright:   Copyright (C) 1999 Heiko Oberdiek.
%
%              This program can be redistributed and/or modified
%              under the terms of the LaTeX Project Public License
%              distributed from CTAN archives in directory
%              macros/latex/base/lppl.txt; either version 1 of
%              the License, or (at your option) any later version.
%
%              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.
%
% 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. The \csname construct is
% necessary, because the catcode of the character '@' is not known
% yet.
\expandafter\newcount\csname c@thumb\endcsname
\csname c@thumb\endcsname0\relax
\expandafter\newif\csname if@thisthumb\endcsname

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

% Now we can use '@' in command names.
\catcode`\@=11

% This command end the processing of the file
% if TeX uses a plain or LaTeX format.
\def\@ThumbEND{\csname @@end\endcsname\end}%

% 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

% Package identification in the log file
\@ifundefined{ProvidesPackage}{%
  \immediate\write-1{%
    Package: thumbpdf 1999/05/05 v1.4 Inclusion of thumbnails (HO)%
  }%
}{%
  \ProvidesPackage{thumbpdf}[%
    1999/05/05 v1.4 Inclusion of thumbnails (HO)]%
}

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

% dummy for \thisthumb
\gdef\thisthumb#1{}

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

% Check: package already loaded?
\@ifundefined{@thumb@orgshipout}{}{%
  \@WarnEnd{You have already loaded this package}%
}

% \csname-Trick because of \outer definition of \newread
\@ifundefined{IfFileExists}{%
  \csname newread\endcsname\@ThumbRead
  \def\IfFileExists#1{%
    \openin\@ThumbRead=#1\relax
    \ifeof\@ThumbRead
      \closein\@ThumbRead
      \expandafter\@secondoftwo
    \else
      \closein\@ThumbRead
      \expandafter\@firstoftwo
    \fi
  }%
}{}

% Check: exists file 'thumbdta.tex'?
\IfFileExists{thumbdta.tex}{}{%
  \@WarnEnd{No file 'thumbdta.tex'}%
}

\@ifundefined{PackageWarning}{%
  \gdef\@thumbwarning#1{%
    \immediate\write16{Package thumbpdf Warning: #1 %
      on input line \the\inputlineno.}%
  }%
}{%
  \gdef\@thumbwarning{\PackageWarning{thumbpdf}}%
}
\gdef\thisthumb#1{%
  \expandafter\ifx\csname thumb#1\endcsname\relax
    \@thumbwarning{Thumbnail '#1' undefined}%
  \else
    \global\@thisthumbtrue
    \begingroup
      \edef\x{/Thumb \csname thumb#1\endcsname\space0 R}%
      \global\pdfpageattr\expandafter{\x}%
    \endgroup
  \fi
}

% 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.
\global\let\@thumb@orgshipout\shipout
\gdef\shipout{\AddThumb\@thumb@orgshipout}
\gdef\AddThumb{%
  \begingroup
    \global\advance\c@thumb by 1\relax
    \if@thisthumb
      \global\@thisthumbfalse % reset switch
    \else
      \expandafter\ifx\csname thumb\the\c@thumb\endcsname\relax
        \expandafter\@RemoveThumbAttr\the\pdfpageattr/Thumb{} 0 R\END
      \else
        \edef\x{/Thumb \csname thumb\the\c@thumb\endcsname\space0 R}%
        \global\pdfpageattr\expandafter{\x}%
      \fi
    \fi
  \endgroup
}
\gdef\@RemoveThumbAttr#1/Thumb#2#3 0 R#4\END{%
  \ifx\\#2\\%
    \global\pdfpageattr{#1}%
  \else
    \@RemoveThumbAttr#1#4\END
  \fi
}

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

\def\DefRGB#1{%
  \expandafter\xdef\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
\@ifundefined{@@input}{\input thumbdta.tex\relax}%
                      {\input{thumbdta.tex}}

\endgroup
\endinput
