%%
%% This is file 'pxjahyper-enc.sty'.
%%
%% Copyright (c) 2012-2026 Takayuki YATO (aka. "ZR")
%%   GitHub:   https://github.com/zr-tex8r
%%   Twitter:  @zr_tex8r
%%
%% This package is distributed under the MIT License.
%%

%% package declaration
\NeedsTeXFormat{pLaTeX2e}
\ProvidesPackage{pxjahyper-enc}[2026/04/26 v1.6a]
\def\pxhe@pkgname{pxjahyper-enc}
\providecommand{\bxDebug}[1]{}

%--------------------------------------- package options

%% variables
\let\pxhe@driver=d
\let\pxhe@driver@opt\relax
\newif\ifpxhe@bxdvipdfmx@avail

% driver options
% NB: Only global options are officially supported.
\DeclareOption{dvipdfmx}{%
  \let\pxhe@driver=d\relax
  \def\pxhe@driver@opt{dvipdfmx}%
}
\DeclareOption{dvips}{%
  \let\pxhe@driver=s\relax
  \def\pxhe@driver@opt{dvips}%
}
\DeclareOption{nodvidriver}{%
  \let\pxhe@driver=0\relax
  \def\pxhe@driver@opt{nodvidriver}%
}
% dispatch
\ProcessOptions*

% load 'bxdvipdfmx-tounicode' if available
\IfFileExists{bxdvipdfmx-tounicode.sty}{%
  \pxhe@bxdvipdfmx@availtrue
  \ifx\pxhe@driver@opt\relax\else
    \PassOptionsToPackage{\pxhe@driver@opt}{bxdvipdfmx-tounicode}%
  \fi
  \RequirePackage{bxdvipdfmx-tounicode}[]
}{}

%--------------------------------------- user interface

%% variables
\let\pxhe@bigcode=t
\let\pxhe@tounicode\@empty
\let\pxhe@tounicode@default\@empty

%%<*> \suppressbigcode
\@onlypreamble\suppressbigcode
\@ifdefinable\suppressbigcode{%
  \DeclareRobustCommand*\suppressbigcode{%
    \global\let\pxhe@bigcode=f\relax
  }
}

%%<*> \suppressdefaulttounicode
\@onlypreamble\suppressdefaulttounicode
\@ifdefinable\suppressdefaulttounicode{%
  \DeclareRobustCommand*\suppressdefaulttounicode{%
    \global\let\pxhe@tounicode@default\@empty
  }
}

%%<+> \pxjahyperToUnicodeSpecial{<name>}
\ifpxhe@bxdvipdfmx@avail
  \newcommand*\pxjahyperToUnicodeSpecial[1]{%
    \bxdvipdfmxToUnicodeSpecial{#1}%
  }
\else
  \@ifdefinable\pxjahyperToUnicodeSpecial{%
    \DeclareRobustCommand*\pxjahyperToUnicodeSpecial[1]{%
      \def\pxhe@tounicode{#1}%
    }%
  }
\fi

%--------------------------------------- issue the special

\AtEndOfPackage{%
  \ifpxhe@bxdvipdfmx@avail
    \bxdvipdfmxToUnicodeSpecialDefault\pxhe@tounicode@default
  \else
    % The value is expanded here.
    \pxhe@begin@dvi@hook{%
      \edef\pxhe@tmpa{\pxhe@tounicode}%
      \ifx\pxhe@tmpa\@empty
        \edef\pxhe@tmpa{\pxhe@tounicode@default}%
      \fi
      \ifx\pxhe@tmpa\@empty\else
        \special{pdf:tounicode \pxhe@tmpa}%
      \fi
    }%
  \fi
}

%--------------------------------------- default tounicode special

%% \pxhe@jcode
\ifnum     \jis"2121="8140
  \let\pxhe@jcode=s\relax
\else\ifnum\jis"2121="A1A1
  \let\pxhe@jcode=e\relax
\else\ifnum\jis"2121="3000
  \let\pxhe@jcode=u\relax
\fi\fi\fi
\ifx\pxhe@jcode\@undefined
  \PackageWarningNoLine\pxhe@pkgname
   {Unknown internal kanji code}
  \AtEndOfPackage{\suppressdefaulttounicode}
\fi

%% \pxhe@tounicode@default
\def\pxhe@tounicode@default{%
  \if e\pxhe@jcode % euc
    EUC-UCS2%
  \else\if s\pxhe@jcode % sjis
    90ms-RKSJ-UCS2%
  \else\if t\pxhe@bigcode % ucs, bigcode
    UTF8-UTF16%
  \else % ucs, no bigcode
    UTF8-UCS2%
  \fi\fi\fi
}

%--------------------------------------- environment

%% quit if using bxdvipdfmx
\ifpxhe@bxdvipdfmx@avail
\expandafter\endinput\fi\relax

%% check driver
\ifx d\pxhe@driver\else
  \ifx s\pxhe@driver
    \PackageWarningNoLine\pxhe@pkgname
     {Unsupported driver (dvips)}
  \fi
  \let\pxhe@begin@dvi@hook\@gobble
\expandafter\endinput\fi\relax

%% switch 'pxhe@new@hook@system'
\newif\ifpxhe@new@hook@system
\@ifl@t@r\fmtversion{2020/10/01}{\pxhe@new@hook@systemtrue}{}

%--------------------------------------- begin-dvi hook (new)
\ifpxhe@new@hook@system

%% \pxhe@hook@label
\def\pxhe@hook@label{pxjahyper/enc}

%% \pxhe@begin@dvi@hook{<text>}
\def\pxhe@begin@dvi@hook#1{%
  \AddToHook{shipout/firstpage}[\pxhe@hook@label]{#1}%
}

%% declare necessary rules
\DeclareHookRule{shipout/firstpage}{\pxhe@hook@label}{<}{hyperref}

\fi
%--------------------------------------- begin-dvi hook (old)
\ifpxhe@new@hook@system\else

%% packages
\RequirePackage{atbegshi}[2011/10/05]% v1.16

%% \pxhe@begin@dvi@hook{<text>}
\def\pxhe@begin@dvi@hook#1{%
  \begingroup
    \toks\z@{#1}%
    \toks\tw@\expandafter{\AtBegShi@HookFirst}%
    \xdef\AtBegShi@HookFirst{\the\toks@\the\toks\tw@}%
  \endgroup
}

\fi
%--------------------------------------- done
\endinput
%% EOF
