%%
%% This is file 'bxutf8.def'.
%%
%
%% file declaration
\ProvidesFile{bxutf8.def}[2023/02/23 v1.2a BX UTF-8 input encoding]
%
% Note: All definitions in this file should be local.
% This file should not spill any spaces or \par.
%
%% code guards
% Here spaces are set back to effective. So great care must be taken
% so spaces should not be spilt out (in horizontal mode).
\catcode32=10\relax
\edef\bxus@i@restore@codes{%
  \catcode33=\the\catcode33%
  \catcode34=\the\catcode34%
  \catcode39=\the\catcode39%
  \catcode45=\the\catcode45%
  \catcode46=\the\catcode46%
  \catcode47=\the\catcode47%
  \catcode58=\the\catcode58%
  \catcode63=\the\catcode63%
  \catcode96=\the\catcode96%
\relax}
\catcode33=12 % <!>
\catcode34=12 % <">
\catcode39=12 % <'>
\catcode45=12 % <->
\catcode46=12 % <.>
\catcode47=12 % </>
\catcode58=12 % <:>
\catcode63=12 % <?>
\catcode96=12 % <`>
\g@addto@macro\bxus@i@restore@codes{%
  \let\bxus@i@restore@codes\@undefined}%
%
%--------------------------------------- dynamic part
%
% abort when this is first loaded after \begin{document}
\def\bxus@i@error{\PackageError{inputenc}}%
\ifx\@nodocument\relax
  \ifx\bxus@bxutf@loaded\@undefined
    \bxus@i@error
     {cannot load `bxutf8' after \string\begin{document}}\@eha
    \bxus@i@restore@codes\endinput\fi\fi
