% Copyright (c) 2003-2021 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}[2021/02/06 v1.0j CMap support: searchable PDF]

\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

\@ifundefined{pdffontattr}{%
  \@ifundefined{pdfextension}{%
% The cmap package can be used only with pdflatex and lualatex,
% but not with ordinary latex
    \PackageWarningNoLine{cmap}{pdftex not detected - exiting}%
    \endinput
  }{%
    \protected\def\cmap@pdfobj{\pdfextension obj }%
    \protected\def\cmap@fontattr{\pdfextension fontattr }%
    \def\cmap@lastobj{\number\pdffeedback lastobj \space 0 R}%
  }%
}{%
  \let\cmap@pdfobj\pdfobj
  \let\cmap@fontattr\pdffontattr
  \def\cmap@lastobj{\number\pdflastobj\space 0 R}%
}

\@ifundefined{pdfoutput}{%
  \ifnum\outputmode<1
}{%
  \ifnum\pdfoutput<1
}%
  \PackageWarningNoLine{cmap}{pdftex in DVI mode - exiting}
  \expandafter\endinput
\fi

\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

\@ifundefined{pdfnobuiltintounicode}{%
  \@ifundefined{pdfgentounicode}\relax{%
    \pdfgentounicode=0
  }%
  \protected\def\cmap@nobuiltintounicode{%
    \expandafter\@gobble\the
  }%
}{%
  \let\cmap@nobuiltintounicode\pdfnobuiltintounicode
}

\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\cmap@pdfobj stream
      %attr {/Type /CIDFile}
      file {\reserved@f}%
    \expandafter\xdef\csname cmap@set@\cmap@f@encoding\endcsname{%
      \cmap@nobuiltintounicode\noexpand\font@name
      \noexpand\expandafter\cmap@fontattr\noexpand\font@name{/ToUnicode \cmap@lastobj}}%
  }{%
    \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
        \ifdim\fontcharwd\font@name`\i=\fontcharwd\font@name`\w
          \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
