%
% bxbase.def
%

% Note: bxbase.def and pxbase.def provide same set of functionality.
% To avoid version clash, when a version of one of them is loaded,
% load of an older version of the other is aborted.

%%%% stuffs read when \input'ed
\ifx\bxbaseOk\undefined
\bgroup
\catcode`\@=11
\gdef\bx@defextension{def}
\gdef\bx@ver@#1{\expandafter\expandafter\expandafter\@parse@version
  \csname ver@#1.\@currext\endcsname//00\@nil}
  %% \bxInputDefFile{<def_name>}
\gdef\bxInputDefFile#1{\@onefilewithoptions{#1}[][]\bx@defextension}
  %% \bxCheckCPart{<def_name>}
\gdef\bxCheckCPart#1{%
  \@ifl@aded\@currext{#1}{%
    \ifnum\bx@ver@{#1}>\bx@ver@\@currname \expandafter\endinput\fi}{}}
\egroup
\let\bxbaseOk=t
\bxInputDefFile{bxbase}  %% read this file again
\expandafter\endinput\fi

%%--------------- folloing stuffs are read by \bxTnext

%%%% file declaration
\NeedsTeXFormat{LaTeX2e}
\ProvidesFile{bxbase.def}[2010/06/15 v0.5 BX/PX base library]
\bxCheckCPart{pxbase}
%* v0.5.r13

%%%% error messages
\def\bx@b@pkgname{BX/PXbase} % bundle name
\def\bx@b@error{\PackageError\bx@b@pkgname}
\def\bx@err@nobbl{\bx@b@error{babel not yet loaded}\@eha}
\def\bx@err@oorng{\bx@b@error{Number out of range}\@eha}
\def\bx@err@nanbc{\bx@b@error{Non-BMP characters not supported}\@eha}
\def\bx@err@ilnfm{\bx@b@error{Illegal number format}\@eha}
\def\bx@err@uddrv#1{\bx@b@error{\string#1 is undefined}\@eha}

%%%% common stuffs
\ifx\bxUcv\@undefined
\newcount\bxUcv       % argument of \bx@Ux
\fi
\newif\ifbxOk         % all-purpsoe switch
\let\bxHex\relax      % result of \bxToHex...
\let\bxRes\relax      % result of \bxToLower
\edef\bx@b@resetcatcodes{% < ! " ' + ^ >
  \catcode33=\the\catcode33\catcode34=\the\catcode34%
  \catcode39=\the\catcode39\catcode43=\the\catcode43%
  \catcode94=\the\catcode94\relax}
\catcode33=12 \catcode34=12 \catcode39=12 \catcode43=12
\catcode94=6

%%------ Programming tools

%%%% \bxNullify\CS
% Make \CS void and unoverridable by \providecommand.
\def\bxNullify#1{\global\let#1\bx@nullified}
\def\bx@nullified{\@gobble\bx@@nullified}

%%%% \bxDebug{<text>}
% If \bxUseDebug is defined when bxbase is loaded, this command
% prints debug messages; otherwise it is ignored.
%\let\bx@typeout\typeout
%\ifx\bxUseDebug\@undefined \let\bxDebug\@gobble
%\else \def\bxDebug#1{\bx@typeout{BX debug: #1}}\fi
\providecommand*\bxDebug{\@gobble}

%%%% \bxCheckMA\CS <text> \bxEndCheckMA
% If this construct appears in moving arguments, it expands to
% an error message command.
\def\bxCheckMA{\let\bx@chkma@a\bx@chkma@a\noexpand}
\let\bxEndCheckMA\@empty \let\bx@chkma@z\relax
\def\bx@chkma@a#1{\bx@chkma@z\relax\bx@chkma@b}
\def\bx@chkma@b#1#2#3\bxEncCheckMA{\noexpand\bx@err@ivfrg#1#2}
\def\bx@err@ivfrg#1{\bx@b@error{Cannot use \string#1 here}\@eha}

%%------ TeX engine distinction

%%%% \bxIfPrimitive\CS{<yes>}{<no>}
% Checks if \CS is a primitive.
\def\bxIfPrimitive#1{%
  \edef\bx@tmpa{\string#1}\edef\bx@tmpb{\meaning#1}%
  \ifx\bx@tmpa\bx@tmpb \expandafter\@firstoftwo
  \else \expandafter\@secondoftwo \fi}

%%%% \bxEngineTypeX / \bxEngineTypeY
% Numbers that signify what I am.
  % \bxEngineTypeX values
\chardef\bxETTeX=0     % none of those
\chardef\bxETpTeX=1    % pTeX-extended
\chardef\bxETXeTeX=2   % XeTeX-extended
\chardef\bxETOmega=3   % Omega-extended
  % \bxEngineTypeY values (plus \bxETTeX)
\chardef\bxETeTeX=1    % e-TeX-extended
\chardef\bxETpdfTeX=3  % pdfTeX-extended
  % (here old pdfTeX without e-TeX is ignored)
  %
\bxIfPrimitive\kansuji{\let\bxEngineTypeX\bxETpTeX}{%
\bxIfPrimitive\XeTeXversion{\let\bxEngineTypeX\bxETXeTeX}{%
\bxIfPrimitive\Omegaversion{\let\bxEngineTypeX\bxETOmega}{%
  \let\bxEngineTypeX\bxETTeX}}}
\bxIfPrimitive\eTeXversion{\let\bxEngineTypeY\bxETeTeX}{%
\bxIfPrimitive\pdftexversion{\let\bxEngineTypeY\bxETpdfTeX}{%
  \let\bxEngineTypeY\bxETTeX}}

%%%% \bx@b@uptex
\bxIfPrimitive\kchar{\chardef\bx@b@uptex\@ne}{\chardef\bx@b@uptex\z@}

%%------ Babel something

%%<+> \bxBDHookBabel
% A begindocument-hook about Babel.
\newif\ifbx@bbl@inuse
\def\bx@bbl@bdhook{}
\providecommand\bxBDHookBabel{\bx@bdhook@babel@do}
\def\bx@bdhook@babel@do{%
  \AtBeginDocument{%
    \@ifpackageloaded{babel}{%
      \bx@bbl@inusetrue
      \ifx\bx@bbl@captlang\@undefined\else \bx@bbl@do@captlang \fi
      \bx@bbl@bdhook
    }{}}%
  \bxNullify\bxBDHookBabel}
\@onlypreamble\bxBDHookBabel
\@onlypreamble\bx@bdhook@babel@do

%%<+> \bxAtBeginDocumentBabel{<text>}
% Register a begin-document hook that willl be invoked only when babel
% is loaded.
\def\bxAtBeginDocumentBabel{%
  \g@addto@macro\bx@bbl@bdhook}
\@onlypreamble\bxAtBeginDocumentBabel
\@onlypreamble\bx@bbl@bdhook

%%%% \bx@bbl@patch@sel@lang
% A patch to \select@language that disables automatic switching of
% caption language.
\def\bx@bbl@patch@sel@lang{%  this wlll be never invoked twice
  \global\let\bx@bbl@org@select@language\select@language
  \gdef\select@language##1{%
    \bx@bbl@copy{**C}{##1}\bx@bbl@copy{##1}{**Z}%
    \bx@bbl@org@select@language{##1}\bx@bbl@copy{##1}{**C}}}
\@onlypreamble\bx@bbl@patch@sel@lang

%%%% \bx@bbl@do@captlang
\def\bx@bbl@@main{main}
\def\bx@bbl@@default{default}
\def\bx@bbl@do@captlang{%
  % set captions
    \ifx\bx@bbl@captlang\bx@bbl@@main
      \let\bx@bbl@captlang\bbl@main@language \fi
    \ifx\bx@bbl@captlang\bx@bbl@@default
    \else\expandafter\ifx\csname l@\bx@bbl@captlang\endcsname\relax
      \@nolanerr\bx@bbl@captlang
    \else \csname date\bx@bbl@captlang\endcsname
          \csname captions\bx@bbl@captlang\endcsname
    \fi\fi}
\@onlypreamble\bx@bbl@do@captlang

%%%% \bx@bbl@copy{<lang1>}{<lang2>}
% Copies \captions... and \date... for save.
\def\bx@bbl@copy#1#2{%
  \bx@bbl@copy@a{#1}{#2}{captions}%
  \bx@bbl@copy@a{#1}{#2}{date}}
\def\bx@bbl@copy@a#1#2#3{%
  \edef\bx@tmpa{\let\expandafter\noexpand\csname#3#1\endcsname
    \expandafter\noexpand\csname#3#2\endcsname}\bx@tmpa}
\@namedef{captions**Z}{\@empty}
\@namedef{date**Z}{\@empty}

%%<*> \bxcaptionlanguage{<lang>}
% Change the caption language manually, with disabling automatic
% switching on it.
\def\bxcaptionlanguage#1{%
  \@ifpackageloaded{babel}%
   {\ifx\bx@bbl@captlang\@undefined \bx@bbl@patch@sel@lang \fi
    \xdef\bx@bbl@captlang{#1}}%
   {\bx@err@nobbl}}
\@onlypreamble\bxcaptionlanguage

%%<*> \bxmainlanguage{<lang>}
% Changes the 'main' language (one that is effective at beginning).
\def\bxmainlanguage#1{%
  \expandafter\ifx\csname date#1\endcsname\relax
    \@nolanerr{#1}\fi
  \xdef\bbl@main@language{#1}}
\@onlypreamble\bxmainlanguage

%%<+> \bxTrivLangDef{<lang>}
% Makes a fake language option.
\def\bxTrivLangDef#1{%
  \@ifundefined{date#1}{%
    \@ifundefined{l@#1}%
      {\expandafter\chardef\csname l@#1\endcsname\z@}{}%
    \@namedef{date#1}{\relax}%
    \@namedef{captions#1}{\relax}%
    \@namedef{extras#1}{\relax}%
    \@namedef{noextras#1}{\relax}%
    \@namedef{date#1}{\relax}%
  }{}}
\@onlypreamble\bxTrivLangDef

%%------ 'Number-to-hex-string' functions
% All public commands put their result in \bxHex.

%%%% preparation
\newif\ifbx@hx@bigchar
\ifnum\bxEngineTypeX>\@ne \bx@hx@bigchartrue \fi
\ifnum\bx@b@uptex>\z@ \bx@hx@bigchartrue \fi

%%<+> \bxToHexTiny
% For one-to-two-digit hex strings.
\def\bxToHexTiny#1{%
  \chardef\bxHex=#1\relax
  \expandafter\bx@hx@tohextiny@a\meaning\bxHex\@nil}
\def\bx@hx@tohextiny@a#1"#2\@nil{\def\bxHex{#2}}

%%<+> \bxToHexSmall
% For one-to-three-digit hex strings.
\def\bxToHexSmall#1{%
  \mathchardef\bxHex=#1\relax
  \expandafter\bx@hx@tohexsmall@a\meaning\bxHex\@nil}
\def\bx@hx@tohexsmall@a#1"#2\@nil{\def\bxHex{#2}}

%%<+> \bxToHexTwo
% For zero-padded two-digit hex strings.
\def\bxToHexTwo#1{%
  \chardef\bxHex=#1\relax
  \expandafter\bx@hx@tohextwo@a\meaning\bxHex"0\@nil}
\def\bx@hx@tohextwo@a#1"#2#3#4#5\@nil{\bx@hx@tohextwo@b#2#3#4\@nil}
\def\bx@hx@tohextwo@b#1"#2\@nil{\def\bxHex{#2#1}}

%%<+> \bxToHexThree
% For zero-padded three-digit hex strings.
\def\bxToHexThree#1{%
  \mathchardef\bxHex=#1\relax
  \expandafter\bx@hx@tohexthree@a\meaning\bxHex"00\@nil}
\def\bx@hx@tohexthree@a#1"#2#3#4#5#6\@nil{\bx@hx@tohexthree@b#2#3#4#5\@nil}
\def\bx@hx@tohexthree@b#1"#2\@nil{\def\bxHex{#2#1}}

%%<+> \bxToHexFour
% For zero-padded four-digit hex strings.
\ifbx@hx@bigchar    %----<*bigchar>
\def\bxToHexFour#1{%
  \chardef\bxHex=#1\relax
  \expandafter\bx@hx@tohexfour@a\meaning\bxHex"000\@nil}
\def\bx@hx@tohexfour@a#1"#2#3#4#5#6#7\@nil{\bx@hx@tohexfour@b#2#3#4#5#6\@nil}
\def\bx@hx@tohexfour@b#1"#2\@nil{\def\bxHex{#2#1}}
\else               %----<*!bigchar>
\mathchardef\bx@hx@fzzz="4000
\def\bxToHexFour#1{\bgroup
  \@tempcnta=#1\relax\@tempcntb\@tempcnta\divide\@tempcntb\bx@hx@fzzz
  \ifcase\@tempcntb \advance\@tempcnta\bx@hx@fzzz \or \or
   \advance\@tempcnta-\bx@hx@fzzz \or \advance\@tempcnta-"8000 \or
   \@tempcnta\z@ \fi
  \mathchardef\bx@hx@tmp\@tempcnta
  \expandafter\bx@hx@tohexfour@a\meaning\bx@hx@tmp\@nil
  \egroup \let\bxHex\bx@hx@gtmp}
\def\bx@hx@tohexfour@a#1"#2#3\@nil{%
  \expandafter\let\expandafter\bx@hx@tmp
   \csname bxhx@X/\the\@tempcntb#2\endcsname
  \ifx\bx@hx@tmp\relax \bx@err@oorng \global\let\bx@hx@gtmp\@empty
  \else \xdef\bx@hx@gtmp{\bx@hx@tmp#3}\fi}
\def\bx@hx@nxt#1#2#3{\bgroup\catcode`#3=12
  \global\expandafter\def\csname bxhx@X/#1#2\endcsname{#3}\egroup}
