% Copyright (c) 2003-2007 Vladimir Volovich <vvv@vsu.ru>
% cmap package -- download CMap files into PDF
%   to make "search" and "copy-and-paste" functions work properly
% You may distribute and/or modify this program under the terms of LPPL
% the program consists of cmap.sty and {t1,t2a,t2b,t2c,t5,ot1,ot1tt,lae,lfe}.cmap
% Usage: put \usepackage{cmap} immediately after the \documentclass line
% Thanks to:
%   Han The Thanh
%   Maxim I. Tishin
%   Petr Sojka
%   Werner Lemberg
%   Oleg Katsitadze

\ProvidesPackage{cmap}[2007/04/25 v1.0g CMap support: searchable PDF]

\@ifundefined{pdffontattr}{%
% The cmap package can be used only with pdflatex,
% but not with ordinary latex
  \PackageWarningNoLine{cmap}{pdftex not detected - will not do anything}%
  \endinput
}\relax
\ifnum\pdfoutput<1
  \PackageWarningNoLine{cmap}{pdftex in DVI mode - will not do anything}
  \expandafter\endinput
\fi

\DeclareOption{resetfonts}{%
  \@for\reserved@a:=%
    OT1/cmr/m/n/5,OT1/cmr/m/n/7,OT1/cmr/m/n/10,%
    OML/cmm/m/it/5,OML/cmm/m/it/7,OML/cmm/m/it/10,%
    OMS/cmsy/m/n/5,OMS/cmsy/m/n/7,OMS/cmsy/m/n/10,%
    OMX/cmex/m/n/10%
  \do{%
    \expandafter\let\csname\reserved@a\endcsname\relax
  }%
}
\ProcessOptions

\edef\reserved@a{\noexpand\in@{,fontenc.sty,}{\@filelist}}% enc.def
\reserved@a
\ifin@
\PackageWarningNoLine{cmap}{fontenc already loaded - some fonts may be unprocessed}
\fi
%\@ifpackageloaded{babel}{%
%\PackageWarningNoLine{cmap}{babel already loaded - some fonts may be unprocessed}%
%}\relax

\def\extract@font{%
  \get@external@font
  \global\expandafter\font\font@name\external@font\relax
  \font@name\relax
  \cmap@hook
  \csname\f@encoding+\f@family\endcsname
  \csname\curr@fontshape\endcsname
  \relax
}
\def\cmap@load{%
  \edef\reserved@f{\lowercase{\def\noexpand\reserved@f{\cmap@f@encoding.cmap}}}%
  \reserved@f
  \IfFileExists{\reserved@f}{%
    \immediate\pdfobj stream
      %attr {/Type /CIDFile}
      file {\reserved@f}%
    \expandafter\xdef\csname cmap@set@\cmap@f@encoding\endcsname{%
      \noexpand\expandafter\pdffontattr\noexpand\font@name{/ToUnicode \the\pdflastobj\space 0 R}}%
  }{%
    \global\expandafter\let\csname cmap@set@\cmap@f@encoding\endcsname\empty
  }%
}
\def\cmap@test@tt{%
  \ifdim\fontdimen3\font@name=\z@
    \ifdim\fontdimen4\font@name=\z@
      \begingroup
        \setbox0=\hbox{\font@name i}%
        \setbox1=\hbox{\font@name W}%
        \ifdim\wd0=\wd1 %
          \edef\reserved@f{\lowercase{\def\noexpand\reserved@f{\cmap@f@encoding tt.cmap}}}%
          \reserved@f
          \IfFileExists{\reserved@f}{\xdef\cmap@f@encoding{\cmap@f@encoding tt}}\relax
        \fi
      \endgroup
    \fi
  \fi
}
\def\cmap@hook{%
  \@ifundefined{CJK@plane}{%
    \edef\cmap@f@encoding{\f@encoding}%
  }{%
    \edef\cmap@f@encoding{\f@encoding\CJK@plane}%
  }%
  \cmap@test@tt
  \@ifundefined{cmap@set@\cmap@f@encoding}{\cmap@load}\relax
  \csname cmap@set@\cmap@f@encoding\endcsname
}

\endinput