%
%% load 'utf8' encoding
\InputIfFileExists{utf8.def}{}%
  {\bxus@i@error{input encoding `utf8' unavailable}\@eha
    \bxus@i@restore@codes\endinput}%
\makeatletter     % is this needed??
%\@inpenc@test  % This is done in utf8.def
%
%% attach the hook
\def\UTFviii@two@octets#1#2{\expandafter\bxus@choice
  \csname u8:#1\string#2\endcsname#1#2.@}%
\def\UTFviii@three@octets#1#2#3{\expandafter\bxus@choice
  \csname u8:#1\string#2\string#3\endcsname#1#2#3@}%
\def\UTFviii@four@octets#1#2#3#4{\expandafter\bxus@choice@four
  \csname u8:#1\string#2\string#3\string#4\endcsname#1#2#3#4@}%
%
% When this file is reloaded, the 'static' part will be skipped.
\ifx\bxus@bxutf@loaded\@undefined\else
  \bxus@i@restore@codes\endinput \fi
\let\bxus@bxutf@loaded=t%
%
%--------------------------------------- static part
%
%% error messages
\def\bxus@err@uknbc{\bxus@i@error
  {Unknown non-BMP character found}%
  {bxutf8 cannot handle non-BMP characters.}}%
\def\bxus@err@nodrv{\bxus@i@error
  {Unicode driver not found}\@ehb}%
%
%% make tables for 'nybble conversion'
\def\bxus@fornbl{%
  \bxus@fornbl@a00001102203310411512613720821922A23B30C31D32E33FXXX}%
\def\bxus@fornbl@a#1#2#3{%
  \if X#1\else
  \@namedef{bxus@*#1#2}{#3}%
  \bxus@defchr{F#3}{0#1#2}\bxus@defchr{E#3}{#1#2}%
  \bxus@defchr{C#3}{000#1#2}\bxus@defchr{D#3}{001#1#2}%
  \bxus@defchr{8#3}{0#1#2}\bxus@defchr{9#3}{1#1#2}%
  \bxus@defchr{A#3}{2#1#2}\bxus@defchr{B#3}{3#1#2}%
  \expandafter\bxus@fornbl@a\fi}%
\def\bxus@defchr#1#2{%
  {\lccode`\!="#1%
   \lowercase{\expandafter\gdef\csname bxus@@!\endcsname{#2}}}}%
\bxus@fornbl \@namedef{bxus@@.}{}%
%
%% \bxcs@choice
% Called in the form
%   \bxus@choice[\u8:??]??.@  (2-byte) or
%   \bxus@choice[\u8:???]???@ (3-byte)
% where ?? is a (TF-8 byte string and [\u8:...] denotes a single
% control word. This macro expands to the control word if it is
% defined; otherwise expands to \bxus@proc.
\def\bxus@choice#1#2@{%
  \ifx#1\relax \bxus@proc#2%
  \else\expandafter#1\fi}%
%
%% \bxus@choice@four
\def\bxus@choice@four#1#2@{%
  \ifx#1\relax \bxus@prociv#2%
  \else\expandafter#1\fi}%
%
%% \bxus@proc
% When ??? is the UTF-8 string of U+XXXX (padded to length 3 with
% a dot), then \bxus@proc@a??? fully expands to \bxUHex{XXXX}.
% (\bxUHex should be robust.)
\def\bxus@proc#1#2#3{%
  \expandafter\bxus@proc@b\csname bxus@@\string#1\expandafter\endcsname
  \csname bxus@@\string#2\expandafter\endcsname
  \csname bxus@@\string#3\endcsname}%
\def\bxus@proc@b#1#2#3{%
  \expandafter\expandafter\expandafter\expandafter
  \expandafter\expandafter\expandafter\bxus@proc@c
  \expandafter\expandafter\expandafter#1%
  \expandafter#2#3}%
\def\bxus@proc@c#1#2#3#4#5#6#7#8{%
  \expandafter\bxus@proc@d\csname bxus@*#1#2\expandafter\endcsname
  \csname bxus@*#3#4\expandafter\endcsname
  \csname bxus@*#5#6\expandafter\endcsname
  \csname bxus@*#7#8\endcsname}%
\def\bxus@proc@d#1#2{%
  \expandafter\expandafter\expandafter\bxus@proc@e
  \expandafter#1#2\@nil}%
\def\bxus@proc@e#1\@nil#2#3{%
  \bxus@proc@f#2#3#1\@nil}%
\def\bxus@proc@f#1#2{%
  \expandafter\expandafter\expandafter\bxus@proc@g
  \expandafter#1#2\@nil}%
\def\bxus@proc@g#1\@nil#2\@nil{%
  \bxUHex{#2#1}}%
%
%% \bxus@prociv
% The variant of \bxus@proc that is adapted to 4-octet sequences.
\def\bxus@prociv#1#2#3#4{%
  \expandafter\bxus@prociv@a\csname bxus@@\string#1\expandafter\endcsname
  \csname bxus@@\string#2\expandafter\endcsname
  \csname bxus@@\string#3\expandafter\endcsname
  \csname bxus@@\string#4\endcsname}%
\def\bxus@prociv@a#1#2#3#4{%
  \expandafter\expandafter\expandafter\bxus@prociv@b
  \expandafter#3#4@#1#2}%
\def\bxus@prociv@b#1@#2#3{%
  \expandafter\expandafter\expandafter\bxus@prociv@c
  \expandafter#2#3#1@@}%
\def\bxus@prociv@c#1#2#3#4{%
  \expandafter\bxus@prociv@d\csname bxus@*#1#2\expandafter\endcsname
  \csname bxus@*#3#4\endcsname}%
\def\bxus@prociv@d#1#2{%
  \expandafter\expandafter\expandafter\bxus@prociv@e
  \expandafter#1#2}%
\def\bxus@prociv@e#1#2#3@#4@{\bxus@prociv@f#3@#4#1#2@}%
\def\bxus@prociv@f#1#2#3#4{%
  \expandafter\bxus@prociv@g\csname bxus@*#1#2\expandafter\endcsname
  \csname bxus@*#3#4\endcsname}%
\def\bxus@prociv@g#1#2{%
  \expandafter\expandafter\expandafter\bxus@prociv@h
  \expandafter#1#2}%
\def\bxus@prociv@h#1#2#3@#4@{\bxus@prociv@i#3@#4#1#2@}%
\def\bxus@prociv@i#1#2#3#4{%
  \expandafter\bxus@prociv@j\csname bxus@*#1#2\expandafter\endcsname
  \csname bxus@*#3#4\endcsname}%
\def\bxus@prociv@j#1#2{%
  \expandafter\expandafter\expandafter\bxus@prociv@k
  \expandafter#1#2}%
\def\bxus@prociv@k#1#2@#3@{\bxUHex{#3#1#2}}%
%
%
%% default (useless) Unicode driver
% Note: this \bxUHex definition is used for check.
\providecommand*\bxUHex{\bxUHexDummyBxutf}%
\newcommand*\bxUHexDummyBxutf[1]{?\protect\bxus@err@nodrv}%
%
%--------------------------------------- all done
\bxus@i@restore@codes
\endinput
%% EOF