\@for\bx@hx@tmp:={040,051,062,073,144,155,166,177,248,259,26A,27B,%
  34C,35D,36E,37F}\do{\expandafter\bx@hx@nxt\bx@hx@tmp}
\fi                 %----</bigchar>

%%<+> \bxToHexFive
%%<+> \bxToHexFiveX
% For zero-padded five-digit hex strings.
\def\bxToHexFive{\bx@tohexfive@aa\relax}
\def\bxToHexFiveX{\bx@tohexfive@aa
  {\ifnum\@tempcnta>\bx@hx@vdmax \@tempcnta=\bx@hx@vdmax\fi}}
\ifbx@hx@bigchar    %----<*bigchar>
\chardef\bx@hx@vdmax="FFFFF
\def\bx@tohexfive@aa#1#2{%
  {\@tempcnta=#2\relax#1\global\chardef\bx@hx@gtmp=\@tempcnta}%
  \expandafter\bx@hx@tohexfivex@a\meaning\bx@hx@gtmp"0000\@nil}
\def\bx@hx@tohexfivex@a#1"#2#3#4#5#6#7#8\@nil{%
  \bx@hx@tohexfivex@b#2#3#4#5#6#7\@nil}
\def\bx@hx@tohexfivex@b#1"#2\@nil{\edef\bxHex{#2#1}}
\else               %----<*!bigchar>
\edef\bx@hx@vdmax{"FFFFF\space}
\def\bx@tohexfive@aa#1#2{%
  \bgroup \@tempcnta=#2\relax#1%
    \@tempcntb=\@tempcnta \divide\@tempcnta\bx@hx@m\relax
    \bxToHexTwo\@tempcnta \let\bx@hx@tmp\bxHex
    \multiply\@tempcnta-\bx@hx@m\advance\@tempcnta\@tempcntb\relax
    \bxToHexThree\@tempcnta \xdef\bx@hx@gtmp{\bx@hx@tmp\bxHex}%
  \egroup \let\bxHex\bx@hx@gtmp}
\fi                 %----</bigchar>

%%<+> \bxToHexEight
% For zero-padded eight-digit hex strings.
\def\bxToHexEight#1{%
  \bgroup \@tempcnta=#1\relax
    \@tempcntb=\@tempcnta\divide\@tempcnta\p@\relax
    \bxToHexFour\@tempcnta \let\bx@hx@tmp\bxHex
    \multiply\@tempcnta-\p@\advance\@tempcnta\@tempcntb\relax
    \bxToHexFour\@tempcnta \xdef\bx@hx@gtmp{\bx@hx@tmp\bxHex}%
  \egroup \let\bxHex\bx@hx@gtmp}

