% Copyright (c) 2003 Vladimir Volovich <vvv@vsu.ru>
% cmap package -- download CMap files into PDF
%   to make "search" and "cut-n-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}.cmap
% Usage: put \usepackage{cmap} immediately after the \documentclass line
% Thanks to:
%   Han The Thanh
%   Maxim I. Tishin
%   Petr Sojka
%   Werner Lemberg
% TODO:
%   add *.cmap files for other font encodings (contributions are welcome):
%     TS1, OT1, OT2, LY1, IL2, OML, OMS, ...
%   support dvips?
% History:
% 2003/03/07 private version
% 2003/03/11 version 1.0: first public version
% 2003/03/13 version 1.0a:
%   change error to warning for non-pdftex
%   added warnings if cmap is loaded after fontenc or babel
%   added t5.cmap - thanks to Han The Thanh
% 2003/03/19 version 1.0b:
%   minor refinements
% 2003/05/22 version 1.0c:
%   fixed a typo due to which the package had no effect at all

\ProvidesPackage{cmap}[2003/05/22 v1.0c 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

\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{\f@encoding.cmap}}}%
  \reserved@f
  \IfFileExists{\reserved@f}{%
    \immediate\pdfobj stream
      %attr {/Type /CIDFile}
      file {\reserved@f}%
    \expandafter\xdef\csname cmap@set@\f@encoding\endcsname{%
      \noexpand\expandafter\pdffontattr\noexpand\font@name{/ToUnicode \the\pdflastobj\space 0 R}}%
  }{%
    \global\expandafter\let\csname cmap@set@\f@encoding\endcsname\empty
  }%
}
\def\cmap@hook{%
  \@ifundefined{cmap@set@\f@encoding}{\cmap@load}\relax
  \csname cmap@set@\f@encoding\endcsname
}

\endinput
