%%
%% This is file 'pxjahyper-uni.def'.
%% 
%% Copyright (c) 2012-2024 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}
\ProvidesFile{pxjahyper-uni.def}[2024/08/25 v1.5]
\providecommand{\bxDebug}[1]{}

%--------------------------------------- environment check

\@ifpackageloaded{hyperref}{}{%else
  \bxDebug{pxjahyper-uni: hyperref not yet loaded}%
  \endinput}

%% if already redefined, then okay
\def\pxhy@tmpa#1{\@backslashchar 000#1}
\ifx\HyPsd@Char\pxhy@tmpa\else
  \bxDebug{pxjahyper-uni: already redefined}
  \let\jsCheckHyperrefUnicode\@empty
\expandafter\endinput\fi\relax

%% if this is pTeX and pxjatoucs is not loaded, then quit
\if \ifnum\jis"2121="3000 F%
    \else\ifx\bxjaSjisToUcs\@undefined T\else F%
    \fi\fi T%
  \bxDebug{pxjahyper-uni: bxjatoucs not yet loaded}
\expandafter\endinput\fi\relax

%--------------------------------------- the patch

%% The following code is taken from hyperref.sty.
%%%%BEGIN
  \def\HyPsd@Char#1{%
    \ifnum`#1<128 %
      \@backslashchar 000#1%
    \else
      \ifnum`#1<65536 %
        \expandafter\HyPsd@CharTwoByte\number`#1!%
      \else
        \expandafter\expandafter\expandafter\HyPsd@CharSurrogate
        \intcalcSub{`#1}{65536}!%
      \fi
    \fi
  }%
  \def\HyPsd@CharTwoByte#1!{%
    \expandafter\expandafter\expandafter\HyPsd@CharOctByte
    \IntCalcDiv#1!256!!%
    \expandafter\expandafter\expandafter\HyPsd@CharOctByte
    \IntCalcMod#1!256!!%
  }%
  \def\HyPsd@CharOctByte#1!{%
    \@backslashchar
    \IntCalcDiv#1!64!%
    \intcalcDiv{\IntCalcMod#1!64!}{8}%
    \IntCalcMod#1!8!%
  }%
  \def\HyPsd@CharSurrogate#1!{%
    \@backslashchar 33%
    \IntCalcDiv#1!262144!%
    \expandafter\expandafter\expandafter\HyPsd@CharOctByte
    \intcalcDiv{\IntCalcMod#1!262144!}{1024}!%
    \@backslashchar 33%
    \expandafter\expandafter\expandafter\IntCalcAdd
    \intcalcDiv{\IntCalcMod#1!1024!}{256}!4!%
    \expandafter\expandafter\expandafter\HyPsd@CharOctByte
    \IntCalcMod#1!256!!%
  }%
%%%%END

\ifnum\jis"2121="3000 \else             %<*pTeX>
  \def\HyPsd@Char#1{%
    \ifnum`#1<128 %
      \@backslashchar 000#1%
    \else\ifnum`#1<256 %
      \expandafter\HyPsd@CharTwoByte\number`#1!%
    \else
      \expandafter\HyPsd@CharTwoByte\number\pxhy@to@ucs{`#1}!%
    \fi\fi
  }%
  \def\pxhy@to@ucs#1{"FFFD}
  \ifnum\jis"2121="8140 % sjis
    \let\pxhy@to@ucs\bxjaSjisToUcs
  \else\ifnum\jis"2121="A1A1 % euc
    \let\pxhy@to@ucs\bxjaEucToUcs
  \fi\fi
\fi                                     %</pTeX>

%% set flag
\let\pxHyperrefUnicodePatched=t
\bxDebug{pxjahyper-uni: patched}

%% disable the verification of bxjscls
\let\jsCheckHyperrefUnicode\@empty

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