%%<+> \bxToHexUC
% For zero-padded four-to-six-digit hex strings.
\ifbx@hx@bigchar    %----<*bigchar>
\def\bxToHexUC#1{%
  \chardef\bxHex=#1\relax
  \expandafter\bx@hx@tohexuc@a\meaning\bxHex"\@empty\@empty000\@nil}
\def\bx@hx@tohexuc@a#1"#2#3#4#5#6#7#8#9\@nil{%
  \bx@hx@tohexuc@b#2#3#4#5#6#7#8\@nil}
\def\bx@hx@tohexuc@b#1"#2\@nil{\edef\bxHex{#2#1}}
\else               %----<*!bigchar>
\mathchardef\bx@hx@m="1000
\def\bxToHexUC#1{%
  \bgroup \@tempcnta=#1\relax
    \@tempcntb=\@tempcnta \divide\@tempcnta\bx@hx@m\relax
    \bxToHexSmall\@tempcnta \let\bx@hx@tmp\bxHex
    \multiply\@tempcnta-\bx@hx@m\advance\@tempcnta\@tempcntb\relax
    \bxToHexThree\@tempcnta \xdef\bx@hx@gtmp{\bx@hx@tmp\bxHex}%
  \egroup \let\bxHex\bx@hx@gtmp}
