%%
%% This is file 'bxtoolbox-ja.def'.
%%

%% file declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{bxtoolbox-ja.def}[2023/02/23 v1.2a]

%% check intergrity
\edef\bxtb@tmpa{\@currname.\@currext}\def\bxtb@tmpb{bxtoolbox-ja.def}
\ifx\bxtb@tmpa\bxtb@tmpb\else
  \PackageError\bxtb@pkgname{Illegal load sequence}\@eha
\expandafter\endinput\fi\relax

%% load submodule
\providecommand*\bxRequireDefinition[1]{%
  \def\bx@@def{def}\@onefilewithoptions{#1}[][]\bx@@def}
\bxRequireDefinition{bxtoolbox-ext}

%% code guards
\edef\bxtb@j@restore@codes{%
  \catcode34=\the\catcode34%
  \catcode45=\the\catcode45%
  \catcode46=\the\catcode46%
  \catcode47=\the\catcode47%
  \catcode58=\the\catcode58%
  \catcode59=\the\catcode59%
  \catcode63=\the\catcode63%
  \catcode96=\the\catcode96%
\relax}
\catcode34=12 % <">
\catcode45=12 % <->
\catcode46=12 % <.>
\catcode47=12 % </>
\catcode58=12 % <:>
\catcode59=12 % <;>
\catcode63=12 % <?>
\catcode96=12 % <`>
\AtEndOfPackage{%
  \bxtb@j@restore@codes
  \undef\bxtb@j@restore@codes}

%--------------------------------------- pTeX something

%% switch 'bxtb@pxbase@ok'
\newbool{bxtb@pxbase@ok}
\ifbxinpTeX
  \IfFileExists{pxbase.sty}{%
    \bxtb@pxbase@oktrue
  }{}
\fi

%%<+> \bxToYokoDir
% Switches to horizontal(yoko) writing direction
% when using pTeX.
\ifbxinpTeX         %----<*pTeX>
\newcommand*\bxToYokoDir{%
  \bxIfydir{}{\yoko}}
\else               %----<*!pTeX>
\newcommand*\bxToYokoDir{}
\fi                 %----</pTeX>

%%<+> \bxAtBeginDviX
% \AtBeginDvi that is safe to use in pTeX.
\ifx\AddToHook\@undefined %----
\newcommand\bxAtBeginDviX[1]{%
  \global\setbox\@begindvibox\vbox{%
    \bxToYokoDir\unvbox\@begindvibox#1}}
\else %----
\newcommand\bxAtBeginDviX[1]{%
  \AtBeginDvi{#1}}
\fi %----

%--------------------------------------- Japanese something

%%<+> \bxGetZenkakuWidth
% Assigns 1zw to \bxResDim
\ifbxinpTeX
  \bxNewrobustcmd*\bxGetZenkakuWidth{%
    \bxResDim=1zw\relax}
\else\ifbxHasAlUcsChar
  \bxToUcsChar{"4E00}\let\bx@jan@onekanji\bxRes
  \bxNewrobustcmd*\bxGetZenkakuWidth{%
    \begingroup \setbox\z@\hbox{\bx@jan@onekanji}%
      \xdef\bx@jan@g@next{\bxResDim=\the\wd\z@\relax}%
    \endgroup \bx@jan@g@next}
  \AtBeginDocument{%
    \ifdef\jsZw{%
      \bxRenewrobustcmd*\bxGetZenkakuWidth{%
        \bxResDim=1\jsZw}}{}%
    \ifdef\zw{%
      \bxRenewrobustcmd*\bxGetZenkakuWidth{%
        \bxResDim=1\zw}}{}%
  }
\fi\fi

%%<+> \bxIfCharToken{<token>}{<yes>}{<no>}
% Tests if the token is a character token with catcode 11 or 12.
\newcommand*\bxIfCharToken[1]{%
  \bxIfcat{\relax\noexpand#1}{\@secondoftwo}%
    {\expandafter\bxtb@ifchartok@a\meaning#1\bxtb@end}}
\def\bxtb@ifchartok@a#1{%
  \bxIfcsundefX{bxtb@ICTF/#1}{\bxtb@ifchartok@false}%
    {\bxtb@ifchartok@b#1}}
\def\bxtb@ifchartok@b#1#2#3#4#5#6{%
  \bxIfcsundefX{bxtb@ICT/#1#2#3#4#5#6}{\bxtb@ifchartok@false}%
    {\bxtb@ifchartok@true}}
\def\bxtb@ifchartok@true#1\bxtb@end#2#3{#2}
\def\bxtb@ifchartok@false#1\bxtb@end#2#3{#3}
\def\do#1#2:{\cslet{bxtb@ICTF/#1}{t}\cslet{bxtb@ICT/#1#2}{t}}
\do thelet:% 11
\do thecha:% 12
\do kanjic:% 16-19(upTeX)

%--------------------------------------- kanji-encoding something

%%<+> \bxInternalJaEncoding
\let\bxInternalJaEncoding=?
\ifbxHasAlUcsChar
  \let\bxInternalJaEncoding=u
\else\ifbxinpdfTeX
  \let\bxInternalJaEncoding=b
\else\ifbxinpTeX
  \ifnum\jis"2121="8140 \let\bxInternalJaEncoding=s\else
  \ifnum\jis"2121="A1A1 \let\bxInternalJaEncoding=e\else
  \ifnum\jis"2121="3000 \let\bxInternalJaEncoding=u\fi\fi\fi
\fi\fi\fi

%%<+> \bxInputJaEncoding
\let\bxInputJaEncoding=?
\ifbxHasAlUcsChar
  \let\bxInputJaEncoding=u
\else\ifbxinpdfTeX
  \let\bxInputJaEncoding=b
\else\ifbxtb@pxbase@ok
  \@@input pxbsjc.def\relax
  \def\bxtb@tmpb#1#2\@nil{%
    \ifnum `#1=\jis"723F \let\bxInputJaEncoding=s\else
    \ifnum `#1=\jis"693D \let\bxInputJaEncoding=e\else
    \ifnum `#1=\jis"7379 \let\bxInputJaEncoding=u\fi\fi\fi}
  \expandafter\bxtb@tmpb\bxtb@tmpa\@nil
  \ifx e\bxInputJaEncoding
    \@@input pxbsjc1.def\relax
    \def\bxtb@tmpb#1#2\@nil{%
      \ifnum `#1=\jis"723F \let\bxInputJaEncoding=a\fi}
    \expandafter\bxtb@tmpb\bxtb@tmpa\@nil
  \fi
\fi\fi\fi

%%<+> \bxOutputJaEncoding
\let\bxOutputJaEncoding=?
% if input is 'auto', output should not be equal to it,
% but I don't know what to do then.
\let\bxOutputJaEncoding=\bxInputJaEncoding

%% \bxtb@jaencname
\def\bxtb@jaencname#1#2{%
  \ifx u#1#2\else
  \ifx s#1sjis\else
  \ifx e#1euc\else
  \ifx a#1auto\else
  \ifx b#1byte\else
  (unknown)\fi\fi\fi\fi\fi}

%%<*> \internaljaencodingname
\newcommand*\internaljaencodingname{}
\edef\internaljaencodingname{%
  \bxtb@jaencname\bxInternalJaEncoding{unicode}}

%%<*> \inputjaencodingname
\newcommand*\inputjaencodingname{}
\edef\inputjaencodingname{%
  \bxtb@jaencname\bxInputJaEncoding{utf8}}

%%<*> \outputjaencodingname
\newcommand*\outputjaencodingname{}
\edef\outputjaencodingname{%
  \bxtb@jaencname\bxOutputJaEncoding{utf8}}

%%<*> \infojenc
\newcommand*\infojenc{%
  \typeout{%
  Kanji encoding:
  input=\inputjaencodingname;
  output=\outputjaencodingname;
  internal=\internaljaencodingname}%
}

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