%%
%% lltjfont.sty: derived from plfonts.dtx in pLaTeX.
%% 
\makeatletter
\let\k@encoding\@empty
\let\ck@encoding\@empty
% LuaTeX-ja uses JY3 and JT3 encodings.
\def\cy@encoding{JY3}\def\ct@encoding{JT3}

\let\k@family\@empty
\let\k@series\@empty
\let\k@shape\@empty
\def\curr@kfontshape{\k@encoding/\k@family/\k@series/\k@shape}
\def\rel@fontshape{\f@encoding/\f@family/\f@series/\f@shape}
\ifdefined\Cht\else\newdimen\Cht\fi
\ifdefined\cht\else\newdimen\cht\fi
\ifdefined\Cdp\else\newdimen\Cdp\fi
\ifdefined\cdp\else\newdimen\cdp\fi
\ifdefined\Cwd\else\newdimen\Cwd\fi
\ifdefined\cwd\else\newdimen\cwd\fi
\ifdefined\Cvs\else\newdimen\Cvs\fi
\ifdefined\cvs\else\newdimen\cvs\fi
\ifdefined\Chs\else\newdimen\Chs\fi
\ifdefined\chs\else\newdimen\chs\fi
\ifdefined\cHT\else\newdimen\cHT\fi
%\let\afont\font
\def\inlist@#1#2{%
  \def\in@@##1<#1>##2##3\in@@{%
    \ifx\in@##2\in@false\else\in@true\fi}%
  \in@@#2<#1>\in@\in@@}
\def\fam@elt{\noexpand\fam@elt}
\def\enc@elt{\noexpand\enc@elt}
\let\kenc@list\@empty
\let\kyenc@list\@empty
\let\ktenc@list\@empty
\newbox\tstrutbox
\newbox\zstrutbox
\def\strut{\relax
  \ifydir
    \ifmmode\copy\strutbox\else\unhcopy\strutbox\fi
  \else
    \ifmmode\copy\tstrutbox\else\unhcopy\tstrutbox\fi
  \fi
}
\def\tstrut{\relax\hbox{\tate
   \ifmmode\copy\tstrutbox\else\unhcopy\tstrutbox\fi}}
\def\zstrut{\relax\hbox{\tate
   \ifmmode\copy\zstrutbox\else\unhcopy\zstrutbox\fi}}
\def\DeclareFontEncoding{%
  \begingroup
  \nfss@catcodes
  \expandafter\endgroup
  \DeclareFontEncoding@}