\fi                 %----</bigchar>

%%<+> \bxToDecFour
\def\bxToDecFour#1{%
  \expandafter\bx@hx@todecfour@a\number#1"000\@nil}
\def\bx@hx@todecfour@a#1#2#3#4#5#6\@nil{\bx@hx@todecfour@b#1#2#3#4#5\@nil}
\def\bx@hx@todecfour@b#1"#2\@nil{\def\bxHex{#2#1}}

%%<+> \bxToDecFive
\def\bxToDecFive#1{%
  \expandafter\bx@hx@todecfive@a\number#1"0000\@nil}
\def\bx@hx@todecfive@a#1#2#3#4#5#6#7\@nil{\bx@hx@todecfive@b#1#2#3#4#5#6\@nil}
\def\bx@hx@todecfive@b#1"#2\@nil{\def\bxHex{#2#1}}

%%------ Input through code values

%%<+> \bxBDHookUnicode
% A begindocument-hook about Unicode (& CID) input.
\providecommand\bxBDHookUnicode{\bx@bdhook@unicode@do}
\def\bx@bdhook@unicode@do{%
  \AtBeginDocument{\bx@assign@Ux}%
  \bxNullify\bxBDHookUnicode}
\@onlypreamble\bxBDHookUnicode
\@onlypreamble\bx@bdhook@unicode@do

%%<+> \bxBDHookJisInput
% A begindocument-hook about JIS-code input.
\providecommand\bxBDHookJisInput{\bx@bdhook@jisinput@do}
\def\bx@bdhook@jisinput@do{%
  \AtBeginDocument{\bx@assign@JI}%
  \bxNullify\bxBDHookJisInput}
\@onlypreamble\bxBDHookJisInput
\@onlypreamble\bx@bdhook@jisinput@do

%%<+> \bxFallbackSym
\def\bxFallbackSym#1{\leavevmode\bx@fbs@fnt{\dimen@=\bx@junit\relax
  \hb@xt@\dimen@{\hss\vrule\@width\z@\@height.88\dimen@\@depth.12\dimen@
  \frame{\lower.04\dimen@\hb@xt@.84\dimen@{\hss\vbox to.84\dimen@{\vss
  \hsize=.84\dimen@\baselineskip\z@\lineskip.01\dimen@\parskip\z@skip
  \leftskip\@flushglue\rightskip\@flushglue\parfillskip\z@skip
  \parindent\z@\let\\\bx@fbs@c\leavevmode\let\sml=\bx@fbs@fs\bx@fbs@f
  #1\unskip\par\vss}\hss}}\hss}}}
\edef\bx@junit{\ifnum\bxEngineTypeX=\@ne 1zw\else 1em\fi}
\def\bx@fbs@c{\unskip\par\ignorespaces}
\def\bx@fbs@fnt{\global\let\bx@fbs@fnt\relax
  \global\font\bx@fbs@fs=phvr at 2.4pt
  \global\font\bx@fbs@f=phvr at 3.6pt}

%%<+> \bxCodeValueSeq\CS{<text>}
% Dispatcher for code value sequences.
\def\bxCodeValueSeq{%
  \let\bx@cvseq@branch\bx@cvseq@branch@hex
  \bx@cvseq@a}
\def\bx@cvseq@a#1#2{%
  {\catcode33=12%
   \xdef\bx@gtmpa{\uppercase{\def\noexpand\bx@tmpa{#2}}}}%
  \let\bx@cvseq@do=#1\bx@gtmpa\relax
  \expandafter\bx@cvseq@b\bx@tmpa,!\@nil}
\def\bx@cvseq@b{\futurelet\bx@toka\bx@cvseq@branch}
\def\bx@cvseq@branch@hex{%
  \if      !\bx@toka \let\bx@nxt\bx@cvseq@fin
  \else\if "\bx@toka \let\bx@nxt\bx@cvseq@pfx
  \else\if '\bx@toka \let\bx@nxt\bx@cvseq@pfx
  \else\if +\bx@toka \let\bx@nxt\bx@cvseq@pfx
  \else \let\bx@nxt\bx@cvseq@hex \fi\fi\fi\fi
  \bx@nxt}
\def\bx@cvseq@pfx{%
  \afterassignment\bx@cvseq@post \bxUcv=}
\def\bx@cvseq@hex{%
  \afterassignment\bx@cvseq@post \bxUcv="0}
\def\bx@cvseq@post#1{%
  \if ,#1\expandafter\bx@cvseq@post@a
  \else \bx@err@ilnfm \expandafter\bx@cvseq@fin \fi}
\def\bx@cvseq@post@a{%
  \bx@cvseq@do \bx@cvseq@b}
\def\bx@cvseq@fin#1\@nil{}

%%<+> \bxCodeValueSeqD\CS{<text>}
% Same as \bxCodeValueSeq, except that it defaults to decimal.
\def\bxCodeValueSeqD{%
  \let\bx@cvseq@branch\bx@cvseq@branch@dec \bx@cvseq@a}
\def\bx@cvseq@branch@dec{%
  \if      !\bx@toka \let\bx@nxt\bx@cvseq@fin
  \else    \let\bx@nxt\bx@cvseq@pfx \fi
  \bx@nxt}

%%<+> \bxUx, \bxUI, \bxAJ, \bxJI, \bxKI
% The prefixed names for \Ux, etc. They are (more likely to be)
% collision-safe.
\let\bxUx\relax
\DeclareRobustCommand*\bxUx{\bxCodeValueSeq\bx@Ux}
\let\bxUI\relax
\DeclareRobustCommand*\bxUI{\bxCodeValueSeq\bx@UI}
\let\bxAJ\relax
\DeclareRobustCommand*\bxAJ{\bxCodeValueSeqD\bx@AJ}
\let\bxJI\relax
\DeclareRobustCommand*\bxJI{\bxCodeValueSeq\bx@JI}
\let\bxKI\relax
\DeclareRobustCommand*\bxKI{\bxCodeValueSeqD\bx@KI}

%%<+> \bxUHex{<value>}, \bxUInt{<value>}
% Alternative entry points for \Ux. The code value is specified in
% hexadecimal (\bxUHex) / decimal (\bxUInt) numerals.
\def\bxUHex{\protect\bx@Uxh}
\def\bxUqInt{\protect\bx@UInt}
\def\bx@UInt#1{\bxUcv=#1\relax\bx@Ux}

%% \bx@UIh@fb, \bx@AJ@fb
\def\bx@UIh@fb#1{\bxUcv="#1\relax\bxToHexFive\bxUcv
  \expandafter\bx@b@fb@five\bxHex[Ux]}
\def\bx@AJ@fb{\bxToDecFive\bxUcv
  \expandafter\bx@b@fb@five\bxHex[AJ]}
\def\bx@JI@fb{\bxToHexFour\bxUcv
  \expandafter\bx@b@fb@four\bxHex[JI]}
\def\bx@KI@fb{\bxToDecFive\bxUcv
  \expandafter\bx@b@fb@five\bxHex[KI]}
\def\bx@b@fb@five#1#2#3#4#5#6[#7]{%
  \bxFallbackSym{{\sml#7}\\\bx@hx@supzero#1#2#3\\#4#5}}
\def\bx@b@fb@four#1#2#3#4#5[#6]{%
  \bxFallbackSym{{\sml#6}\\#1#2\\#3#4}}
\def\bx@hx@supzero#1{\ifx0#1\else\expandafter#1\fi}

%%%% \bx@assign@Ux
% Picks up appropriate drivers \bx@Ux etc. according to current
% load-states of packages.
% [driver]  [purpose]      [argument]    [default]
% \bx@Ux    Unicode        \bxUcv        go to \bx@UI
% \bx@Uxh   Unicode        #1(hex)       go to \bx@UIh
% \bx@UI    Unicode-ideo.  \bxUcv        go to \bx@UIh
% \bx@UIh   Unicode-ideo.  #1(hex)       fallback
% \bx@AJ    Adobe-Japan1   \bxUcv        fallback
% From the default value it can be said:
%  - Implemetations for \bx@Ux(h) must fallback on \bx@UI(h) when
%    they cannot be handle the given character, unless such fallback
%    is known to be useless.
%  - When you implement one of \bx@Ux or \bx@Uxh, then you must also
%    implement the other.
%  - When you implement \bx@UI, then you must also implement \bx@UIh.
%    (But you can implement only \bx@UIh.)
  % Initial values
\def\bx@Ux{\bx@UI}          % pass to \bx@UI
\def\bx@Uxh{\bx@UIh}        % pass to \bx@UIh
\def\bx@UI{\bxToHexUC\bxUcv\bx@UIh\bxHex} % pass to \bx@UIh
\let\bx@UIh\bx@UIh@fb       % 'fallback' driver
\let\bx@AJ\bx@AJ@fb         % 'fallback' driver
  % some helpers
  % one more step put so that \UTF could be redefined
\def\bx@UIh@otf{\UTF}
  % \UMS with the argument expanded
\def\bx@UIh@ums#1{\expandafter\UMS\expandafter{#1}}
  % \CID with \bxUcv
\def\bx@AJ@otf{\CID\bxUcv}
\def\bx@assign@Ux{%
  % If I have bxucs, then set \bx@Ux to \bx@Ux@ucs
  % (which fallbacks on \bx@UI in failure).
  \@ifpackageloaded{bxucs}{%
    \let\bx@Ux\bx@Ux@ucs\let\bx@Uxh\bx@Uxh@ucs}{}%
  % If I have ums or bxsuika then I use \UMS as \bx@UIh driver.
  \@ifpackageloaded{ums}{\bxOktrue}{\bxOkfalse}
  \@ifpackageloaded{bxsuika}{\bxOktrue}{}
  \ifbxOk \let\bx@UIh\bx@UIh@UMS \fi
  % If I have utf or otf then I use \UTF as \bx@UIh driver.
  \@ifpackageloaded{utf}{\bxOktrue}{\bxOkfalse}
  \@ifpackageloaded{otf}{\bxOktrue}{}
  \ifbxOk \let\bx@UIh\bx@UIh@otf \let\bx@AJ\bx@AJ@otf \fi
  % pxotf is same as otf, except that it also provides a direct
  % way to \bx@UI and \bx@AJ.
  \@ifpackageloaded{pxotf}{%
    \let\bx@UIh\bxUIh@otf \let\bx@UI\pxUcvUTF \let\bx@AJ\pxUcvCID}{}%
  % If I am upTeX I use \kchar.
  \ifnum\bx@b@uptex>\z@
    \def\bx@UI{\kchar\bxUcv}\def\bx@UIh##1{\kchar"##1\relax}%
  % If I am a Unicode-aware engine, then...
  \else\ifnum\bxEngineTypeX>\@ne % 
    \def\bx@UI{\char\bxUcv}\def\bx@UIh##1{\char"##1\relax}%
    \let\bx@Ux\bx@UI \let\bx@Uxh\bx@UIh
    % zxjatype provides better handling of \UI & \Ux.
    \@ifpackageloaded{zxjatype}{%
      \let\bx@UI\zxjt@UI \let\bx@UIh\zxjt@UIh
      \let\bx@Ux\zxjt@Ux \let\bx@Uxh\zxjt@Uxh}{}%
    % zxotf provides \CID comamnd.
    \@ifpackageloaded{zxotf}{%
      \def\bx@AJ{\CID\bxUcv}}{}%
  \fi\fi
  % If \bx@UIh@usr is known I use it.
  \ifx\bx@UIh@usr\@undefined\else \let\bx@UIh\bx@UIh@usr \fi
  \let\bx@setuidriver@a\bx@setuidriver@b
  \ifx\bx@AJ@usr\@undefined\else \let\bx@AJ\bx@AJ@usr \fi
  \let\bx@setajdriver@a\bx@setajdriver@b
}
\@onlypreamble\bx@assign@Ux

%%%% \setUIdriver\CS
\def\setUIdriver#1{%
  \ifx#1\@undefined \bx@err@uddrv#1%
  \else \bx@setuidriver@a#1\fi}
\def\bx@setuidriver@a#1{\let\bx@UIh@usr#1}
\def\bx@setuidriver@b#1{\let\bx@UIh#1}

%%%% \setAJdriver\CS
\def\setAJdriver#1{%
  \ifx#1\@undefined \bx@err@uddrv#1%
  \else \bx@setajdriver@a#1\fi}
\def\bx@setajdriver@a#1{\let\bx@AJ@usr#1}
\def\bx@setajdriver@b#1{\let\bx@AJ#1}

%%%% \bx@assign@JI
% Picks up appropriate drivers \bx@JI etc.
% [driver]  [purpose]      [argument]    [default]
% \bx@JI    JIS-code       \bxUcv        fallback
% \bx@KI    Kuten-code     \bxUcv        fallback
\def\bx@JI{\bx@JI@fb}
\def\bx@KI{\bx@KI@fb}
\def\bx@assign@JI{%
  % \JI & \KI are available only in (u)pTeX
  \ifnum\bxEngineTypeX=\@ne
    \def\bx@JI{\char\jis\bxUcv\relax}\let\bx@KI\bx@KI@pTeX
  \fi
}
\ifnum\bxEngineTypeX=\@ne     %----<*pTeX>
%% \bx@KI@pTeX
  % \KI{2????} (0213 plane 2) works only when kanji-internal=sjis
\mathchardef\bx@ki@max=\ifnum\jis"2121="8140 30000\else 20000\fi\relax
\mathchardef\bx@ki@myr=10000
\mathchardef\bx@ki@bas="2020
\let\bx@ki@char=\char
\ifx\kchar\@undefined\else \let\bx@ki@char=\kchar \fi
\def\bx@KI@pTeX{%
  \ifnum\bxUcv<\bx@ki@max\else \bxUcv=\z@ \fi
  \ifnum\bxUcv<\bx@ki@myr \advance\bxUcv\bx@ki@myr \fi
  \expandafter\bx@ki@ptex@a\the\bxUcv\@nil
  \bx@ki@char\jis\bxUcv\relax}
\bgroup \@tempcnta=94
  \@for\bx@tmpa:=01,08,03,04,05,12,13,14,15\do{\advance\@tempcnta\@ne
    \expandafter\xdef\csname bx@JSH/\bx@tmpa\endcsname{\the\@tempcnta}}
  \@tempcntb=77 \loop
    \advance\@tempcnta\@ne \advance\@tempcntb\@ne
    \expandafter\xdef\csname bx@JSH/\the\@tempcntb\endcsname
     {\the\@tempcnta}%
  \ifnum\@tempcntb<94 \repeat
\egroup
\def\bx@ki@ptex@a#1#2#3#4#5#6\@nil{%
  \ifnum#1=\@ne \bxUcv=#2#3\relax
  \else \bxUcv=0\csname bx@JSH/#2#3\endcsname\relax \fi
  \multiply\bxUcv\@cclvi \advance\bxUcv#4#5\relax
  \advance\bxUcv\bx@ki@bas}
\fi                           %----</pTeX>
\@onlypreamble\bx@assign@JI

%%------ Assignment check

%%%% \bxAssign <assignment>\relax
\def\bxAssign{%
  \bxOkfalse\afterassignment\bx@b@assign@a}
\def\bx@b@assign@a#1\relax{%
  \def\bx@tmpa{#1}\ifx\bx@tmpa\@empty\bxOktrue\fi}

%%------ Case conversion

%%%% \bxToLower{<string>}
% Sets to \bxRes <string> converted to lowercase.
\def\bxToLower#1{%
  \edef\bxRes{\lowercase{\def\noexpand\bxRes{#1}}}\bxRes}

%%%% \bxToUpper{<string>}
\def\bxToUpper#1{%
  \edef\bxRes{\uppercase{\def\noexpand\bxRes{#1}}}\bxRes}

%%------ Document-level specials
% Such specials must come before any typeset commands in DVI.

%%%% \bxDocumentSpecial{<text>}
% Appends \special{<text>} to the list of 'document-level specials'.
% A simple use of \AtBeginDvi will do in this case.
\def\bxDocumentSpecial#1{%
  \AtBeginDvi{\special{#1}}}
\@onlypreamble\bxDocumentSpecial

%%%% \bxDocumentSpecialUrgent{<text>}
% Similar to the above, but it demands that the given special be
% placed as early as possible.
\def\bxDocumentSpecialUrgent#1{%
  \bx@docspec@urgent@init
  \bx@prepend@box\bx@dsu@box{\special{#1}}}
\def\bx@prepend@box#1#2{%
  \global\setbox#1\vbox{#2\unvbox#1}}
\providecommand\bx@docspec@urgent@init{\bx@docspec@urgent@init@do}
\def\bx@docspec@urgent@init@do{%
  \newbox\bx@dsu@box
  \AtBeginDocument{%
    \ifx\AtBeginShipoutFirst\@undefined
      \bx@prepend@box\@begindvibox{\unvbox\bx@dsu@box}%
    \else % 'atbegshi' package is in use
      {\toks@{\unvbox\bx@dsu@box}%
       \toks\tw@\expandafter{\AtBegShi@HookFirst}%
       \xdef\AtBegShi@HookFirst{\the\toks@\the\toks\tw@}}
    \fi}%
  \bxNullify\bx@docspec@urgent@init}
\@onlypreamble\bxDocumentSpecialUrgent
\@onlypreamble\bx@docspec@urgent@init
\@onlypreamble\bx@docspec@urgent@init@do

%%%% \recordpapersize
\def\recordpapersize{%
  \bxDocumentSpecial{papersize=\the\paperwidth,\the\paperheight}%
  \bxNullify\recordpapersize}
\@onlypreamble\recordpapersize

%%<*> \dvipdfmxmapline{<text>}
%%<*> \dvipdfmxmapfile{<text>}
\def\dvipdfmxmapline#1{\bxDocumentSpecial{pdf:mapline #1}}
\@onlypreamble\dvipdfmxmapline
\def\dvipdfmxmapfile#1{\bxDocumentSpecial{pdf:mapfile #1}}
\@onlypreamble\dvipdfmxmapfile

%%----- "safe-caret" module
\newif\ifbx@acr@ok
\newif\ifbx@acr@needed \bx@acr@neededfalse
\newif\ifbx@safecaret \bx@safecaretfalse
\catcode`\^=13

%%%%<*> \safecaret
\def\safecaret{%
  \bx@safecarettrue}
\@onlypreamble\safecaret

%%%% \bxEnableSafeCaret
\def\bxEnableSafeCaret{%
  \bx@acr@neededtrue}
\@onlypreamble\bxEnableSafeCaret

%%%% \bx@acr@defbytes
\def\bx@acr@defbytes{%
  \bgroup\@tempcnta128\lccode`\Z=`\B\catcode`\^\active
    \@whilenum\@tempcnta<256 \do{%
      \lccode`\^\@tempcnta \chardef\bx@acr@tok\@tempcnta
      \expandafter\bx@acr@defbytes@do@a\meaning\bx@acr@tok\@nil
      \advance\@tempcnta\@ne}\egroup}
\def\bx@acr@defbytes@do@a#1"#2\@nil{% name is bx@acr@B/ab etc.
  \lowercase{\expandafter\gdef\csname bx@acr@Z/#2\endcsname{^}}}
\@onlypreamble\bx@acr@defbytes
\@onlypreamble\bx@acr@defbytes@do@a

%%%% \bx@acr@caret
%% This command should be \let'd to active <^>.
\def\bx@acr@caret{%
  \let\bx@acr@out\@empty \bx@acr@caret@a}
\def\bx@acr@caret@a{%
  \bx@acr@nextcaret{\bx@acr@caret@b}{\bx@acr@finish{\bx@acr@nxc}}}
\def\bx@acr@caret@b#1{%
  \let\bx@acr@hex\@empty
  \bx@acr@nexthexdgt{\bx@acr@caret@c}{\bx@acr@finish{\bx@acr@nxdc}}}
\def\bx@acr@caret@c#1{%
  \edef\bx@acr@hex{#1}%
  \bx@acr@nexthexdgt{\bx@acr@caret@d}{\bx@acr@finish{\bx@acr@nxdc\bx@acr@hex}}}
\def\bx@acr@caret@d#1{%
  \edef\bx@acr@hex{\bx@acr@hex#1}%
  \expandafter\bx@acr@caret@e\csname bx@acr@B/\bx@acr@hex\endcsname
  \bx@acr@ifok{\bx@acr@nextcaret{\bx@acr@caret@g}{\bx@acr@finish{}}}%
    {\bx@acr@finish{\bx@acr@nxdc\bx@acr@hex}}}
\def\bx@acr@caret@e#1{%
  \ifx#1\relax \bx@acr@okfalse
  \else \bx@acr@oktrue \expandafter\bx@acr@caret@f#1\fi}
\def\bx@acr@caret@f#1{%
  \expandafter\def\expandafter\bx@acr@out\expandafter{\bx@acr@out#1}}
\def\bx@acr@caret@g#1{\bx@acr@caret@a}
\def\bx@acr@nxc{\noexpand^}
\def\bx@acr@nxdc{\noexpand^\noexpand^}
\def\bx@acr@nextcaret#1#2{%
  \def\bx@acr@tmpa{#1}\def\bx@acr@tmpb{#2}%
  \futurelet\bx@acr@tok\bx@acr@nextcaret@a}
\def\bx@acr@nextcaret@a{%
  \let\bx@acr@nxt\bx@acr@tmpb
  \ifx^\bx@acr@tok \let\bx@acr@nxt\bx@acr@tmpa
  \fi \bx@acr@nxt}
\def\bx@acr@nexthexdgt#1#2{%
  \def\bx@acr@tmpa{#1}\def\bx@acr@tmpb{#2}%
  \futurelet\bx@acr@tok\bx@acr@nexthexdgt@a}
\def\bx@acr@nexthexdgt@a{%
  \let\bx@acr@nxt\bx@acr@tmpb
  \ifcat @\noexpand\bx@acr@tok \let\bx@acr@nxt\bx@acr@tmpa \fi
  \ifcat "\noexpand\bx@acr@tok \let\bx@acr@nxt\bx@acr@tmpa \fi
  \bx@acr@nxt}
\def\bx@acr@ifok{%
  \ifbx@acr@ok \expandafter\@firstoftwo \else \expandafter \@secondoftwo \fi}
\def\bx@acr@finish#1{%
  \edef\bx@acr@nxt{#1}%
  \ifx\bx@acr@out\@empty \bx@acr@oktrue \else \bx@acr@okfalse \fi
  \bx@acr@ifok{\expandafter\bx@acr@finish@a\bx@acr@nxt\@nil}%
    {\expandafter\bx@acr@out\bx@acr@nxt}}
\def\bx@acr@finish@a#1#2\@nil{%
    % replace first ^ with normal one to avoid loop
  \bx@acr@normcaret #2}
\let\bx@acr@normcaret\sp % this does not mean much

%%%% \bx@acr@initialize
\def\bx@acr@initialize{%
  \bx@acr@defbytes}
\@onlypreamble\bx@acr@initialize

%%%% \bx@acr@annihilate
\def\bx@acr@annihilate{%
  \let\bx@acr@caret\relax\let\bx@acr@caret@a\relax
  \let\bx@acr@caret@b\relax\let\bx@acr@caret@c\relax
  \let\bx@acr@caret@d\relax\let\bx@acr@caret@e\relax
  \let\bx@acr@caret@f\relax\let\bx@acr@caret@g\relax
  \let\bx@acr@nextcaret\relax\let\bx@acr@nextcaret@a\relax
  \let\bx@acr@nexthexdgt\relax\let\bx@acr@nexthexdgt@a\relax
  \let\bx@acr@finish\relax\let\bx@acr@finish@a\relax}
\@onlypreamble\bx@acr@annihilate

%%%% \bx@acr@babel@hookcaret
\def\bx@acr@babel@hookcaret{%
  \expandafter\ifx\csname active@char\string^\endcsname\relax\else
    \declare@shorthand\system@group{^}{\bx@acr@caret}%
    \let\bx@acr@normcaret\bx@acr@babel@normcaret
    \bxEnableSafeCaret \fi}
\@onlypreamble\bx@acr@babel@hookcaret
\edef\bx@acr@babel@normcaret{%
  \expandafter\noexpand\csname normal@char\string^\endcsname}

%%%% \bx@acr@verbatim@hookcaret
\def\bx@acr@verbatim@hookcaret{%
  \g@addto@macro\@noligs{\catcode`\^\active\let^\bx@acr@caret
    \let\bx@acr@normcaret\bx@acr@verb@normcaret}%
  \bxEnableSafeCaret}
\begingroup \catcode`\^=12
\global\let\bx@acr@verb@normcaret=^\endgroup
\@onlypreamble\bx@acr@verbatim@hookcaret

%%%% begin-document hook
\providecommand*\bxBDHookSafeCaret{\bx@bdhook@safecaret@do}
\def\bx@bdhook@safecaret@do{%
  \AtBeginDocument{%
  %% function activated by '\safecaret'
  \ifbx@safecaret
    \@ifpackageloaded{babel}{\bx@acr@babel@hookcaret}{}%
    \bx@acr@verbatim@hookcaret
  \fi
  %% initialize or annihilate...
  \ifbx@acr@needed \bx@acr@initialize
  \else \bx@acr@annihilate \fi
  \let\bxEnableSafeCaret\@undefined}%
  \bxNullify\bxBDHookSafeCaret}
\@onlypreamble\bx@bdhook@safecaret@do

\catcode`\^=6
%%------ "shadow map" module

%%%% \bxUseShadowMap\CS{<map_tfm>}
\def\bxUseShadowMap#1#2{%
  \global\font#1=#2\relax}

%%%% \bxMap\CS
\mathchardef\bx@m@cc=256
\def\bxMap#1{\begingroup\count@\bxUcv\divide\count@\bx@m@cc
  \chardef\bx@m@hb\count@ \multiply\count@\bx@m@cc
  \advance\bxUcv-\count@ \setbox\z@\hbox{#1\bx@m@hb\char\bxUcv}%
  \global\bxUcv\wd\z@ \endgroup}

%%------ all done
\bx@b@resetcatcodes \let\bx@nxt\relax
\endinput
%% EOF