\def\DeclareFontEncoding@#1#2#3{%
  \expandafter
  \ifx\csname T@#1\endcsname\relax
     \def\cdp@elt{\noexpand\cdp@elt}%
     \xdef\cdp@list{\cdp@list\cdp@elt{#1}%
                    {\default@family}{\default@series}%
                    {\default@shape}}%
     \expandafter\let\csname#1-cmd\endcsname\@changed@cmd
     \def\enc@elt{\noexpand\enc@elt}%
  \else
     \@font@info{Redeclaring font encoding #1}%
  \fi
  \global\@namedef{T@#1}{#2}%
  \global\@namedef{M@#1}{\default@M#3}%
  \xdef\LastDeclaredEncoding{#1}%
  }
\def\DeclareKanjiEncoding#1{%
  \@latex@warning{%
     The \string\DeclareKanjiEncoding\space is obsoleted command.  Please use
     \MessageBreak
     the \string\DeclareTateKanjiEncoding\space for `Tate-kumi' encoding, and
     \MessageBreak
     the \string\DeclareYokoKanjiEncoding\space for `Yoko-kumi' encoding.
     \MessageBreak
     I treat the `#1' encoding as `Yoko-kumi'.}
  \DeclareYokoKanjiEncoding{#1}%
}
\def\DeclareYokoKanjiEncoding{%
  \begingroup
  \nfss@catcodes
  \expandafter\endgroup
  \DeclareYokoKanjiEncoding@}
\def\DeclareYokoKanjiEncoding@#1#2#3{%
  \expandafter
  \ifx\csname T@#1\endcsname\relax
    \def\cdp@elt{\noexpand\cdp@elt}%
    \xdef\cdp@list{\cdp@list\cdp@elt{#1}%
                    {\default@k@family}{\default@k@series}%
                    {\default@k@shape}}%
    \expandafter\let\csname#1-cmd\endcsname\@changed@kcmd
    \def\enc@elt{\noexpand\enc@elt}%
    \xdef\kyenc@list{\kyenc@list\enc@elt<#1>}%
    \xdef\kenc@list{\kenc@list\enc@elt<#1>}%
  \else
    \@font@info{Redeclaring KANJI (yoko) font encoding #1}%
  \fi
  \global\@namedef{T@#1}{#2}%
  \global\@namedef{M@#1}{\default@KM#3}%
  }
\def\DeclareTateKanjiEncoding{%
  \begingroup
  \nfss@catcodes
  \expandafter\endgroup
  \DeclareTateKanjiEncoding@}
\def\DeclareTateKanjiEncoding@#1#2#3{%
  \expandafter
  \ifx\csname T@#1\endcsname\relax
    \def\cdp@elt{\noexpand\cdp@elt}%
    \xdef\cdp@list{\cdp@list\cdp@elt{#1}%
                    {\default@k@family}{\default@k@series}%
                    {\default@k@shape}}%
    \expandafter\let\csname#1-cmd\endcsname\@changed@kcmd
    \def\enc@elt{\noexpand\enc@elt}%
    \xdef\ktenc@list{\ktenc@list\enc@elt<#1>}%
    \xdef\kenc@list{\kenc@list\enc@elt<#1>}%
  \else
    \@font@info{Redeclaring KANJI (tate) font encoding #1}%
  \fi
  \global\@namedef{T@#1}{#2}%
  \global\@namedef{M@#1}{\default@KM#3}%
  }
\@onlypreamble\DeclareKanjiEncoding
\@onlypreamble\DeclareYokoKanjiEncoding
\@onlypreamble\DeclareYokoKanjiEncoding@
\@onlypreamble\DeclareTateKanjiEncoding
\@onlypreamble\DeclareTateKanjiEncoding@
\def\DeclareKanjiEncodingDefaults#1#2{%
  \ifx\relax#1\else
    \ifx\default@KT\@empty\else
      \@font@info{Overwriting KANJI encoding scheme text defaults}%
    \fi
    \gdef\default@KT{#1}%
  \fi
  \ifx\relax#2\else
    \ifx\default@KM\@empty\else
      \@font@info{Overwriting KANJI encoding scheme math defaults}%
    \fi
    \gdef\default@KM{#2}%
  \fi}
\let\default@KT\@empty
\let\default@KM\@empty
\@onlypreamble\DeclareKanjiEncodingDefaults
\def\DeclareFontFamily#1#2#3{%
 \@ifundefined{T@#1}%
    {\@latex@error{Encoding scheme `#1' unknown}\@eha}%
    {\def\reserved@a{#3}%
     \global
     \expandafter\let\csname #1+#2\expandafter\endcsname
            \ifx \reserved@a\@empty
              \@empty
            \else \reserved@a
            \fi
    }%
}
\def\DeclareKanjiFamily#1#2#3{%
 \@ifundefined{T@#1}%
    {\@latex@error{KANJI Encoding scheme `#1' unknown}\@eha}%
    {\def\reserved@a{#3}%
     \global
     \expandafter\let\csname #1+#2\expandafter\endcsname
            \ifx \reserved@a\@empty
              \@empty
            \else \reserved@a
            \fi
     }%
}
\def\DeclareKanjiSubstitution#1#2#3#4{%
  \expandafter\ifx\csname T@#1\endcsname\relax
    \@latex@error{KANJI Encoding scheme `#1' unknown}\@eha
  \else
    \begingroup
       \def\reserved@a{#1}%
       \toks@{}%
       \def\cdp@elt##1##2##3##4{%
         \def\reserved@b{##1}%
         \ifx\reserved@a\reserved@b
           \addto@hook\toks@{\cdp@elt{#1}{#2}{#3}{#4}}%
         \else
           \addto@hook\toks@{\cdp@elt{##1}{##2}{##3}{##4}}%
         \fi}%
       \cdp@list
       \xdef\cdp@list{\the\toks@}%
    \endgroup
    \global\@namedef{D@#1}{\def\default@family{#2}%
                           \def\default@series{#3}%
                           \def\default@shape{#4}}%
  \fi}
\def\DeclareErrorKanjiFont#1#2#3#4#5{%
   \xdef\error@kfontshape{%
      \noexpand\expandafter\noexpand\split@name\noexpand\string
      \expandafter\noexpand\csname#1/#2/#3/#4/#5\endcsname
      \noexpand\@nil}%
   \gdef\default@k@family{#2}%
   \gdef\default@k@series{#3}%
   \gdef\default@k@shape{#4}%
   \global\let\k@family\default@k@family
   \global\let\k@series\default@k@series
   \global\let\k@shape\default@k@shape
   \gdef\f@size{#5}%
   \gdef\f@baselineskip{#5pt}}
\@onlypreamble\DeclareKanjiSubstitution
\@onlypreamble\DeclareErrorKanjiFont
\def\DeclareFixedFont#1#2#3#4#5#6{%
   \begingroup
      \math@fontsfalse
      \every@math@size{}%
      \fontsize{#6}\z@
      \edef\tmp@item{{#2}}%
      \expandafter\expandafter\expandafter
      \inlist@\expandafter\tmp@item\expandafter{\kyenc@list}%
      \ifin@
        \usekanji{#2}{#3}{#4}{#5}%
        \global\let#1\getjfont
      \else
        %\expandafter\expandafter\expandafter
        %\inlist@\expandafter\tmp@item\expandafter{\ktenc@list}%
        %\ifin@
        %  \usekanji{#2}{#3}{#4}{#5}%
        %  \let\font\tfont
        %\else
          \useroman{#2}{#3}{#4}{#5}%
          \global\expandafter\let\expandafter#1\the\font
        %\fi
      \fi
   \endgroup
  }
\def\reDeclareMathAlphabet#1#2#3{%
  \edef#1{\noexpand\protect\expandafter\noexpand\csname%
    \expandafter\@gobble\string#1\space\space\endcsname}%
  \edef\@tempa{\expandafter\@gobble\string#2}%
  \edef\@tempb{\expandafter\@gobble\string#3}%
  \edef\@tempc{\string @\expandafter\@gobbletwo\string#2}%
  \ifx\@tempc\@tempa%
    \edef\@tempa{\expandafter\@gobbletwo\string#2}%
    \edef\@tempb{\expandafter\@gobbletwo\string#3}%
  \fi
  \expandafter\edef\csname\expandafter\@gobble\string#1\space\space\endcsname%
    {\noexpand\DualLang@mathalph@bet%
      {\expandafter\noexpand\csname\@tempa\space\endcsname}%
      {\expandafter\noexpand\csname\@tempb\space\endcsname}%
  }%
}
\@onlypreamble\reDeclareMathAlphabet
\def\DualLang@mathalph@bet#1#2{%
  \relax\ifmmode
    \ifx\math@bgroup\bgroup%     2e normal style     (\mathrm{...})
      \bgroup\let\DualLang@Mfontsw\DLMfontsw@standard
    \else
      \ifx\math@bgroup\relax%    2e two letter style (\rm->\mathrm)
        \let\DualLang@Mfontsw\DLMfontsw@oldstyle
      \else
        \bgroup\let\DualLang@Mfontsw\DLMfontsw@standard
      \fi
    \fi
  \else
    \let\DualLang@Mfontsw\@firstoftwo
  \fi
  \DualLang@Mfontsw{#1}{#2}%
}
\def\DLMfontsw@standard#1#2#3{#1{#2{#3}}\egroup}
\def\DLMfontsw@oldstyle#1#2{#1\relax\@fontswitch\relax{#2}}
\def\DLMfontsw@oldlfont#1#2{#1\relax#2\relax}
\def\all@shape{all}%
\def\DeclareRelationFont#1#2#3#4#5#6#7#8{%
  \def\rel@shape{#4}%
  \ifx\rel@shape\@empty
     \global
     \expandafter\def\csname rel@#1/#2/#3/all\endcsname{%
       \romanencoding{#5}\romanfamily{#6}%
       \romanseries{#7}}%
  \else
     \global
     \expandafter\def\csname rel@#1/#2/#3/#4\endcsname{%
       \romanencoding{#5}\romanfamily{#6}%
       \romanseries{#7}\romanshape{#8}}%
  \fi
}
\def\SetRelationFont#1#2#3#4#5#6#7#8{%
  \def\rel@shape{#4}%
  \ifx\rel@shape\@empty
     \expandafter\def\csname rel@#1/#2/#3/all\endcsname{%
       \romanencoding{#5}\romanfamily{#6}%
       \romanseries{#7}}%
  \else
     \expandafter\def\csname rel@#1/#2/#3/#4\endcsname{%
       \romanencoding{#5}\romanfamily{#6}%
       \romanseries{#7}\romanshape{#8}}%
  \fi
}
\newif\if@knjcmd
\def\userelfont{\@knjcmdtrue}
\DeclareRobustCommand\selectfont{%
  \let\tmp@error@fontshape\error@fontshape
  \let\error@fontshape\error@kfontshape
  \edef\tmp@item{{\k@encoding}}%
  \expandafter\expandafter\expandafter
  \inlist@\expandafter\tmp@item\expandafter{\kyenc@list}%
  \ifin@
    \let\cy@encoding\k@encoding
    \edef\ct@encoding{\csname t@enc@\k@encoding\endcsname}%
  \else
    \expandafter\expandafter\expandafter
    \inlist@\expandafter\tmp@item\expandafter{\ktenc@list}%
    \ifin@
      \let\ct@encoding\k@encoding
      \edef\cy@encoding{\csname y@enc@\k@encoding\endcsname}%
    \else
      \@latex@error{KANJI Encoding scheme `\k@encoding' unknown}\@eha
    \fi
  \fi
  %\let\font\tfont
  %\let\k@encoding\ct@encoding
  %\xdef\font@name{\csname\curr@kfontshape/\f@size\endcsname}%
  %\pickup@font
  %\font@name
  \let\k@encoding\cy@encoding
  \xdef\font@name{\csname\curr@kfontshape/\f@size\endcsname}%
  \pickup@jfont
  \font@name
  \expandafter\def\expandafter\k@encoding\tmp@item
  \kenc@update
  \let\error@fontshape\tmp@error@fontshape
  \if@knjcmd \@knjcmdfalse
    \expandafter\ifx
    \csname rel@\k@encoding/\k@family/\k@series/\k@shape\endcsname\relax
      \expandafter\ifx
         \csname rel@\k@encoding/\k@family/\k@series/all\endcsname\relax
      \else
         \csname rel@\k@encoding/\k@family/\k@series/all\endcsname
      \fi
    \else
       \csname rel@\k@encoding/\k@family/\k@series/\k@shape\endcsname
    \fi
  \fi
  %\let\font\afont
  \xdef\font@name{\csname\curr@fontshape/\f@size\endcsname}%
  \pickup@font
  \font@name
  \enc@update
  \ifx\f@linespread\baselinestretch \else
    \set@fontsize\baselinestretch\f@size\f@baselineskip
  \fi
  \size@update}
\def\KanjiEncodingPair#1#2{\@namedef{t@enc@#1}{#2}\@namedef{y@enc@#2}{#1}}
\KanjiEncodingPair{JY3}{JT3}
\def\set@fontsize#1#2#3{%
    \@defaultunits\@tempdimb#2pt\relax\@nnil
    \edef\f@size{\strip@pt\@tempdimb}%
    \@defaultunits\@tempskipa#3pt\relax\@nnil
    \edef\f@baselineskip{\the\@tempskipa}%
    \edef\f@linespread{#1}%
    \let\baselinestretch\f@linespread
    \def\size@update{%
      \baselineskip\f@baselineskip\relax
      \baselineskip\f@linespread\baselineskip
      \normalbaselineskip\baselineskip
      \adjustbaseline
      \setbox\strutbox\hbox{%\yoko
          \vrule\@width\z@
                \@height.7\baselineskip \@depth.3\baselineskip}%
      \setbox\tstrutbox\hbox{%\tate
          \vrule\@width\z@
                \@height.5\baselineskip \@depth.5\baselineskip}%
      \setbox\zstrutbox\hbox{%\tate
          \vrule\@width\z@
                \@height.7\baselineskip \@depth.3\baselineskip}%
        \let\size@update\relax}}
\newbox\adjust@box
\newdimen\adjust@dimen
\def\adjustbaseline{%
    \setbox\adjust@box\hbox{\ltjsetparameter{yjabaselineshift=\z@}あ}%
    \cht\ht\adjust@box
    \cdp\dp\adjust@box
    \cwd\wd\adjust@box
    \cvs\normalbaselineskip
    \chs\cwd
    \cHT\cht \advance\cHT\cdp
  % \iftdir
  %   \setbox\adjust@box\hbox{\tbaselineshift\z@ M}%
  %   \adjust@dimen\ht\adjust@box
  %   \advance\adjust@dimen\dp\adjust@box
  %   \advance\adjust@dimen-\cHT
  %   \divide\adjust@dimen\tw@
  %   \advance\adjust@dimen\cdp
  %   \advance\adjust@dimen-\dp\adjust@box
  %   \tbaselineshift\adjust@dimen
  % \fi
  }
\DeclareRobustCommand\romanencoding[1]{%
    \expandafter\ifx\csname T@#1\endcsname\relax
      \@latex@error{Encoding scheme `#1' unknown}\@eha
    \else
      \edef\f@encoding{#1}%
      \ifx\cf@encoding\f@encoding
        \let\enc@update\relax
      \else
        \let\enc@update\@@enc@update
      \fi
    \fi
}
\DeclareRobustCommand\kanjiencoding[1]{%
    \expandafter\ifx\csname T@#1\endcsname\relax
      \@latex@error{KANJI Encoding scheme `#1' unknown}\@eha
    \else
      \edef\k@encoding{#1}%
      \ifx\ck@encoding\k@encoding
         \let\kenc@update\relax
      \else
         \let\kenc@update\@@kenc@update
      \fi
    \fi
}
\DeclareRobustCommand\fontencoding[1]{%
  \edef\tmp@item{{#1}}%
  \expandafter\expandafter\expandafter
  \inlist@\expandafter\tmp@item\expandafter{\kenc@list}%
  \ifin@ \kanjiencoding{#1}\else\romanencoding{#1}\fi}
\def\@@kenc@update{%
  \default@KT
  \csname T@\k@encoding\endcsname
  \csname D@\k@encoding\endcsname
  \let\kenc@update\relax
  \let\ck@encoding\k@encoding
  \edef\tmp@item{{\k@encoding}}%
  \expandafter\expandafter\expandafter
  \inlist@\expandafter\tmp@item\expandafter{\kyenc@list}%
  \ifin@ \let\cy@encoding\k@encoding
  \else
    \expandafter\expandafter\expandafter
    \inlist@\expandafter\tmp@item\expandafter{\ktenc@list}%
    \ifin@ \let\ct@encoding\k@encoding
    \else
      \@latex@error{KANJI Encoding scheme `\k@encoding' unknown}\@eha
    \fi
  \fi
}
\let\kenc@update\relax
\def\@changed@kcmd#1#2{%
   \ifx\protect\@typeset@protect
      \@inmathwarn#1%
      \expandafter\ifx\csname\ck@encoding\string#1\endcsname\relax
         \expandafter\ifx\csname ?\string#1\endcsname\relax
            \expandafter\def\csname ?\string#1\endcsname{%
               \TextSymbolUnavailable#1%
            }%
         \fi
         \global\expandafter\let
               \csname\cf@encoding \string#1\expandafter\endcsname
               \csname ?\string#1\endcsname
      \fi
      \csname\ck@encoding\string#1%
         \expandafter\endcsname
   \else
      \noexpand#1%
   \fi}
\newif\if@notkfam
\newif\if@notffam
\newif\if@tempswz
\DeclareRobustCommand\romanfamily[1]{\edef\f@family{#1}}
\DeclareRobustCommand\kanjifamily[1]{\edef\k@family{#1}}
% \notkfam@list@<enc>: list of non-defined kanji families under <enc>
% \kfam@list@<enc>:    list of kanji families under <enc>, defined in a .fd file
\DeclareRobustCommand\fontfamily[1]{%
  \@notkfamfalse\@notffamfalse\edef\tmp@item{{#1}}%
  \ifcsname \k@encoding+#1\endcsname % if \k@encoding+#1 is already defined
    \edef\k@family{#1}%
  \else % search font definition file...
     \ifcsname kfam@list@\k@encoding\endcsname\else
       \expandafter\gdef\csname kfam@list@\k@encoding\endcsname{}
       \expandafter\gdef\csname notkfam@list@\k@encoding\endcsname{}
     \fi
     \edef\@templist{\@nameuse{kfam@list@\k@encoding}}%
     \expandafter\expandafter\expandafter\inlist@\expandafter\tmp@item\expandafter{\@templist}%
     \ifin@\edef\k@family{#1}\else%
       \edef\@templist{\@nameuse{notkfam@list@\k@encoding}}%
       \expandafter\expandafter\expandafter\inlist@\expandafter\tmp@item\expandafter{\@templist}%
       \ifin@\@notkfamtrue\else%
         \def\@temp##1+{\lowercase{\def\@@temp{##1}}}\expandafter\@temp\k@encoding+%
         \message{(I search kanjifont definition file: \@@temp#1.fd)}%
         \IfFileExists{\@@temp#1.fd}{\@tempswztrue}{\@tempswzfalse}%
         \if@tempswz
           \expandafter\xdef\csname kfam@list@\k@encoding\endcsname
             {\@nameuse{kfam@list@\k@encoding}\fam@elt<#1>}\edef\k@family{#1}%
         \else
           \expandafter\xdef\csname notkfam@list@\k@encoding\endcsname
             {\@nameuse{notkfam@list@\k@encoding}\fam@elt<#1>}\@notkfamtrue%
         \fi
       \fi
     \fi
  \fi
  \ifcsname \f@encoding+#1\endcsname
    \edef\f@family{#1}%
  \else % search font definition file...
     \ifcsname ffam@list@\f@encoding\endcsname\else
       \expandafter\gdef\csname ffam@list@\f@encoding\endcsname{}
       \expandafter\gdef\csname notffam@list@\f@encoding\endcsname{}
     \fi
     \edef\@templist{\@nameuse{ffam@list@\f@encoding}}%
     \expandafter\expandafter\expandafter\inlist@\expandafter\tmp@item\expandafter{\@templist}%
     \ifin@\edef\f@family{#1}\else
       \edef\@templist{\@nameuse{notffam@list@\f@encoding}}%
       \expandafter\expandafter\expandafter\inlist@\expandafter\tmp@item\expandafter{\@templist}%
       \ifin@\@notffamtrue\else
         \def\@temp##1+{\lowercase{\def\@@temp{##1}}}\expandafter\@temp\f@encoding+%
         \message{(I search font definition file: \@@temp#1.fd)}%
         \IfFileExists{\@@temp#1.fd}{\@tempswztrue}{\@tempswzfalse}%
         \if@tempswz
           \expandafter\xdef\csname ffam@list@\f@encoding\endcsname
             {\@nameuse{ffam@list@\f@encoding}\fam@elt<#1>}\edef\f@family{#1}%
         \else
           \expandafter\xdef\csname notffam@list@\f@encoding\endcsname
             {\@nameuse{notffam@list@\f@encoding}\fam@elt<#1>}\@notffamtrue%
         \fi
       \fi
     \fi
  \fi
  \if@notkfam\if@notffam
      {\ifcsname D@\k@encoding\endcsname\@nameuse{D@\k@encoding}\fi
        \xdef\@@temp{\default@family}}\edef\k@family{\@@temp}%
      {\ifcsname D@\f@encoding\endcsname\@nameuse{D@\f@encoding}\fi
        \xdef\@@temp{\default@family}}\edef\f@family{\@@temp}%
  \fi\fi
}
\DeclareRobustCommand\romanseries[1]{\edef\f@series{#1}}
\DeclareRobustCommand\kanjiseries[1]{\edef\k@series{#1}}
\DeclareRobustCommand\fontseries[1]{\kanjiseries{#1}\romanseries{#1}}
\DeclareRobustCommand\romanshape[1]{\edef\f@shape{#1}}
\DeclareRobustCommand\kanjishape[1]{\edef\k@shape{#1}}
\DeclareRobustCommand\fontshape[1]{\kanjishape{#1}\romanshape{#1}}
\def\usekanji#1#2#3#4{%
    \kanjiencoding{#1}\kanjifamily{#2}\kanjiseries{#3}\kanjishape{#4}%
    \selectfont\ignorespaces}
\def\useroman#1#2#3#4{%
    \romanencoding{#1}\romanfamily{#2}\romanseries{#3}\romanshape{#4}%
    \selectfont\ignorespaces}
\def\usefont#1#2#3#4{%
  \edef\tmp@item{{#1}}%
  \expandafter\expandafter\expandafter
  \inlist@\expandafter\tmp@item\expandafter{\kenc@list}%
  \ifin@ \usekanji{#1}{#2}{#3}{#4}%
  \else\useroman{#1}{#2}{#3}{#4}%
  \fi}
\DeclareRobustCommand\normalfont{%
    \kanjiencoding{\kanjiencodingdefault}%
    \kanjifamily{\kanjifamilydefault}%
    \kanjiseries{\kanjiseriesdefault}%
    \kanjishape{\kanjishapedefault}%
    \romanencoding{\encodingdefault}%
    \romanfamily{\familydefault}%
    \romanseries{\seriesdefault}%
    \romanshape{\shapedefault}%
    \selectfont\ignorespaces}
\adjustbaseline
\let\reset@font\normalfont
\DeclareRobustCommand\mcfamily
        {\not@math@alphabet\mcfamily\mathmc
         \kanjifamily\mcdefault\selectfont}
\DeclareRobustCommand\gtfamily
        {\not@math@alphabet\gtfamily\mathgt
         \kanjifamily\gtdefault\selectfont}
\let\romanprocess@table\process@table
\def\kanjiprocess@table{%
  \kanjiencoding{\kanjiencodingdefault}%
  \kanjifamily{\kanjifamilydefault}%
  \kanjiseries{\kanjiseriesdefault}%
  \kanjishape{\kanjishapedefault}%
}
\def\process@table{%
  \romanprocess@table
  \kanjiprocess@table
}
\@onlypreamble\romanprocess@table
\@onlypreamble\kanjiprocess@table
\DeclareTextCommandDefault{\textunderscore}{%
  \leavevmode\kern.06em
  %\iftdir\raise-\tbaselineshift\fi
  \vbox{\hrule\@width.3em}}

%%%%%% LuaTeX-ja specific

% #1 の展開結果から encoding 部分をとりだし，それが和文用かどうかの結果を \ifin@ に代入
\def\ltj@@IsFontJapanese#1{%
  \expandafter\ltj@@IsFontJapaneseX#1//:%
  \expandafter\expandafter\expandafter
  \inlist@\expandafter\tmp@item\expandafter{\kenc@list}}
\def\ltj@@IsFontJapaneseX#1/#2/:{\edef\tmp@item{{\directlua{tex.print('#1')}}}}


\let\extract@afont\extract@font
\def\pickup@jfont{%
    \expandafter \ifx \font@name \relax
       \let\extract@font\extract@jfont
       \define@newfont
       \let\extract@font\extract@afont
    \fi
    \let\getjfont\font@name}

\def\extract@jfont{%
    \get@external@font
    \expandafter\globaljfont\font@name\external@font\relax
    \font@name%\global\zw=\zw\global\zh=\zh
    \csname \f@encoding+\f@family\endcsname
    \csname\curr@fontshape\endcsname}

\let\ltj@@al@do@subst@correction=\do@subst@correction
\def\ltj@@ja@do@subst@correction{%
    \xdef\subst@correction{%
      \font@name
      \global\expandafter\let\csname \curr@fontshape/\f@size\endcsname\font@name%
      \relax}%
    \aftergroup\subst@correction
}
\def\do@subst@correction{%
   \ltj@@IsFontJapanese{\curr@fontshape}\ifin@
     \ltj@@ja@do@subst@correction
   \else
     \ltj@@al@do@subst@correction
   \fi
}
\let\@@italiccorr=\/

%%%%%% Japanese font for math mode

% \M@<enc> から その <enc> が和文用か調べる
{\catcode`M=12%
\gdef\ltj@@mathJapaneseFonts#1M#2#3\relax{\ltj@@IsFontJapanese{#3}}}

\let\ltj@@al@getanddefine@fonts=\getanddefine@fonts
\def\ltj@@ja@getanddefine@fonts#1#2{%
  \xdef\font@name{\csname \string#2/\tf@size\endcsname}%
  \pickup@jfont\let\textfont@name\font@name
  \xdef\font@name{\csname \string#2/\sf@size\endcsname}%
  \pickup@jfont\let\scriptfont@name\font@name
  \xdef\font@name{\csname \string#2/\ssf@size\endcsname}%
  \pickup@jfont
  \edef\math@fonts{\math@fonts\ltjsetparameter{%
    jatextfont={#1,\textfont@name},%
    jascriptfont={#1,\scriptfont@name},%
    jascriptscriptfont={#1,\font@name}}}%
}

\def\getanddefine@fonts#1#2{%
  \ltj@tempcnta=#1\ltj@@IsFontJapanese{\string#2}%
  \ifin@\let\ltj@temp=\ltj@@ja@getanddefine@fonts%
  \else \let\ltj@temp=\ltj@@al@getanddefine@fonts\fi
  \ltj@temp{#1}{#2}%
}
\def\use@mathgroup#1#2{\relax\ifmmode
  \math@bgroup
    \expandafter\ifx\csname M@\f@encoding\endcsname#1\else
    #1\fi\ltj@tempcnta=#2 \expandafter\ltj@@mathJapaneseFonts\string#1\relax%
    \ifin@\jfam#2\relax\else\mathgroup#2\relax\fi
  \expandafter\math@egroup\fi}%

\endinput
